【发布时间】:2015-11-03 13:28:00
【问题描述】:
所以我一直在尝试编写一个代码,将数组中的一行的整数相加(例如,这个整数可以是负数)
所以你会有这样的东西:
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;
}
}
我找不到“添加”负数的方法,我应该在此代码中更改/添加什么?再次感谢:)
【问题讨论】:
-
+运算符有什么问题? -
负整数的处理方式与使用
+一样。例如a + b == 3其中a == 6和b == -3 -
您的程序运行正常:-3+6=3 和 -2+(-5)=-7,或者您是否期待其他结果?
-
我相信显示的输出是所需的输出。 OP - 你的程序给出了什么输出?
-
好吧,我认为自己是个白痴,我使用了一个 %d 并首先打印了 i 而不是总和,所以这就是为什么我没有得到想要的结果。再次感谢大家。