Thursday, November 14, 2013

C++ assigning values to arrays

If you need to assign values to a two dimensional array use a nested "for" loop

ex:
const int ROW = 3;
const int COLS = 2;
int arrayN[ROW][COLS];
int counter1, counter2;

for(counter1 = 0; counter1 < ROW; counter1++)
 { for(counter2 = 0; counter2 < COLS; counter2++)
   {
    cin >> arrayN[counter1][counter2];
   }




When spitting an array out

for(counter1 = 0; counter1< COLS; counter1++)
 { for(counter2 = 0; counter2 < ROW; counter2++)
   {
    cout << arrayN[counter1][counter2];
   }
}

No comments: