|
Home
Site Map Useful Tools Graphing Calc Scientific Calculator |
Two Dimensional Arrays in C++ |
Arrays and pointers have a close relationship. As we will soon see, an array in C++, is basically a constant pointer.
const int ROWS =3;
const int COLS = 3;
//declare a two dimensional array named matrix
int matrix[ROWS][COLS];
//populate with random numbers
for(i =0;i< ROWS; i++)
for(j= 0;j < COLS; j++)
matrix[i][j] = rand() % 100;
void printOut2D(int x[][COLS], int rows){
}
}
const int ROWS =3;
const int COLS = 4;
// create the douible array
int matrix[ROWS][COLS];
int i,j;
for(i =0;i< ROWS; i++)
for(j= 0;j<COLS; j++)
matrix[i][j] = rand() % 100;
//a pointer to COLS number of ints
// if COLS is 4 this is a pointer to
// an array of 4 ints
int (*ptr)[COLS] = matrix;
for(int i =0;i< ROWS; i++){
for(int j= 0;j<COLS; j++)
cout<<ptr[i][j] << " ";
cout<<endl;
}