Arithmetic

C语言-二维数组每行求和

PineappleCat · 5月3日 · 2019年 1773次已读

题目描述
定义一个二维数组(数组元素为整数),两个维度均不超过20。
从键盘输入m,n的值分别作为二维数组实际存储数据的维度,使用循环从键盘给二维数组赋值。
最后输出二维数组每行元素的和
输入
输入数据:
第一行为数组的两个维度m,n。
第二行为数组的元素(m*n个)。
输出
每行元素的和,所有结果输出在一行上,两个数据之间有空格,最后一个数据的后面没有空格。
样例输入
2 3
-1 -1 3 4 5 6
样例输出
1 15

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a[21][21];
    int m,n,sum=0;
      int i,j;
    scanf("%d %d",&m,&n);
    for(i=0;i<m;i++){
            sum=0;
        for(j=0;j<n;j++){
            scanf("%d",&a[i][j]);
            sum = sum+a[i][j];
        }
        printf("%d ",sum);
    }
    return 0;
}

Click here to view the copyright notice of this site(点击此处查看本站版权声明)
0 条回应

必须 注册 为本站用户, 登录 后才可以发表评论!