对于我的作业,我们需要创建一个生成
的代码然而,如果用户说y
问题(AKA第2步)
任何帮助都很棒
int main()
{
int numb=0;
int *newar=NULL;
char response;
cout << "enter integer:";
cin >> numb;
newar = new int [numb];
for(int i=0;i<numb;i++)
{
newar[i]=rand() % 101;
cout << "Do you want to see array? (Y/N) :";
cin>> response;
if (response != 'n')
{
cout<<newar[i]<<" ";
}
}
return 0;
}
答案 0 :(得分:1)
首先填充数组:
for(int i =0; i<numb; i++)
{
newar[i] = rand() % 101;
}
然后提示用户:
cout << "Do you want to see array? (Y/N) :";
cin >> response;
如果是,请进行新循环:
if (response != 'n')
{
for(int j = 0; j < numb; j++) // (you could use i as well, it would be another i
{
cout << newar[j] << " ";
}
}