要求二维数组每行的和,可以使用双重循环来遍历每一行并求和。以下是一个示例代码:
#include <stdio.h>
int main() {
int rows, cols;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
int arr[rows][cols];
// Input values into the array
printf("Enter the elements of the array:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &arr[i][j]);
}
}
// Calculate the sum of each row
for (int i = 0; i < rows; i++) {
int sum = 0;
for (int j = 0; j < cols; j++) {
sum += arr[i][j];
}
printf("Sum of row %d: %d\n", i+1, sum);
}
return 0;
}
在这个示例中,我们首先输入二维数组的行数和列数,然后输入数组的元素。接着使用双重循环遍历每一行并计算每行的和,最后输出每行的和。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/895609.html