Calculate the Addition or Subtraction & Trace of 2 Matrices
#include
#include
void trace(int arr[][10], int m, int n);
void main()
{
int
array1[10][10], array2[10][10], arraysum[10][10],
arraydiff[10][10];
int
i, j, m, n, option;
clrscr();
printf("Enter the order of the matrix array1 and array2 \n");
scanf("%d %d", &m, &n);
printf("Enter the elements of matrix array1 \n");
for
(i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &array1[i][j]);
}
}
printf("matrix array1 is \n");
for
(i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%3d", array1[i][j]);
}
printf("\n");
}
printf("Enter the elements of matrix array2 \n");
for
(i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &array2[i][j]);
}
}
printf("matrix array2 is \n");
for
(i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%3d", array2[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++)
{
arraysum[i][j] = array1[i][j] + array2[i][j];
}
}
printf("Sum matrix is \n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%3d", arraysum[i][j]) ;
}
printf("\n");
}
trace (arraysum, m, n);
break;
case
2:
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
arraydiff[i][j] = array1[i][j] - array2[i][j];
}
}
printf("Difference matrix is \n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%3d", arraydiff[i][j]) ;
}
printf("\n");
}
trace (arraydiff, m, n);
break;
}
}
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);
getch();
}
Output
Enter the order of the matrix array1 and
array2
3 3
Enter the elements of matrix array1
2 3 4
7 8 9
5 6 8
matrix array1 is
2 3 4
7 8 9
5 6 8
Enter the elements of matrix array2
3 3 3
3 4 6
8 4 7
matrix array2 is
3 3 3
3 4 6
8 4 7
Enter your option: 1 for Addition and 2 for
Subtraction
1
Sum matrix is
5 6 7
10 12
15
13 10
15
Trace of the resultant matrix is = 32
Enter the order of the matrix array1 and
array2
3 3
Enter the elements of matrix array1
10 20 30
15 18 20
12 14 16
matrix array1 is
10 20
30
15 18
20
12 14
16
Enter the elements of matrix array2
1 5 9
10 15 14
9 12 13
MATRIX array2 is
1 5 9
10 15
14
9 12
13
Enter your option: 1 for Addition and 2 for
Subtraction
2
Difference matrix is
9 15
21
5 3 6
3 2 3
Trace of the resultant matrix is = 15
Find the Transpose of a given Matrix
#include
#include
void main()
{
static int array[10][10];
int i, j, m, n;
printf("Enter the order of the matrix \n");
scanf("%d %d", &m, &n);
printf("Enter the coefiicients of the matrix\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
printf("The given matrix is \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array[i][j]);
}
printf("\n");
}
printf("Transpose of matrix is \n");
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
printf(" %d", array[i][j]);
}
printf("\n");
}
getch();
}
Output
Enter the order of the matrix
3 3
Enter the coefiicients of the matrix
3 7 9
2 7 5
6 3 4
The given matrix is
3 7 9
2 7 5
6 3 4
Transpose of matrix is
3 2 6
7 7 3
9 5 4
No comments:
Post a Comment