A program for matrix addition using separate function to read, add and display.
A program for matrix addition using separate function to read, add and display.
Codes
/*A program to sort the data in ascending order using function*/
/*Programed by Jitendra Kr Yadav
10116
22 Mar,2011*/
#include<stdio.h>
#include<conio.h>
void read(int matrix[][20],int,int);
void add(int matrix1[][20],int matrix2[][20],int,int);
void display(int matrix[][20],int,int);
main()
{
int matrix1[20][20];
int matrix2[20][20];
int row1,row2,col1,col2;
printf(“Enter the dimension of matrix 1:\n”);
scanf(“%d%d”,&row1,&col1);
printf(“Enter the dimension of matrix 2:\n”);
scanf(“%d%d”,&row2,&col2);
read(matrix1,row1,col1);
read(matrix2,row2,col2);
add(matrix1,matrix2,row1,col1);
getch();
}
void read(int matrix[][20],int row,int col)
{
int i,j;
printf(“Enter the elements of matrix:\n”);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf(“%d”,&matrix[i][j]);
}
void add(int matrix1[][20],int matrix2[][20],int row,int col)
{
int matrix[20][20];
int i, j;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
matrix[i][j]=matrix1[i][j]+matrix2[i][j];
display(matrix,row,col);
}
void display(int matrix[][20],int row,int col)
{
int i,j;
printf(“The sum of two matrix is:”);
for(i=0;i<row;i++)
{
printf(“\n”);
for(j=0;j<col;j++)
printf(“%d\t”,matrix[i][j]);
}
printf(“\n\n”);
printf(“Press any key to continue…”);
}
Output
Enter the dimension of matrix 1:
2
2
Enter the dimension of matrix 2:
2
2
Enter the elements of matrix:
1
2
3
4
Enter the elements of matrix:
4
3
2
1
The sum of two matrix is:
5 5
5 5
Press any key to continue…