Q. Write a program to accept an mXn matrix and display an m+1 X n+1 matrix such that the m+1th row contains the sum of all elements of corresponding row and the n+1th column contains the sum of elements of the corresponding column.
A
1 2 3
4 5 6
7 8 9
B
1 2 3 6
4 5 6 15
7 8 9 24
12 15 18 45
Program
#include <stdio.h>
int main()
{
int i, j, m, n,p,q,t=0;
int matrix[10][20],x[10],y[10];
int sumR, sumC;
printf("Enter number of rows : ");
scanf("%d", &m);
printf("Enter number of columns : ");
scanf("%d", &n);
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("Enter data in [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
printf("\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
printf("\n");
p=1;
for (i = 0; i < m; i++)
{
sumR = 0;
for (j = 0; j < n; j++)
{
sumR += matrix[i][j];
}
x[p]=sumR;
p++;
}
printf("\n");
q=1;
for (i = 0; i < n; i++)
{
sumC = 0;
for (j = 0; j < m; j++)
{
sumC += matrix[j][i];
}
y[q]=sumC;
q++;
}
p=1;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("%d",x[p]);
p++;
printf("\n");
}
for(i=1;i<q;i++)
{
t=t+y[i];
printf("%d\t",y[i]);
}
printf("%d\t",t);
return 0;
}
Output:
Enter number of rows : 3
Enter number of columns : 3
Enter data in [0][0]: 1
Enter data in [0][1]: 2
Enter data in [0][2]: 3
Enter data in [1][0]: 4
Enter data in [1][1]: 5
Enter data in [1][2]: 6
Enter data in [2][0]: 7
Enter data in [2][1]: 8
Enter data in [2][2]: 9
1 2 3
4 5 6
7 8 9
1 2 3 6
4 5 6 15
7 8 9 24
12 15 18 45
A
1 2 3
4 5 6
7 8 9
B
1 2 3 6
4 5 6 15
7 8 9 24
12 15 18 45
Program
#include <stdio.h>
int main()
{
int i, j, m, n,p,q,t=0;
int matrix[10][20],x[10],y[10];
int sumR, sumC;
printf("Enter number of rows : ");
scanf("%d", &m);
printf("Enter number of columns : ");
scanf("%d", &n);
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("Enter data in [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
printf("\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
printf("\n");
p=1;
for (i = 0; i < m; i++)
{
sumR = 0;
for (j = 0; j < n; j++)
{
sumR += matrix[i][j];
}
x[p]=sumR;
p++;
}
printf("\n");
q=1;
for (i = 0; i < n; i++)
{
sumC = 0;
for (j = 0; j < m; j++)
{
sumC += matrix[j][i];
}
y[q]=sumC;
q++;
}
p=1;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("%d",x[p]);
p++;
printf("\n");
}
for(i=1;i<q;i++)
{
t=t+y[i];
printf("%d\t",y[i]);
}
printf("%d\t",t);
return 0;
}
Output:
Enter number of rows : 3
Enter number of columns : 3
Enter data in [0][0]: 1
Enter data in [0][1]: 2
Enter data in [0][2]: 3
Enter data in [1][0]: 4
Enter data in [1][1]: 5
Enter data in [1][2]: 6
Enter data in [2][0]: 7
Enter data in [2][1]: 8
Enter data in [2][2]: 9
1 2 3
4 5 6
7 8 9
1 2 3 6
4 5 6 15
7 8 9 24
12 15 18 45
0 Comments