2d arrays alina solovyova-vincent department of computer science engineering university of nevada,...
DESCRIPTION
Declarations char letters[3][4]; char letters[3][4]; or or const int row = 3; const int col = 4; char letters[row][col]; //preferred way const int row = 3; const int col = 4; char letters[row][col]; //preferred wayTRANSCRIPT
2D Arrays2D Arrays
Alina Solovyova-VincentAlina Solovyova-VincentDepartment of Computer Science & EngineeringDepartment of Computer Science & Engineering
University of Nevada, RenoUniversity of Nevada, RenoSpring 2006Spring 2006
DeclarationsDeclarations
datatype datatype array_name array_name [num_of_rows] [num_of_col];[num_of_rows] [num_of_col];
int array_1[10][10]; int array_1[10][10]; // indexed from (0,0) to (9,9)// indexed from (0,0) to (9,9)char array_2[3][4]; char array_2[3][4]; //indexed from (0,0) to (2,3)//indexed from (0,0) to (2,3)float array_3[2][10]; float array_3[2][10]; //indexed from (0,0) to (1,9)//indexed from (0,0) to (1,9)
The first index always indicates the row, the second the column.
DeclarationsDeclarations
char letters[3][4];char letters[3][4];
oror
const int row = 3;const int row = 3;const int col = 4;const int col = 4;char letters[row][col]; char letters[row][col]; //preferred way//preferred way
1-D vs. 2-D Arrays1-D vs. 2-D Arrays
00
11
22
33
44
0,00,0 0,10,1 0,20,2 0,30,3 0,40,4
1,01,0 1,11,1 1,21,2 1,31,3 1,41,4
2,02,0 2,12,1 2,22,2 2,32,3 2,42,4
3,03,0 3,13,1 3,23,2 3,33,3 3,43,4
4,04,0 4,14,1 4,24,2 4,34,3 4,44,4
int table[4][3];int table[4][3];
72 45 1572 45 1562 10 062 10 019 99 19 99 272738 24 89 38 24 89
table[2][2]
ExamplesExamples
float array_A[12][2]; float array_A[12][2];
char array_B[7][3]; char array_B[7][3];
int array_C[4][10]; int array_C[4][10];
Row-Major OrderRow-Major Order
72 45 1572 45 1562 10 062 10 019 99 2719 99 2738 24 8938 24 89
|72|45|15|62|10|0|19|99|27|38|24|89| ? | ? ||72|45|15|62|10|0|19|99|27|38|24|89| ? | ? |
InitializationInitialization 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,int nums[3][4] = {2,4,6,8, 1,3,5,71,3,5,7, ,
5,2,7,10}; 5,2,7,10};
int nums[3][4] = { {2,4,6,8},int nums[3][4] = { {2,4,6,8}, {{1,3,5,7}1,3,5,7}, ,
{{5,2,7,10} }; 5,2,7,10} };
Accessing All Elements of an ArrayAccessing All Elements of an Array
for ( for ( ii = 0; = 0; ii < row; < row; ii++ )++ ){{
for( for( jj=0; =0; jj<column; <column; jj++ )++ ) cout << bunch_of_nums[ cout << bunch_of_nums[ii][][jj] << ‘\t’; ] << ‘\t’;
cout << endl;cout << endl;}}
Accessing Individual ElementsAccessing Individual Elements
int num;int num;int bunch_of_nums[10][10];int bunch_of_nums[10][10];
bunch_of_nums[0][0] = 25;bunch_of_nums[0][0] = 25;num = bunch_of_nums[3][5];num = bunch_of_nums[3][5];
Using Subscript ExpressionsUsing Subscript Expressions
result = result + value[ result = result + value[ i + 2i + 2][ ][ i * numi * num] ;] ;
cout << num[ cout << num[ i % rowi % row ][ ][col-6col-6];];
WorksheetWorksheet Declare an array to store 5x5 matrix of Declare an array to store 5x5 matrix of
integers. integers. List all of the elements on the major List all of the elements on the major
diagonal.diagonal. List all of the elements on the minor List all of the elements on the minor
diagonal.diagonal. Write a code segment that would display Write a code segment that would display
all of the elements on the major diagonal.all of the elements on the major diagonal. Write a code segment that would display Write a code segment that would display
all of the elements on the minor diagonal.all of the elements on the minor diagonal.
const int size = 5;const int size = 5;
int square[size][size];int square[size][size];
// display major diagonal in one loop// display major diagonal in one loopfor (int i = 0; i < size; i++)for (int i = 0; i < size; i++)cout << square[i][i] << endl;cout << square[i][i] << endl;
// display minor diagonal in one loop// display minor diagonal in one loopfor (int i = 0; i < size; i++)for (int i = 0; i < size; i++)cout << square[i][size-1-i] << endl;cout << square[i][size-1-i] << endl;
const int size = 5;const int size = 5;
int square[size][size];int square[size][size];
// display major diagonal in two loops// display major diagonal in two loopsfor (int i = 0; i < size; i++)for (int i = 0; i < size; i++)for (int j = 0; j < size; j++)for (int j = 0; j < size; j++)
if (i == j)if (i == j)cout << square[i][j] << endl;cout << square[i][j] << endl;
// display minor diagonal in two loops// display minor diagonal in two loopsfor (int i = 0; i < size; i++)for (int i = 0; i < size; i++)for (int j = 0; j < size; j++)for (int j = 0; j < size; j++)
if (i + j == size-1)if (i + j == size-1)cout << square[i][j] << endl;cout << square[i][j] << endl;
WorksheetWorksheet
Write a code segment that declares Write a code segment that declares 45 x 33 array of integers and initializes all 45 x 33 array of integers and initializes all
elements to 0. The program should then elements to 0. The program should then prompt the user for a location in that prompt the user for a location in that array ( row number, column number) and array ( row number, column number) and tell the user whether it’s a valid location tell the user whether it’s a valid location ( i.e. within bounds). For example (4, 16) ( i.e. within bounds). For example (4, 16) is a valid, (-3, 5) is invalid, (13, 56) is is a valid, (-3, 5) is invalid, (13, 56) is invalid. If location is valid, display the invalid. If location is valid, display the contents of that location.contents of that location.
const int nrow = 45;const int nrow = 45;const int ncol = 33;const int ncol = 33;
int arr[nrow][ncol] = { 0 };int arr[nrow][ncol] = { 0 };
do {do {cout << "Enter row and column: ";cout << "Enter row and column: ";cout << r << c;cout << r << c;
}}while (r < 0 || r >= nrow ||while (r < 0 || r >= nrow || c < 0 || c >= ncol);c < 0 || c >= ncol);
cout << "The element in ("cout << "The element in ("<< r << ", " << c << ") is "<< r << ", " << c << ") is "<< arr[r][c];<< arr[r][c];
2-D Arrays in Functions2-D Arrays in Functions
void ProcessValues (int [ ][5], int, int); void ProcessValues (int [ ][5], int, int);
//works with ALL 2-d arrays that have//works with ALL 2-d arrays that have // exactly 5 columns: i.e. 10x5, 2x5 ….// exactly 5 columns: i.e. 10x5, 2x5 ….
int max(int[ ][2], int, int);int max(int[ ][2], int, int); void main()void main(){ {
const int nrow = 4, ncol = 2; const int nrow = 4, ncol = 2; int nums[nrow][ncol] = {3,2,5,13,1,7,9,4}; int nums[nrow][ncol] = {3,2,5,13,1,7,9,4};
cout << "The max value of the array is " cout << "The max value of the array is "
<< << max(nums, nrow , ncol)max(nums, nrow , ncol);;} }
int max(int vals[ ][2], int nr, int nc)int max(int vals[ ][2], int nr, int nc) { {
int i, j, max = vals[0][0]; int i, j, max = vals[0][0];
for (i = 0; i < nr; i++) for (i = 0; i < nr; i++) for (j = 0; j < nc; j++) for (j = 0; j < nc; j++) if (max < vals[i][j]) if (max < vals[i][j]) max = vals[i][j]; max = vals[i][j];
return max; return max; }}
WorksheetWorksheet
Write a function that receives an array of Write a function that receives an array of floating point values, number of rows and floating point values, number of rows and number of columns (known to be 7) in number of columns (known to be 7) in that array, and a “special” number that array, and a “special” number between 0 and 6. The function should between 0 and 6. The function should return the sum of all elements in the return the sum of all elements in the column that corresponds to the “special” column that corresponds to the “special” number.number.