Download presentation
Presentation is loading. Please wait.
Published byAbigail Moody Modified over 9 years ago
1
CSE202: Lecture 16The Ohio State University1 Two Dimensional Arrays
2
CSE202: Lecture 16The Ohio State University2 Two-dimensional Arrays A two-dimensional array consists of both rows and columns of elements. It is essentially a matrix. To declare a two-dimensional array, we merely use two sets of square brackets. –The first contains the number of rows –The second contains the number of columns //Creates a 2D array with 3 rows and 4 columns int vals[3][4];
3
CSE202: Lecture 16The Ohio State University3 Indices in 2D arrays Assume that the two dimensional array called val is declared and looks like the following: To access the cell containing 6, we reference val[1][3], that is, row 1, column 3. val Col 0Col 1Col 2Col 3 Row 0816952 Row 1315276 Row 21425210
4
CSE202: Lecture 16The Ohio State University4 Using 2D arrays Just like 1D arrays, once you have specified the index, you are just working with a single variable of the given data type. Assignments and usage is still the same: sumRow0 = val[0][0] + val[0][1] + val[0][2] + val[0][3]; //assigns 72 to cell at row 2, column 3 val[2][3] = 72;
5
CSE202: Lecture 16The Ohio State University5 Initializing 2D arrays You can use additional braces to indicate when rows start and end, but you don’t have to do that. int val[3][4] = { {8,16,9,52}, {3,15,27,6}, {14,25,2,10} }; Or int val[3][4] = {8,16,9,52, 3,15,27,6, 14,25,2,10}; Or (correct, but not as clear as the first two): int val[3][4] = {8,16,9,52,3,15,27,6,14,25,2,10};
6
CSE202: Lecture 16The Ohio State University6 More on 2D arrays Initialization of 2D arrays is done in row order. 2D arrays work well with (for) loops like 1D arrays. However, to access all elements, typically you will need nested loops for 2D arrays. Can you see why?
7
CSE202: Lecture 16The Ohio State University7 Example Program... int main() { const int NUM_ROW(3); const int NUM_COL(4); int vals[NUM_ROW][NUM_COL] = { {11, 12, 13, 14}, {21, 22, 23, 24}, {31, 32, 33, 34} }; // output the array for (int row = 0; row < NUM_ROW; row++) { for (int col = 0; col < NUM_COL; col++) { cout << vals[row][col] << " "; } cout << endl; }...
8
CSE202: Lecture 16The Ohio State University8 Example Program... int vals[NUM_ROW][NUM_COL] = { {11, 12, 13, 14}, {21, 22, 23, 24}, {31, 32, 33, 34} }; // output the array for (int row = 0; row < NUM_ROW; row++) { for (int col = 0; col < NUM_COL; col++) { cout << vals[row][col] << " "; } cout << endl; }... > array2DExample.exe 11 12 13 14 21 22 23 24 31 32 33 34
9
CSE202: Lecture 16The Ohio State University9 Example Program (2)... int main() { const int NUM_ROW(3); const int NUM_COL(4); int vals[NUM_ROW][NUM_COL] = { {11, 12, 13, 14}, {21, 22, 23, 24}, {31, 32, 33, 34} }; // output the transpose of the array for (int col = 0; col < NUM_COL; col++) { for (int row = 0; row < NUM_ROW; row++) { cout << vals[row][col] << " "; } cout << endl; }...
10
CSE202: Lecture 16The Ohio State University10 Matrix Addition Algorithm Add two n x m matrices A, B; for each row i of A do –for each column j of A do °C[i][j] = A[i][j] + B[i][j]; Output matrices A, B and C.
11
CSE202: Lecture 16The Ohio State University11 matrixAdd.cpp... int main() { const int NUM_ROWS(3); // number of matrix rows const int NUM_COLS(2); // number of matrix columns // Note: A, B and C have the same dimensions double A[NUM_ROWS][NUM_COLS] = { {3, 3}, {1, 1}, {2, 0} }; double B[NUM_ROWS][NUM_COLS] = { {3, 0}, {3, 0}, {0, 1} }; double C[NUM_ROWS][NUM_COLS]; // C = A + B for (int i=0; i<NUM_ROWS; i++) { for (int j=0; j<NUM_COLS; j++) { C[i][j] = A[i][j] + B[i][j]; }...
12
CSE202: Lecture 16The Ohio State University12 matrixAdd.cpp (2)... // display A cout << "A =" << endl; for (int i=0; i<NUM_ROWS; i++) { for (int j=0; j<NUM_COLS; j++) { cout << " " << A[i][j] << " "; } cout << endl; } cout << endl;...
13
CSE202: Lecture 16The Ohio State University13 Distance Matrix Algorithm Compute a matrix D storing distances to a point (x,y); Input: x, y for each row i of D do –for each column j of D do °D[i][j] = distance from (x,y) to (i,j) Output matrix D.
14
CSE202: Lecture 16The Ohio State University14 distanceMatrix.cpp... // dimensions of distance matrix const int N_ROWS(8); const int N_COLS(12); double A[N_ROWS][N_COLS]; double x(0.0), y(0.0); cout << "Enter (x,y) coordinates of center: "; cin >> x >> y; for (int i=0; i<N_ROWS; i++) { for (int j=0; j<N_COLS; j++) { double dx = j-x; // Note: x represents columns double dy = i-y; // Note: y represents rows double dist = sqrt(dx*dx+dy*dy); A[i][j] = dist; }...
15
CSE202: Lecture 16The Ohio State University15 X Matrix Algorithm Compute a character matrix A representing an X; Input: h (height of the X) Initialize the first h columns in the first h rows of A to ‘-’; Set all elements of A on the diagonal from A[0][0] to A[h-1][h-1] to ‘X’; Set all elements of A on the diagonal from A[0][h-1] to A[h-1][0] to ‘X’.
16
CSE202: Lecture 16The Ohio State University16 X.cpp... // dimensions of matrix A const int N_ROWS(50); const int N_COLS(N_ROWS); char A[N_ROWS][N_COLS]; int height(0); cout << "Enter height of X: "; cin >> height; while (height > N_ROWS) { cout << "Input error. Input must be less than or equal to " << N_ROWS << ". " << endl; cout << "Enter height of X: "; cin >> height; }...
17
CSE202: Lecture 16The Ohio State University17 X.cpp (2) // initialize elements of A to '-' for (int i=0; i<height; i++) { for (int j=0; j<height; j++) { A[i][j] = '-'; } // draw diagonal \ in A for (int k=0; k<height; k++) { A[k][k] = 'X'; } // draw diagonal / in A for (int k=0; k<height; k++) { A[k][height-k-1] = 'X'; }
18
CSE202: Lecture 16The Ohio State University18 X.cpp... // dimensions of matrix A const int N_ROWS(50); const int N_COLS(N_ROWS); char A[N_ROWS][N_COLS]; int height(0.0); cout << "Enter height of X: "; cin >> height; while (height > N_ROWS) { cout << "Input error. Input must be less than or equal to " << N_ROWS << ". " << endl; cout << "Enter height of X: "; cin >> height; }...
19
CSE202: Lecture 16The Ohio State University19 Two Dimensional Arrays as Parameters
20
CSE202: Lecture 16The Ohio State University20 Matrix Addition Algorithm Add two n x m matrices A, B; for each row i of A do –for each column j of A do °C[i][j] = A[i][j] + B[i][j]; Output matrices A, B and C.
21
CSE202: Lecture 16The Ohio State University21 matrixAdd2.cpp... int main() { // Note: A, B and C have the same dimensions double A[NUM_ROWS][NUM_COLS] = { {3, 3}, {1, 1}, {2, 0} }; double B[NUM_ROWS][NUM_COLS] = { {3, 0}, {3, 0}, {0, 1} }; double C[NUM_ROWS][NUM_COLS]; add(A, B, C); cout << "A = " << endl; display(A); cout << "B = " << endl; display(B); cout << "C = " << endl; display(C); return 0; }...
22
CSE202: Lecture 16The Ohio State University22 matrixAdd2.cpp... // GLOBAL CONSTANTS const int NUM_ROWS(3); // number of matrix rows const int NUM_COLS(2); // number of matrix columns // Function prototypes void add(const double M1[NUM_ROWS][NUM_COLS], const double M2[NUM_ROWS][NUM_COLS], double M3[NUM_ROWS][NUM_COLS]); void display(const double M[NUM_ROWS][NUM_COLS]); int main() { // Note: A, B and C have the same dimensions // Matrices double A[NUM_ROWS][NUM_COLS] = { {3, 3}, {1, 1}, {2, 0} }; double B[NUM_ROWS][NUM_COLS] = { {3, 0}, {3, 0}, {0, 1} }; double C[NUM_ROWS][NUM_COLS];...
23
CSE202: Lecture 16The Ohio State University23 Function add() // Add two matrices // M1[][] = First matrix // M2[][] = Second matrix // M3[][] = M1[][] + M2[][] // Note: M1, M2 and M3 must have the same dimensions void add(const double M1[NUM_ROWS][NUM_COLS], const double M2[NUM_ROWS][NUM_COLS], double M3[NUM_ROWS][NUM_COLS]) { // M3 = M1 + M2 for (int i=0; i < NUM_ROWS; i++) { for (int j=0; j<NUM_COLS; j++) { M3[i][j] = M1[i][j] + M2[i][j]; }
24
CSE202: Lecture 16The Ohio State University24 Function display() // Output matrix // M[][] = Matrix void display(const double M[NUM_ROWS][NUM_COLS]) { for (int i=0; i < NUM_ROWS; i++) { for (int j=0; j < NUM_COLS; j++) { cout << " " << setw(3) << M[i][j]; } cout << endl; } cout << endl; }
25
CSE202: Lecture 16The Ohio State University25 matrixAdd2.cpp... int main() { // Note: A, B and C have the same dimensions double A[NUM_ROWS][NUM_COLS] = { {3, 3}, {1, 1}, {2, 0} }; double B[NUM_ROWS][NUM_COLS] = { {3, 0}, {3, 0}, {0, 1} }; double C[NUM_ROWS][NUM_COLS]; add(A, B, C); cout << "A = " << endl; display(A); cout << "B = " << endl; display(B); cout << "C = " << endl; display(C); return 0; }...
26
CSE202: Lecture 16The Ohio State University26 Passing 2D Arrays into Functions 2D arrays are always passed by reference. To declare a 2D array parameter, –Define global constants NUM_ROWS and NUM_COLS; –Parameter is: type array2D[NUM_ROWS][NUM_COLS] For instance, void f(int array2D[NUM_ROWS][NUM_COLS]) No way to declare a variable length 2D array parameter. For instance, void f(int array2D[][]) is illegal and will generate a syntax error.
27
CSE202: Lecture 16The Ohio State University27 Multi-Dimensional Arrays Arrays can have higher dimensions. Definitions, intialization, indexing, function parameters are similar to 2D arrays. Note multidimensional arrays can have quite a few elements so you must be mindful of your memory capacity: int big[1000][1000][1000]; //a billion ints
28
CSE202: Lecture 16The Ohio State University28 Common Programming Errors Addressing indices that are out of bounds of the array range. This will run-time crash or at the very least a logical error. Be careful especially when using expressions to compute the indices. –Remember, indexing starts with 0, not 1!!! Forgetting to declare the array (either altogether or forgetting the [] ) Assuming the array is initialized (to zero.)
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.