Skip to main content
deleted 439 characters in body
Source Link
dda
  • 1.6k
  • 1
  • 13
  • 18
    int P1[][12] = { //0-10
 
        {0}, //MODE
        {0, 0, 0, 0,}, //TOD variables
                          // OnHR, OnMin, OffHr, OffMin
        {0}, //DOW
        {0, 0, 0, 0,},  //Sunday
        {0, 0, 0, 0,},    // OnHR, OnMin, OffHr, OffMin
        {0, 0, 0, 0,},
        {0, 0, 0, 0,},
        {0, 0, 0, 0,},
        {0, 0, 0, 0,},
        {0, 0, 0, 0,},  //Saturday
        
        {0, 0, 0, 0, 0,} // Temperature variables
                            //Sensor, GreaterThan/LessThan, Temp, Buffer, State
        
    };
void HrMinBounce() {
      int i = 0;
      int q = 0;
 

      for (int i; i < 10; i++) { //iterating down Plug arrays
          for (int q; q < 4; q++) {
            if (i != 0 || i != 2) { //non time arrays
            
                if (q == 0 || q == 2) { //hours
                  if (P1[i][q] > 23) {
                    P1[i][q] = 0;
                  }
                  if (P1[i][q] < 0) {
                    P1[i][q] = 23;
                  }
                }//End of hours
 
                if (q == 1 || q == 3) { //minutes
                  if (P1[i][q] > 59) {
                    P1[i][q] = 0;
                  }
                  if (P1[i][q] < 0) {
                    P1[i][q] = 59;
                  }
                }//End of minutes
 
                
              }
          }
 
        }
      }

}

I have 5 of these arrays, so I was hoping to make one loop that could take care of all 5 at once. Here is my previous loop that works but I would have to copy and paste a whole lot to get the result without the extra for loop.

   void MinHrLimit() {
      int i = 0;

      for (int i; i < 3; i++) {
        if (i == 0 || i == 2) {
          if (P1[1][i] > 23) {
            P1[1][i] = 0;
          }
          if (P1[1][i] < 0) {
            P1[1][i] = 23;
          }
        }
      }

               
    }
    int P1[][12] = { //0-10
 
        {0}, //MODE
        {0, 0, 0, 0,}, //TOD variables
                          // OnHR, OnMin, OffHr, OffMin
        {0}, //DOW
        {0, 0, 0, 0,},  //Sunday
        {0, 0, 0, 0,},    // OnHR, OnMin, OffHr, OffMin
        {0, 0, 0, 0,},
        {0, 0, 0, 0,},
        {0, 0, 0, 0,},
        {0, 0, 0, 0,},
        {0, 0, 0, 0,},  //Saturday
        
        {0, 0, 0, 0, 0,} // Temperature variables
                            //Sensor, GreaterThan/LessThan, Temp, Buffer, State
        
    };
void HrMinBounce(){
      int i = 0;
      int q = 0;
 

      for(int i; i < 10; i++){ //iterating down Plug arrays
          for(int q; q < 4; q++){
            if(i != 0 || i != 2){ //non time arrays
            
                if(q == 0 || q == 2){//hours
                  if(P1[i][q] > 23){
                    P1[i][q] = 0;
                  }
                  if(P1[i][q] < 0){
                    P1[i][q] = 23;
                  }
                }//End of hours
 
                if(q == 1 || q == 3){//minutes
                  if(P1[i][q] > 59){
                    P1[i][q] = 0;
                  }
                  if(P1[i][q] < 0){
                    P1[i][q] = 59;
                  }
                }//End of minutes
 
                
              }
          }
 
        
      }

}

I have 5 of these arrays so I was hoping to make one loop that could take care of all 5 at once. Here is my previous loop that works but I would have to copy and paste a whole lot to get the result without the extra for loop.

   void MinHrLimit(){
      int i = 0;

      for(int i; i < 3; i++){
        if(i == 0 || i == 2){
          if(P1[1][i] > 23){
            P1[1][i] = 0;
          }
          if(P1[1][i] < 0){
            P1[1][i] = 23;
          }
        }
      }

               
    }
int P1[][12] = { //0-10
  {0}, //MODE
  {0, 0, 0, 0,}, //TOD variables
  // OnHR, OnMin, OffHr, OffMin
  {0}, //DOW
  {0, 0, 0, 0,},  //Sunday
  {0, 0, 0, 0,},    // OnHR, OnMin, OffHr, OffMin
  {0, 0, 0, 0,},
  {0, 0, 0, 0,},
  {0, 0, 0, 0,},
  {0, 0, 0, 0,},
  {0, 0, 0, 0,},  //Saturday
  {0, 0, 0, 0, 0,} // Temperature variables
  //Sensor, GreaterThan/LessThan, Temp, Buffer, State
};
void HrMinBounce() {
  int i = 0;
  int q = 0;

  for (int i; i < 10; i++) { //iterating down Plug arrays
    for (int q; q < 4; q++) {
      if (i != 0 || i != 2) { //non time arrays
        if (q == 0 || q == 2) { //hours
          if (P1[i][q] > 23) {
            P1[i][q] = 0;
          }
          if (P1[i][q] < 0) {
            P1[i][q] = 23;
          }
        }//End of hours
        if (q == 1 || q == 3) { //minutes
          if (P1[i][q] > 59) {
            P1[i][q] = 0;
          }
          if (P1[i][q] < 0) {
            P1[i][q] = 59;
          }
        }//End of minutes
      }
    }
  }
}

I have 5 of these arrays, so I was hoping to make one loop that could take care of all 5 at once. Here is my previous loop that works but I would have to copy and paste a whole lot to get the result without the extra for loop.

void MinHrLimit() {
  int i = 0;

  for (int i; i < 3; i++) {
    if (i == 0 || i == 2) {
      if (P1[1][i] > 23) {
        P1[1][i] = 0;
      }
      if (P1[1][i] < 0) {
        P1[1][i] = 23;
      }
    }
  }
}
Source Link
Tay
  • 13
  • 2

Nested for loop used to evaluate a two dimensional array of integers

I am trying to use a nested for loop to evaluate and change numbers within the two dimensional array. Basically these numbers are minutes, or hours. This function is meant to reset the button controlled numbers when they go above 23 (hours) or above 59 (minutes) and also from going below 0. The two dimensional array that I made also has three arrays that need to be skipped (P1[0], P1[2] and P1[10] because they do not contain any time variables. When I click the buttons and increment the numbers nothing unusual happens they just don't bounce back like they are supposed to.

Here is the two dimensional array:

    int P1[][12] = { //0-10

        {0}, //MODE
        {0, 0, 0, 0,}, //TOD variables
                          // OnHR, OnMin, OffHr, OffMin
        {0}, //DOW
        {0, 0, 0, 0,},  //Sunday
        {0, 0, 0, 0,},    // OnHR, OnMin, OffHr, OffMin
        {0, 0, 0, 0,},
        {0, 0, 0, 0,},
        {0, 0, 0, 0,},
        {0, 0, 0, 0,},
        {0, 0, 0, 0,},  //Saturday
        
        {0, 0, 0, 0, 0,} // Temperature variables
                            //Sensor, GreaterThan/LessThan, Temp, Buffer, State
        
    };

Here is the for loop that doesn't seem to work:

void HrMinBounce(){
      int i = 0;
      int q = 0;


      for(int i; i < 10; i++){ //iterating down Plug arrays
          for(int q; q < 4; q++){
            if(i != 0 || i != 2){ //non time arrays
            
                if(q == 0 || q == 2){//hours
                  if(P1[i][q] > 23){
                    P1[i][q] = 0;
                  }
                  if(P1[i][q] < 0){
                    P1[i][q] = 23;
                  }
                }//End of hours

                if(q == 1 || q == 3){//minutes
                  if(P1[i][q] > 59){
                    P1[i][q] = 0;
                  }
                  if(P1[i][q] < 0){
                    P1[i][q] = 59;
                  }
                }//End of minutes

                
              }
          }

        
      }

}

I have 5 of these arrays so I was hoping to make one loop that could take care of all 5 at once. Here is my previous loop that works but I would have to copy and paste a whole lot to get the result without the extra for loop.

   void MinHrLimit(){
      int i = 0;

      for(int i; i < 3; i++){
        if(i == 0 || i == 2){
          if(P1[1][i] > 23){
            P1[1][i] = 0;
          }
          if(P1[1][i] < 0){
            P1[1][i] = 23;
          }
        }
      }

               
    }