#include <stdio.h>
#include <conio.h>
void main()
{
int A[10][10], B[10][10], sumat[10][10], diffmat[10][10];
int i, j, M,N, option;
void trace (int arr[][10], int M, int N);
clrscr();
printf(“Enter the order of the matrix A and B\n”);
scanf(“%d %d”, &M, &N);
printf(“Enter the elements of matrix A\n”);
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
scanf(“%d”,&A[i][j]);
}
}
printf(“MATRIX A is\n”);
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf(“%3d”,A[i][j]);
}
printf(“\n”);
}
printf(“Enter the elements of matrix B\n”);
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
scanf(“%d”,&B[i][j]);
}
}
printf(“MATRIX B is\n”);
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf(“%3d”,B[i][j]);
}
printf(“\n”);
}
printf(“Enter your option: 1 for Addition and 2 for Subtraction\n”);
scanf(“%d”,&option);
switch (option)
{
case 1: for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
sumat[i][j] = A[i][j] + B[i][j];
}
}
printf(“Sum matrix is\n”);
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf(“%3d”,sumat[i][j]) ;
}
printf(“\n”);
}
trace (sumat, M, N);
break;
case 2:for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
diffmat[i][j] = A[i][j] – B[i][j];
}
}
printf(“Difference matrix is\n”);
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf(“%3d”,diffmat[i][j]) ;
}
printf(“\n”);
}
trace (diffmat, M, N);
break;
}
} /* End of main() */
/*Function to find the trace of a given matrix and print it*/
void trace (int arr[][10], int M, int N)
{
int i, j, trace = 0;
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
if (i==j)
{
trace = trace + arr[i][j];
}
}
}
printf (“Trace of the resultant matrix is = %d\n”, trace);
}
/*———————————–
Enter the order of the matrix A and B
2 2
Enter the elements of matrix A
1 1
2 2
MATRIX A is
1 1
2 2
Enter the elements of matrix B
3 3
4 4
MATRIX B is
3 3
4 4
Enter your option: 1 for Addition and 2 for Subtraction
1
Sum matrix is
4 4
6 6
Trace of the resultant matrix is = 10
———————————————*/
Not Satisfied ? Just search & get the result
Related posts:
- Sample – Program to find the sum of each row & column of a matrix of size n x m and if matrix is square, find the sum of the diagonals also..
- C program to read N integers (zero, +ve and -ve) into an array A and to a) Find the sum of negative numbers b) Find the sum of positive numbers and c) Find the average of all input numbers Output the various results computed with proper headings
- Sample – Program to find the sum of either of the diagonals of a 4 x 4 matrix.
- C program to simulate a simple calculator to perform arithmetic operations like addition, subtraction,multiplication and division only on integers
- C program to find the sum of odd numbers and sum of even numbers from 1 to N. Output the computed sums on two different lines with suitable headings
