繁体   English   中英

负整数和正整数之和导致意外输出 - C++

[英]Sum of negative and positive integers leads to unexpected output - c++

我有一个二维数组,愿意检查(每列)的总和是否为 0。 此外,从数组中添加整数似乎会导致意外的输出。

#include<bits/stdc++.h>
using namespace std;
int main() {
long long int lines;
int temp;
int is_0 = 1;
cin>>lines;
int numbers[3][lines];
for(int i = 0; i < 3; i++){
    for(int j = 0; j < lines; j++){
        cin>>temp;
        numbers[i][j]=temp;
    }
}

for(int i = 0; i<3; i++){
    temp = 0;
    for(int j = 0; j<lines;j++){
        temp += numbers[i][j];          
    }
    cout<<temp<<endl;
}
return 0;
}

示例输入:

2
1
1
1
-1
-1
-1

示例输出:

2
0
-2

有任何想法吗?

该程序按预期工作。 您正在创建以下矩阵

 1   1
 1  -1
-1  -1

和这里

for(int i = 0; i<3; i++){
    temp = 0;
    for(int j = 0; j<lines;j++){
        temp += numbers[i][j];          
    }
    cout<<temp<<endl;
}

您正在总结每一行的数字。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM