所以即時通訊有一個小問題。我只是不知道我是否正確地做。這個問題很模糊。 (對我來說)想知道我是否能夠得到一些幫助,因爲我現在已經在我的書中解決了這個簡單的問題2個小時了,它讓我覺得很開心!在此先感謝:)編寫一個從1到100填充100個整數數組的程序
「編寫一個填充程序」填充「100個整數元素數組從1到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;
}
代碼不會在所有問題的描述相符。 – chris
問題要求像這樣的數組{1,2,3,... 100} – cppguy
哦,geez。我忘了提到我推出的這個代碼是一個早期的項目,它認爲它與它「相似」。但我認爲這是一個更難的過程,然後下面的答案。感謝您的小費。我就像一個月。所以我爲可怕的解釋道歉。 – Harkins1721