include <stdio.h>
int main(){
int number [5][5] , row=0, col=0,sumrow[5], sumcol[5];
for(row=0;row<5;row++)
{
printf("Enter row %d ", row+1);
for(col=0;col<5;col++)
scanf("%d",&number[row][col]);
}
// I want to read in row and col of numbers
for(row=0;row<5,sumrow[row]=sumcol[col]=0;row++);
for(row=0;row<5;row++)
for(col=0;col<5;col++)
{
sumrow[row]=sumrow[row]+number[row][col];
sumcol[col]=sumrow[col]+number[row][col];
}
// here I want to plus all the row in sumrow and col in sumcol
printf("Row totals ");
for(row=0;row<5;row++) {
printf("%d ",sumrow[row]);
}
printf("\n Row totals ");
for(col=0;col<5;col++){
printf("%d ",sumrow[col]);
}
return 0;
}
此代码无效。我想读取矩阵和所有行和列。 我不知道问题是什么。
答案 0 :(得分:1)
如果你想添加行和列的所有元素或者下面所有行和列的总和是代码,
#include <stdio.h>
int main ()
{
static int array[10][10];
int i, j, m, n, sum = 0;
int rowSum = 0;
int colSum = 0;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
printf("Enter the elements of the matrix\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}
printf("Sum of the %d row is = %d\n", i, sum);
rowSum += sum;
sum = 0;
}
printf("Sum of all the row is = %d\n", rowSum);
sum = 0;
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
sum = sum + array[i][j];
}
printf("Sum of the %d column is = %d\n", j, sum);
colSum += sum;
sum = 0;
}
printf("Sum of all the column is = %d\n", colSum);
}
答案 1 :(得分:0)
我不知道我是否理解正确,但如果你想要总结矩阵的所有元素,你应该使用它,例如:
int a[5][5],n,m,i,j, soma=0;
printf("Enter value of m and n: ");
scanf("%d%d",&m,&n);
printf("\nEnter elemets of the matrix:\n");
for(i=0;i<m;++i)
for(j=0;j<n;++j)
scanf("%d",&a[i][j]);
for(i=0;i<m;++i){
sum_col += i;
}
for(j=0;j<n;++j){
sum_row += ;
}
printf("\nYour sum is: %d", soma);
getch();
return 0;
PS:我也使用了conio库,所以你应该在开头使用#include<conio.h>
但是如果你想把麻木的行和麻木的麻木相加并逐一打印,你应该使用这个:
int number [5][5] , row=0, col=0,sumrow=0, sumcol=0, cont_1=0, cont_2=0;
for(row=0;row<5;row++)
{
printf("Enter row %d ", row+1);
for(col=0;col<5;col++)
scanf("%d",&number[row][col]);
}
// I want to read in row and col of numbers
for(row=0;row<5;row++){
cont_1++;
for(col=0;col<5;col++)
{
sumcol += number[row][col];
}
printf("\nThe sum of Col %d is %d", cont_1, sumcol);
sumcol = 0;
}
for(col=0;col<5;col++){
cont_2++;
for(row=0;row<5;row++)
{
sumrow += number[row][col];
}
printf("\nThe sum of Row %d is %d", cont_2, sumrow);
sumrow = 0;
}
return 0;
我只是让你的代码更清晰,所以你应该明白你在做什么。