2014-04-14 29 views
-1

所以即時通訊有一個小問題。我只是不知道我是否正確地做。這個問題很模糊。 (對我來說)想知道我是否能夠得到一些幫助,因爲我現在已經在我的書中解決了這個簡單的問題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; 
} 
+11

代碼不會在所有問題的描述相符。 – chris

+0

問題要求像這樣的數組{1,2,3,... 100} – cppguy

+0

哦,geez。我忘了提到我推出的這個代碼是一個早期的項目,它認爲它與它「相似」。但我認爲這是一個更難的過程,然後下面的答案。感謝您的小費。我就像一個月。所以我爲可怕的解釋道歉。 – Harkins1721

回答

2

代碼的核心只需要創建數組,與

int arr[] = new int[100]; 

然後將其填入for循環,例如,與

for (i = 0; i<100; i++) arr[i] = i+1; 

請注意,數組指數從零開始計數,但您想從一開始填充。

+0

謝謝。我使用陣列很短的時間,它使我感到困惑。我購買了John Molluzzo編寫的「C++ for Business程序員」一書,僅在第8章中做了介紹,下面的一些回覆使用了我尚未使用的函數。你們似乎是最合法的。至少在我目前的狀態。 – Harkins1721

0

您確定代碼與您的問題有關嗎?

,你想要什麼樣的樣本程序是這樣的:

#include <stdlib.h> 
#include <stdio.h> 

#define N 100 

int main(void) 
{ 
     int arr[N], i; 

     for (i = 0; i < N; i++) 
       arr[i] = i + 1; 

     for (i = 0; i < N; i++) 
       printf("%d ", arr[i]); 

     return 0; 
} 
0
#include <iostream> 
#define NUM_VALUES 100 

int main() 
{ 
    int i = 1; 
    int array[NUM_VALUES]; 

    while (i <= NUM_VALUES) 
    { 
     array[i-1] = i; 
     i++; 
    } 

    i = 1; 

    while (i <= NUM_VALUES) 
    { 
     std::cout << array[i-1] << " "; 
     i++; 
    } 

    std::cout << std::endl; 
    return 0; 
} 
相關問題