Sunday, 30 August 2015

Write a C function using pointers to add two matrices and to return the resultant matrix to the calling function

Write a C function using pointers to add two matrices and to return the resultant matrix to the calling function
1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  int a[5][5],b[5][5],row,col;  
4:  void add(int(*)[5]);  
5:  int main()  
6:  {  
7:        int c[5][5],i,j;  
8:        clrscr();  
9:        printf("Enter row : ");  
10:        scanf("%d",&row);  
11:        printf("Enter column : ");  
12:        scanf("%d",&col);  
13:        printf("Enter matrix A :\n");  
14:        for(i=0;i<row;i++)  
15:        {  
16:              for(j=0;j<col;j++)  
17:              {  
18:                    scanf("%d",&a[i][j]);  
19:              }  
20:        }  
21:        printf("Enter matrix B :\n");  
22:        for(i=0;i<row;i++)  
23:        {  
24:              for(j=0;j<col;j++)  
25:              {  
26:                    scanf("%d",&b[i][j]);  
27:              }  
28:        }  
29:        add(c);  
30:        printf("Addition :\n");  
31:        for(i=0;i<row;i++)  
32:        {  
33:              for(j=0;j<col;j++)  
34:              {  
35:                    printf("%d\t",c[i][j]);  
36:              }  
37:              printf("\n");  
38:        }  
39:        getch();  
40:        return 0;  
41:  }  
42:  void add(int c[5][5])  
43:  {  
44:        int i,j;  
45:        for(i=0;i<row;i++)  
46:        {  
47:              for(j=0;j<col;j++)  
48:              {  
49:                    c[i][j]=a[i][j]+b[i][j];  
50:              }  
51:        }  
52:  }  

4 comments: