Two dimensional array in c


Declaration:

data type  name of array[row constant expression][col constant expression];
  ex:int a[4][5],score[N][M]
           a[i][j]
In the above example, the first index value i specifies a row index, whilst j specifies a column index.

  • The C language allows arrays of any type, including arrays of arrays.
  • With two-bracket pairs, we obtain a two-dimensional array. This idea can be iterated to obtain arrays of higher dimension.  
 
 

Declarations of arrays

Remarks

int a[50];

A one-dimensional array

int b[2][3];

A two-dimensional array

int c[2][3][2];

A three-dimensional array

 

 ex.     

1.Declaration:  int a[3][3];
2.logical structure:        
3.Assignment:
   for(i=0;i<3;i++)        or:int a[3][3]={{4,8,11},{5,1,6},{2,9,3}}
       for(j=0;j<3;j++)
              sacnf(“%d”,&a[i][j]);
 

storage: 

 
We can think of a[i][j] as the element in the ith row, jth column of the array(counting from 0).

 

Two Dimensional Array (simple matrix):

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>
int main() {

int array[3][3];
int row,column;

// Using nested loop to store values in a 2d array
for(row=0; row<3; row++) {
for(column=0; column<3; column++) {
array[row][column] = row+column;
}
}
// Using nested loop to display vlues of a 2d array
for(row=0; row<3; row++) {
for(column=0; column<3; column++) {
printf("%d\t",array[row][column]);
}
printf("\n");
}

return 0 ;
}



Output

0       1       2
1 2 3
2 3 4  

  

The sum of two matrix:

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<stdio.h>
int main() {
int i,j,numberOfRows,numberOfCols;
int A[10][10],B[10][10],C[10][10];

printf("Enter number of rows and cols:\n");
scanf("%d %d",&numberOfRows,&numberOfCols);
printf("\nA Matrix:\n");

//input of A matrix from the user
for(i=0; i<numberOfRows; i++) {
for(j=0; j<numberOfCols; j++) {
printf("A[%d][%d]= ",i,j);
scanf("%d",&A[i][j]);
}
printf("\n");
}
printf("\nB Matrix:\n");

//input of B matrix from the user
for(i=0; i<numberOfRows; i++) {
for(j=0; j<numberOfCols; j++) {
printf("B[%d][%d]= ",i,j);
scanf("%d",&B[i][j]);
}
printf("\n");
}

//print A matrix
printf("A = ");
for(i=0; i<numberOfRows; i++) {
printf("\t");
for(j=0; j<numberOfCols; j++) {
printf("%3d ",A[i][j]);
}
printf("\n");
}

//print B matrix
printf("\nB = ");
for(i=0; i<numberOfRows; i++) {
printf("\t");
for(j=0; j<numberOfCols; j++) {
printf("%3d ",B[i][j]);
}
printf("\n");
}
//Adding two matrices A and B and store the sum to the C matrix
for(i=0; i<numberOfRows; i++) {
for(j=0; j<numberOfCols; j++) {
C[i][j] = A[i][j] + B[i][j];
}

}

//print C matrix
printf("\nA + B = ");
for(i=0; i<numberOfRows; i++) {

for(j=0; j<numberOfCols; j++) {
printf("%3d ",C[i][j]);
}
printf("\n");
printf("\t");
}
}

 

Output

Enter number of rows and cols:
3 3

A Matrix:
A[0][0]= 7
A[0][1]= 8
A[0][2]= 2

A[1][0]= 3
A[1][1]= 11
A[1][2]= 9

A[2][0]= 1
A[2][1]= 12
A[2][2]= 4


B Matrix:
B[0][0]= 2
B[0][1]= 7
B[0][2]= 6

B[1][0]= 3
B[1][1]= 7
B[1][2]= 12

B[2][0]= 11
B[2][1]= 5
B[2][2]= 8

A = 7 8 2
3 11 9
1 12 4

B = 2 7 6
3 7 12
11 5 8

A + B = 9 15 8
6 18 21
12 17 12 


Next Post Previous Post
No Comment
Add Comment
comment url