Presentation is loading. Please wait.

Presentation is loading. Please wait.

Two Dimensional Arrays Rohit Khokher

Similar presentations

Presentation on theme: "Two Dimensional Arrays Rohit Khokher"— Presentation transcript:

1 Two Dimensional Arrays Rohit Khokher

2 Two dimensional Arrays
A vector can be represented as an one dimensional array A matrix can be represented as a two dimensional array A[8][10] Columns Rows A[5][6] Array element

3 The value of sales of three items by four sales person
1 2 3 A[0][0] A[0][1] A[0][2] A[1][0] A[1][1] A[1][2] A[2][0] A[2][1] A[2][2] 4 A[3][0] A[3][1] A[3][2] Sales Persons

4 2-D array 2-Day Array Declaration #define ROWS 2 #define COLS 3
1 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});

5 Compile time initialization
1 int A[ROWS ][COLS]= {{0,0,0}, {1,1,1}}; int A[ ][COLS]= { {0,0,0}, {1,1,1} }; 1 2 int A[ROWS][COLS]= { {0,0}, {2} }; The missing values are initialized to zero automatically

6 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”); }

7 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

8 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]; }

9 Compute Column sum for (j=0; j<COLS; j++) { C[j]=0;
A[0][0] A[0][1] A[0][2] A[0][3] A[1][0] A[1][1] A[1][2] A[1][3] A[2][0] A[2][1] A[2][2] A[2][3] A[3][0] A[3][1] A[3][2] A[3][3] 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] C[0]= A[0][0] + A[1][0] + A[2][0] + A[3][0] C[1]= A[0][1] + A[1][1] + A[2][1] + A[3][1] C[2]= A[0][2] + A[1][2] + A[2][2] + A[3][2] C[3]= A[0][3] + A[1][3] + A[2][3] + A[3][3]

10 Compute diagonal sum D =0; for (j=0; j<COLS; j++) D= D+ A[j][j]; OR
for (i=0; i<ROWS; i++) D=D+A[i][i]; D= A[0][0] + A[1][1] + A[2][2] + A[3][3]

11 Compute second diagonal sum
for (i=0; i<ROWS; i++) D= D+ A[i][COLS-i]; D= A[0][3] + A[1][2] + A[2][1] + A[3][0]

12 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: i-1,j-1 i-1,j i-1, j+1 I,j-1 i,j i,j+1 i+1,j-1 i+1,j i+1,j+1

13 Add two matrices for (i=0; i<ROWS; i++) for (j=0; j<COLS; j++)

14 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

15 Practice problem To verify the correctness the matrix multiplication algorithm described before compute:

16 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 Write a program that reads a 2D array and determines whether the array is a magic square or not.

17 Project Read about the tic-tac-toe game at . Find the simplest algorithm that can be implemented in C using 1D and 2D arrays.

Download ppt "Two Dimensional Arrays Rohit Khokher"

Similar presentations

Ads by Google