C LAB‎ > ‎(Sem1) Introduction to C‎ > ‎

WAP to add elements of two 3x3 matrices.

posted Oct 30, 2010, 1:57 AM by Neil Mathew   [ updated Oct 30, 2010, 2:21 AM ]

SOURCE CODE:

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
#include<stdio.h>
 
int main()
{
 
int A[3][3], B[3][3];
int i,j;
 
//INPUT of MATRIX/ 2D ARRAY A
printf("\n Enter the elements of matrix A: \n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf(" %d ", &A[i][j]);
}
}
 
//INPUT of MATRIX/ 2D ARRAY B
printf("\n Enter the elements of matrix B: \n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf(" %d ", &B[i][j]);
}
}
 
//ADDING THE TWO MATRICES
printf("\n Adding matrices A and B = matrix C ");
int C[3][3];
 
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
C[i][j]=A[i][j]+B[i][j];
}
}
 
//OUTPUT
printf("\n Matrix C is: \n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf(" %d ", C[i][j]);
}
printf("\n");
}
 
return 0;
}
 

OUTPUT:

 Enter the elements of matrix A: 
1 2 3
1 2 3
3 2 1

 Enter the elements of matrix B: 
3 2 1
3 2 1
1 2 3

 Adding matrices A and B = matrix C 
 Matrix C is: 
 4  4  4 
 4  4  4 
 4  4  4 


Comments