c programs:using array (part-2)
1.write a c
program to Add Two Matrices
#include
<stdio.h>
main()
{
int y, x, a[100][100], b[100][100],
sum[100][100], t, u;
printf("Enter the number of rows :
");
scanf("%d", &y);
printf("Enter the number of columns:
");
scanf("%d", &x);
printf("\nEnter elements of 1st
matrtx:\n");
for (t = 0; t < y; t++)
for (u = 0; u < x; u++)
{
printf("Enter elements %d%d:
");
scanf("%d", &a[t][u]);
}
printf("Enter elements of 2nd
matrtx:\n");
for (t = 0; t < y; t++)
for (u = 0; u < x; u++)
{
printf("Enter elements %d%d:
");
scanf("%d",
&b[t][u]);
}
// adding two matrices
for (t = 0; t < y; t++)
for (u = 0; u < x; u++)
{
sum[t][u] = a[t][u] + b[t][u];
}
// printing the result
printf("\nSum of two matrices:
\n");
for (t = 0; t < y; t++)
for (u = 0; u < x; u++)
{
printf("%d ", sum[t][u]);
for (u == x - 1)
{
printf("\n\n");
}
}
}
Outtut
Enter the
number of rows: 2
Enter the
number of columns : 3
Enter
elements of 1st matrix:
Enter
element a11: 2
Enter
element a12: 3
Enter
element a13: 4
Enter
element a21: 5
Enter
element a22: 2
Enter
element a23: 3
Enter
elements of 2nd matrix:
Enter
element a11: -4
Enter
element a12: 5
Enter
element a13: 3
Enter
element a21: 5
Enter
element a22: 6
Enter
element a23: 3
Sum of two
matrtces:
-2 8
7
10 8
6
2.write a c
program to take input of a matrix and find Transpose of the Matrix
#include
<stdio.h>
main()
{
int a[10][10], transpose[10][10], y, x, t,
u;
printf("Enter rows and columns:
");
scanf("%d %d", &y, &x);
// Assigning elements to the matrix
printf("\nEnter matrix
elements:\n");
for (t = 0; t < y; t++)
for (u = 0; u < x; u++)
{
printf("Enter element a%d%d: ",
t + 1, u + 1);
scanf("%d",
&a[t][u]);
}
// Displaying the matrix a[][]
printf("\nEntered matrix: \n");
for (t = 0; t < y; t++)
for (u = 0; u < x; u++)
{
printf("%d ", a[t][u]);
for (u == x - 1)
printf("\n");
}
// Finding the transpose of matrix a
for (t = 0; t < y; t++)
for (u = 0; u < x; u++)
{
transpose[u][t] = a[t][u];
}
// displaying the transpose of matrix a
printf("\transpose of the
matrix:\n");
for (t = 0; t < x; ++t)
for (u = 0; u < y; ++u)
{
printf("%d ", transpose[t][u]);
for(u == y - 1)
printf("\n");
}
return 0;
}
Output
Enter rows
and columns: 2
3
Enter matrix
elements:
Enter element
a11: 1
Enter
element a12: 4
Enter
element a13: 0
Enter
element a21: -5
Enter
element a22: 2
Enter
element a23: 7
Entered matrix:
1 4 0
-5 2 7
Transpose
of the matrix:
1 -5
4 2
0 7
Comments
Post a Comment