Thursday, November 27, 2014

C Program: Function sub program for the matrix operation

/*.........................................................................................................................................
File : rec16
Program: Function sub program for the matrix operation
Author : sujithbaby440@gmail.com
Date :15/11/2014
..........................................................................................................................................*/
#include<stdio.h>
void ReadMat(int M,int N,int x[5][5]);
void PrintMat(int M,int N,int x[5][5]);
void AddMat(int M,int n,int A[5][5],int B[5][5],int C[5][5]);
main()
{
int m,n,a[5][5],b[5][5],c[5][5];
system("clear");
printf("Enter the number of rows and columns for the matrix\n");
scanf("%d%d",&m,&n);
printf("\n1st matrix\n");
ReadMat(m,n,a);
PrintMat(m,n,a);
printf("\n2nd matrix\n");
ReadMat(m,n,b);
PrintMat(m,n,b);
AddMat(m,n,a,b,c);
printf("\nThe sum of the matrix\n");
PrintMat(m,n,c);
}
void ReadMat(int M,int N,int X[5][5])
{
int i,j;
printf("Enter values into matrix\n");
for(i=1;i<=M;i++){
for(j=1;j<=N;j++){
scanf("%d",&X[i][j]);
}
}
}
void PrintMat(int M,int N,int X[5][5])
{
int i,j;
printf("\nMatrix:\n");
for(i=1;i<=M;i++){
for(j=1;j<=N;j++){
printf("%d ",X[i][j]);
}
printf("\n");
}
}
void AddMat(int M,int N,int A[5][5],int B[5][5],int C[5][5])
{
int i,j;
for(i=1;i<=M;i++){
for(j=1;j<=N;j++){
C[i][j]=A[i][j]+B[i][j];
}
}
}


OUTPUT 1
Enter the number of rows and columns for the matrix
3 3
1st matrix
Enter values into matrix
1 2 8
4 5 6
7 9 9
Matrix:
1 2 8
4 5 6
7 9 9
2nd matrix
Enter values into matrix
2 1 2
4 5 6
1 1 1
Matrix:
2 1 2
4 5 6
1 1 1
The sum of the matrix
Matrix:
3 3 10
8 10 12
8 10 10
[141740@localhost ~]$


OUTPUT 2
Enter the number of rows and columns for the matrix
3 3
1st matrix
Enter values into matrix
1 1 1
1 1 1
2 2 2
Matrix:
1 1 1
1 1 1
2 2 2
2nd matrix
Enter values into matrix
4 4 4
5 5 5
1 1 1
Matrix:
4 4 4
5 5 5
1 1 1
The sum of the matrix
Matrix:
5 5 5
6 6 6
3 3 3
[141740@localhost ~]$

No comments:

Post a Comment