在此示例中,您了解如何编写C程序以在矩阵中查找每行的总和?我们将在C中编写一个程序,以找到二维数组中的行和示例。在这里,我们将以二维数组读取矩阵并打印每行的所有元素的总和。 Let see an example,
#include<stdio.h>
int main()
{
int i,j,sum=0,rows,cols;
printf("Enter Number of rows:");
scanf("%d",&rows);
printf("Enter Number of cols:");
scanf("%d",&cols);
int a[rows][cols];
printf("Enter matrix elements:");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
scanf("%d",&a[i][j]);
}
printf("Matrix elements are:\n");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
printf("%d ",a[i][j]);
printf("\n");
}
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
sum=sum+a[i][j];
}
printf("\n Sum of %dth row is %d",i,sum);
sum=0;
}
return 0;
}
Enter Number of rows:2
Enter Number of cols:2
Matrix elements are:
4 6
2 4
Sum of 0th row is 10
Sum of 1th row is 6
快乐编码ðÿ~š
没意见:
如果您有任何疑问,请告诉我