190 likes | 381 Views
Two Dimensional Arrays Rohit Khokher. Two dimensional Arrays. A[8][10]. A vector can be represented as an one dimensional array A matrix can be represented as a two dimensional array. Columns. Rows. A[5][6]. Array element. The value of sales of three items by four sales person. Items.
E N D
Two dimensional Arrays A[8][10] • A vector can be represented as an one dimensional array • A matrix can be represented as a two dimensional array Columns Rows A[5][6] Array element
The value of sales of three items by four sales person Items Sales Persons
2-D array 2-Day Array Declaration #define ROWS 2 #define COLS 3 float A[ROWS ][COLS]; Compile time Initialization int A[ROWS ][COLS]= {0,0,0,1,1,1}; or int A[ROWS ][COLS]= ({0,0,0},{1,1,1});
Compile time initialization int A[ROWS ][COLS]= {{0,0,0}, {1,1,1}}; int A[ ][COLS]= { {0,0,0}, {1,1,1} }; int A[ROWS][COLS]= { {0,0}, {2} }; The missing values are initialized to zero automatically
Runtime Initialization for (i=0; i<ROWS; i++) for (j=0; i<COLS; j++) a[i][j]=0; Reading the elements of 2-D array Read data row-wise for (i=0; i<ROWS; i++) for (j=0; j<COLS; j++) scanf (“%d”, &a[i][j]); Priniting the elements of 2-D array for (i=0; i<ROWS; i++) { Print a row for (j=0; j<COLS; j++) printf (“%d”, &a[i][j]); Change line printf (“\n”); }
Matrix manipulation • Read a matrix A (4,4) of real numbers and compute: • sum of each row entries • sum of each column entries • sum of the main diagonal entries • sum of the secondary diagonal entries
Compute row sum for (i=0; i<ROWS; i++) { R[i]=0; Initialize R[i] for (j=0; j<COLS; j++) R[i]=R[i]+A[i][j];}
Compute Column sum for (j=0; j<COLS; j++) { C[j]=0; for (i=0; i<ROWS; i++) C[j]=C[j]+A[i][j]; } Initialize C[j]
Compute diagonal sum D =0; for (j=0; j<COLS; j++) D= D+ A[j][j]; OR D =0; for (i=0; i<ROWS; i++) D=D+A[i][i];
Compute second diagonal sum D =0; for (i=0; i<ROWS; i++) D= D+ A[i][COLS-i];
Practice question • Write a C program to find the largest element of each row of a 2D array. • Write a C program to find the row and column numbers of the smallest element of a 2D array and count the sum of its neighbors. In a 2D array an element A[i][j] may have up to eight neighbors defined as:
Add two matrices for (i=0; i<ROWS; i++) for (j=0; j<COLS; j++) C[i][j]=A[i][j]+B[i][j];
Multiplication of two matrices c11=a11 x b11 + a12 x b21 c12=a11 x b12 + a12 x b22 c13=a11 x b13 + a12 x b23 for (i=0; i<ROWSA; i++) c21=a21 x b11 + a22 x b21 for (j=0; j<COLSB; j++) c22=a21 x b12 + a22 x b22 c23=a21 x b13 + a22 x b23 { C[i][j]=0; c31=a31 x b11 + a32 x b21 for (k=0; j<COLSA; j++) C[i][j]= C[i][j]+A[i][k]+B[k][j]; } c22=a31 x b12 + a32 x b22 c33=a31 x b13 + a32 x b23
Practice problem To verify the correctness the matrix multiplication algorithm described before compute:
Practice problem • A square 2D array is called a magic square if sums of each row, each column, and both the diagonals of the array are equal. A 3 x 3 magic square is shown below. Read more about magic square at http://en.wikipedia.org/wiki/Magic_square Write a program that reads a 2D array and determines whether the array is a magic square or not.
Project • Read about the tic-tac-toe game at http://en.wikipedia.org/wiki/Tic-tac-toe . Find the simplest algorithm that can be implemented in C using 1D and 2D arrays.