Write a Modular C Programming Code for Addition of a 2D Matrix?
CODE:
#include<stdio.h> #define ROW 10 #define COL 10 void ReadArray(int [ROW][COL], int, int); void DisplayArray(int [ROW][COL], int, int); void SumMatrix(int [ROW][COL], int [ROW][COL], int [ROW][COL], int, int); main() { int r,c,sum; int a[ROW][COL]; int b[ROW][COL]; int z[ROW][COL]; printf("Enter the order of 1st 2D matrix: "); scanf("%d%d",&r,&c); printf("\nEnter the Elements of 1st Matix:\n"); ReadArray(a,r,c); printf("Enter the order of 2nd 2D matrix: "); scanf("%d%d",&r,&c); printf("\nEnter the Elements of 2nd Matix:\n"); ReadArray(b,r,c); SumMatrix(a,b,z,r,c); printf("\nThe 1st Matrix is:\n"); DisplayArray(a,r,c); printf("\nThe 2nd Matrix is:\n"); DisplayArray(b,r,c); printf("\nThe Final Addition Matrix is\n"); DisplayArray(z,r,c); } void ReadArray(int a[ROW][COL], int r, int c) { int i,j; for(i=0;i<r;i++) { for(j=0;j<c;j++) { scanf("%d",&a[i][j]); } } } void DisplayArray(int a[ROW][COL], int r, int c) { int i,j; for(i=0;i<r;i++) { for(j=0;j<c;j++) { printf("\t%d",a[i][j]); } printf("\n"); } } void SumMatrix(int a[ROW][COL],int b[ROW][COL],int z[ROW][COL], int r, int c) { int i,j; for(i=0;i<r;i++) { for(j=0;j<c;j++) { z[i][j]=a[i][j]+b[i][j]; } } }
OUTPUT
Enter the order of 1st 2D matrix: 3 3 Enter the Elements of 1st Matix: 3 6 5 9 1 6 5 4 9 Enter the order of 2nd 2D matrix: 3 3 Enter the Elements of 2nd Matix: 3 6 2 5 4 9 2 1 7 The 1st Matrix is: 3 6 5 9 1 6 5 4 9 The 2nd Matrix is: 3 6 2 5 4 9 2 1 7 The Final Addition Matrix is 6 12 7 14 5 15 7 5 16 Process returned 0 (0x0) execution time : 24.845 s Press any key to continue.
Please find some more codes of 1D Arrays, 2D Arrays, Pointers on the below page:
Top 100+ C Programming codes – KLE Technological University