【发布时间】:2014-04-14 23:50:14
【问题描述】:
所以我对此有一个小问题。我只是不知道我这样做是否正确。问题很模糊。 (对我)想知道我是否可以得到一些帮助,因为我已经在我的书中解决了这个简单的问题 2 个小时了,这让我很生气!在此先感谢:)
“编写一个程序,用 1 到 100 的数字“填充”一个由 100 个整数元素组成的数组,然后输出数组中的数字。”
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int size = 301;
int N, I, k;
int score[size];
srand(time(0));
cout << setprecision(2)
<< setiosflags(ios::fixed)
<< setiosflags(ios::showpoint);
//1)Get # of bowlers ..............................................................
cout << "Enter number of bowlers? (Must be between 1 and 301) ";
cin >> N;
while (N<1||N>size)
{
cout << "\nError!! Must be between 1 and 301!! ";
cin >> N;
}
//2) and 5) Get scores ............................................................
for(I = 0; I<N; I++)
{
//cout << "\nEnter score for bowler # " << I + 1 << " ";
//cin >> score[I];
score[I]=rand()%301;
for(k=0; k<I; k++)
{
if(score[k]==score[I])
{
I--;
break;
}
}
}
//3)Get Sum/Avg .....................................................................
int sum = 0;
float avg;
for(I = 0; I<N; I++)
{
sum += score [I];
}
avg = float(sum)/N;
//4) Output scores, sum, and avg ....................................................
for(I = 0; I<N; I++)
{
cout << "\nScore for bowler # " << I + 1 << " is " << score[I];
}
cout<<"\n\n The Sum is " << sum << "\n";
cout <<"\n The Average is "<< avg << "\n";
cout<<"\n\n\n";
system ("pause");
return 0;
}
【问题讨论】:
-
代码与问题描述完全不符。
-
问题要求像这样的数组 {1, 2, 3,.... 100}
-
天哪。我忘了提到我发布的这段代码是一个较早的项目,假设与它“相似”。但我认为这是一个比下面的答案更难的过程。感谢您的小费。我喜欢一个月。所以我为糟糕的解释道歉。
-
真的吗?还没有人提到
std::iota?这就是它的用途。