190 likes | 257 Views
2D Arrays. Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006. Declarations. datatype array_name [num_of_rows] [num_of_col]; int array_1[10][10]; // indexed from (0,0) to (9,9)
E N D
2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006
Declarations datatype array_name [num_of_rows] [num_of_col]; int array_1[10][10]; // indexed from (0,0) to (9,9) char array_2[3][4]; //indexed from (0,0) to (2,3) float array_3[2][10]; //indexed from (0,0) to (1,9) The first index always indicates the row, the second the column.
Declarations char letters[3][4]; or const int row = 3;const int col = 4;char letters[row][col]; //preferred way
int table[4][3]; 72 45 1562 10 019 99 2738 24 89 table[2][2]
Examples float array_A[12][2]; char array_B[7][3]; int array_C[4][10];
Row-Major Order 72 45 1562 10 019 99 2738 24 89 |72|45|15|62|10|0|19|99|27|38|24|89| ? | ? |
Initialization int nums[3][4] = {2,4,6,8,1,3,5,7,5,2,7,10}; int nums[3][4] = {2,4,6,8, 1,3,5,7, 5,2,7,10}; int nums[3][4] = { {2,4,6,8}, {1,3,5,7}, {5,2,7,10} };
Accessing All Elements of an Array for ( i = 0; i < row; i++ ) { for( j=0; j<column; j++ ) cout << bunch_of_nums[i][j] << ‘\t’; cout << endl; }
Accessing Individual Elements int num;int bunch_of_nums[10][10];bunch_of_nums[0][0] = 25;num = bunch_of_nums[3][5];
Using Subscript Expressions result = result + value[ i + 2][ i * num] ; cout << num[ i % row ][col-6];
Worksheet • Declare an array to store 5x5 matrix of integers. • List all of the elements on the major diagonal. • List all of the elements on the minor diagonal. • Write a code segment that would display all of the elements on the major diagonal. • Write a code segment that would display all of the elements on the minor diagonal.
const int size = 5; int square[size][size]; // display major diagonal in one loop for (int i = 0; i < size; i++) cout << square[i][i] << endl; // display minor diagonal in one loop for (int i = 0; i < size; i++) cout << square[i][size-1-i] << endl;
const int size = 5; int square[size][size]; // display major diagonal in two loops for (int i = 0; i < size; i++) for (int j = 0; j < size; j++) if (i == j) cout << square[i][j] << endl; // display minor diagonal in two loops for (int i = 0; i < size; i++) for (int j = 0; j < size; j++) if (i + j == size-1) cout << square[i][j] << endl;
Worksheet Write a code segment that declares 45 x 33 array of integers and initializes all elements to 0. The program should then prompt the user for a location in that array ( row number, column number) and tell the user whether it’s a valid location ( i.e. within bounds). For example (4, 16) is a valid, (-3, 5) is invalid, (13, 56) is invalid. If location is valid, display the contents of that location.
const int nrow = 45; const int ncol = 33; int arr[nrow][ncol] = { 0 }; do { cout << "Enter row and column: "; cout << r << c; } while (r < 0 || r >= nrow || c < 0 || c >= ncol); cout << "The element in (" << r << ", " << c << ") is " << arr[r][c];
2-D Arrays in Functions void ProcessValues (int [ ][5], int, int); //works with ALL 2-d arrays that have // exactly 5 columns: i.e. 10x5, 2x5 ….
int max(int[ ][2], int, int); void main() { const int nrow = 4, ncol = 2; int nums[nrow][ncol] = {3,2,5,13,1,7,9,4}; cout << "The max value of the array is " << max(nums, nrow , ncol); } int max(int vals[ ][2], int nr, int nc) { int i, j, max = vals[0][0]; for (i = 0; i < nr; i++) for (j = 0; j < nc; j++) if (max < vals[i][j]) max = vals[i][j]; return max; }
Worksheet Write a function that receives an array of floating point values, number of rows and number of columns (known to be 7) in that array, and a “special” number between 0 and 6. The function should return the sum of all elements in the column that corresponds to the “special” number.