阅读量:120
在C语言中,可以通过嵌套循环来遍历二维数组,并对每行和每列进行求和操作。以下是一个示例代码来实现二维数组行列求和:
#include
int main() {
int rows, cols;
printf("Enter the number of rows and columns of the array: ");
scanf("%d %d", &rows, &cols);
int arr[rows][cols];
printf("Enter the elements of the array:\n");
// Input elements of the array
for(int i = 0; i < rows class="hljs-keyword">for(int j = 0; j < cols class="hljs-built_in">scanf("%d", &arr[i][j]);
}
}
// Calculate row sums
printf("Row sums:\n");
for(int i = 0; i < rows class="hljs-type">int sum = 0;
for(int j = 0; j < cols xss=removed class="hljs-built_in">printf("Row %d sum: %d\n", i+1, sum);
}
// Calculate column sums
printf("Column sums:\n");
for(int j = 0; j < cols class="hljs-type">int sum = 0;
for(int i = 0; i < rows xss=removed class="hljs-built_in">printf("Column %d sum: %d\n", j+1, sum);
}
return 0;
}
在上面的代码中,首先用户输入二维数组的行数和列数,然后输入数组的元素。接着分别计算每行和每列的和,并输出结果。