在C中添加或减去负数

时间:2015-11-03 13:28:01

标签: c arrays int

所以我一直在尝试做一个在数组中添加行的整数的代码(例如,这个整数可能是负数)

所以你会有这样的事情:

input:
3 2 (Number of rows and columns)
12 7
-3 6
-2 -5

output:
19
3 
-7

我所做的是:

#include <stdio.h>

int main ()
{
static int array[10][10];
int i, j, m, n, sum = 0;

printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
printf("Enter the co-efficients 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("%d\n",sum);
    sum = 0;
}
}

我无法找到一种方法来添加&#34;负数,我应该更改/添加到此代码?再次感谢:)

1 个答案:

答案 0 :(得分:0)

#include <stdio.h>

int main (){
    int array[10][10];
    int i, j, m, n, sum = 0;

    printf("Enter the order of the matrix\n");
    scanf("%d %d", &m, &n);

    printf("Enter the co-efficients 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("%d\n",sum);
        sum = 0;
     }
    return 0;
}