gpt4 book ai didi

计算矩阵中的行总和

转载 作者:太空宇宙 更新时间:2023-11-04 02:45:11 26 4
gpt4 key购买 nike

行总和部分无法正常工作。有什么建议么?另外,如果主对角线是 i==j,对角线是什么?我该如何定义它?

int main (void) {
int A[5][5];
int B[5];
int x=0,sum=0;
int n,m,i=0,j;

printf("Enter rows and columns : \n");
scanf("%d %d",&n,&m);
printf("Enter matrix : \n");

for (i = 0 ; i < n ; i++) {
for (j = 0 ; j < m ; j++) {
scanf("%d",&A[i][j]);
}
}

/* Sum of rows Problem */
for(i = 0 ; i < n ; i++) {
B[i] = 0;
for(j = 0 ; j < m ; j++) {
B[i] = B[i] + A[i][j];
++i;
}
}
for(i = 0 ; i < n ; i++) {
for(j = 0 ; j < m ; j++) {
printf("The sum of rows %d \n", B[j]);
}
}
return 0;
}

最佳答案

实际上,你只需要去掉内循环中的++i,程序就可以正常运行了。

代码:

int main (void) {
int A[5][5];
int B[5];
int x=0,sum=0;
int n,m,i=0,j;

printf("Enter rows and columns : \n");
scanf("%d %d",&n,&m);
printf("Enter matrix : \n");

for (i = 0 ; i < n ; i++) {
for (j = 0 ; j < m ; j++) {
scanf("%d",&A[i][j]);
}
}

/* Sum of rows Problem */
for(i = 0 ; i < n ; i++) {
B[i] = 0;
for(j = 0 ; j < m ; j++) {
B[i] = B[i] + A[i][j]; //Removed the stray ++i from here.
}
}
for(i = 0 ; i < n ; i++)
{
printf("The sum of row %d is %d \n",i+1,B[i]);
}
return 0;
}

回答你的第二个问题,对角线i == size - j- 1 如果size是数组的大小.

关于计算矩阵中的行总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28183290/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com