【发布时间】:2018-09-29 08:47:13
【问题描述】:
我是 C++ 新手,我试图了解如何使用数组。我的想法是:
- 我希望用户输入一个数组,程序输出 数组
- 将数组中的所有值加倍并输出
- 并且对于每个加倍的值,添加加倍的数字的数字 (1位数字将保持不变),然后将新数字输出为 好吧。
(例如,如果数组是 [5, 6, 7, 8],则加倍的值将是 [10, 12, 14, 16],然后您将添加每个值的数字,例如 [1+0, 1+ 2, 1+4, 1+6] 得到 [1, 3, 5, 7]。
我把我的代码展示我的进度,请随时指出过程中的任何错误!
感谢任何帮助!
附言嵌套循环不起作用:(
#include <iostream>
#include <string>
using namespace std;
int maxNum;
int num[20];
int main()
{
cout << "Enter an Array" << endl;
for (int i=0;i<20;i++)
{
cin >> num[i];
maxNum++;
if (num[i]==-1)
break;
}
cout <<"Your array is: " << endl;
for (int i=0;i<maxNum-1;i++)
cout << num[i];
cout << endl;
cout << "Your doubled array is:" << endl;
for (int j=0;j<maxNum-1;j++)
{
num[j]*=2;
cout << num[j];
}
cout << endl;
cout << "When the digits of each seat are added..." << endl;
for (int k=0;k<maxNum;k++)
{
for (int l=0;l<maxNum;l++)
{
int sum[20];
while (num[k]!=0)
{
sum[l]=sum[l]+num[k]%10;
num[k]=num[k]/10;
}
}
cout << sum[l];
}
cout << endl;
}
【问题讨论】: