【发布时间】:2014-10-30 00:13:27
【问题描述】:
我整天都在做这件事,但无法正确输出。 我想要做的是输入两个数字,这将被推入两个数组,以便我们可以减去或添加它们并显示结果。看起来很简单,但抓住的地方很少
- 输入必须从用户一个一个推入数组。
- 如果我没有输入值,代码应该假定它是
'0'或'null'。'0'如果在开头,'null'如果在结尾。例如,如果第一个数字是234,第二个数字是23,那么代码应该输入'023',如果我输入第一个数字为2,第二个数字为3,但不要在end 那么代码应该假定它是null。
问题
- 如果总和大于
10,我不能将“carry”带到下一组。这意味着我得到的值只是两个数字的加法,它是否大于 10 并不重要。例如添加234和890给我[10, 12, 4]
这是代码.....
#include<iostream>
using namespace std;
main() {
int first[10], second[10], result[10], c, n;
cout << "Enter the number of elements in the array ";
cin >> n;
if (n > 10 || n < 0) {
std::cout << "invalid number, you are a bad reader" << endl;
system("PAUSE");
return 0;
}
cout << "Enter elements of first array " << endl;
for (c = 0; c < n; c++) {
cin >> first[c];
if (first[c] > 9 || first[c] < 0) {
std::cout << "invalid number, you are a bad reader" << endl;
system("PAUSE");
return 0;
}
}
cout << "Enter elements of second array " << endl;
for (c = 0; c < n; c++)
cin >> second[c];
cout << "Sum of elements of two arrays " << endl;
for (c = 0; c < n; c++)
cout << first[c] + second[c] << endl;
if ((first[c] + second[c]) > 9) {
cout << "overflow" << endl;
}
//result[c] = first[c] + second [c];
//cout << result[c] <<endl;
system("PAUSE");
return 0;
}
我非常感谢一些建议。
【问题讨论】:
-
我不明白关于不输入值的部分。
-
这意味着当程序要求我输入一个值并且我没有输入任何内容并且只需按 Enter 键时,它应该假定它为 0 或 null。很抱歉造成混乱
-
提示:您可以使用
[c - 1]帮助处理进位 -
int不能为空。 -
@nothing 你是新加坡国立大学的吗?如果是,请告诉我。我当然可以帮助你。