2015-06-16 77 views
1

我有這個代碼,我一直在努力的樂趣,它是儘可能基本的,因爲我是一個初學者,它工作正常,但我似乎無法弄清楚如何讓它顯示最少和最多量的煎餅吃。提前謝謝你。C++數組初學者

#include <iostream> 
using namespace std; 

int main(){ 

    int pancakes[10]; 
    int x,i; 

    cout << "Hello user!" << endl; 
    cout << endl; 
    cout << "Please enter how many pancakes did each of the 10 people eat:" << endl; 
    cout << endl; 

    for (i=0;i<10;i++){ 
     cin >> x; 
     pancakes[i]=x; 
    } 

    cout << "1st person ate" << " " << pancakes[0] << " " << "pancakes" << endl; 
    cout << "2nd person ate" << " " << pancakes[1] << " " << "pancakes" << endl; 
    cout << "3rd person ate" << " " << pancakes[2] << " " << "pancakes" << endl; 
    cout << "4th person ate" << " " << pancakes[3] << " " << "pancakes" << endl; 
    cout << "5th person ate" << " " << pancakes[4] << " " << "pancakes" << endl; 
    cout << "6th person ate" << " " << pancakes[5] << " " << "pancakes" << endl; 
    cout << "7th person ate" << " " << pancakes[6] << " " << "pancakes" << endl; 
    cout << "8th person ate" << " " << pancakes[7] << " " << "pancakes" << endl; 
    cout << "9th person ate" << " " << pancakes[8] << " " << "pancakes" << endl; 
    cout << "10th person ate" << " " << pancakes[9] << " " << "pancakes" << endl; 

    return 0; 
} 
+0

** **閱讀很多**更多關於[C++程序設計(HTTP ://www.stroustrup.com/Programming/)**,然後**像[std :: vector]一樣使用標準的[容器](http://en.cppreference.com/w/cpp/container)**( http://www.cplusplus.com/reference/vector/vector/)。 –

回答

0

首先,而不是有大約10 cout's,你可以使用一個循環打印出來:

for (i=0;i<10;i++){ 

    if(i==0) 
     cout <<i+1<< "st person ate" << " " << pancakes[i] << " " << "pancakes" << endl; 
    else if(i==1) 
     cout << i+1<<"nd person ate" << " " << pancakes[i] << " " << "pancakes" << endl; 
    else if(i==2) 
     cout << i+1<<"rd person ate" << " " << pancakes[i] << " " << "pancakes" << endl; 
    else 
     cout <<i+1<< "th person ate" << " " << pancakes[i] << " " << "pancakes" << endl; 
} 

其次,你可以直接輸入數值到您的數組,不需要中間變量x

for (i=0;i<10;i++){ 

    cin >> pancakes[i]; 
} 

爲了您的最大值和最小值問題,可採取兩個變量,說 - maxmin。將它們初始化爲任意最小值(例如0,如果不處理負數)和最大值(例如INT_MAX)。或者,您可以將它們初始化爲數組的第一個元素。

對於查找最大值和最小值,可以遍歷整個數組,同時檢查元素是大於還是小於最大值和最小值變量。如果是這樣,然後將它們分配給您的變量:

for (i=0;i<10;i++){ 

if(pancakes[i]>max) 
    max = pancakes[i]; 

if(pancakes[i]<min) 
    min = pancakes[i]; 
} 
+0

很高興解釋如何用循環生成「人吃」文本。 –

+0

@ YvesDaoust:謝謝,編輯。 –

+0

我不知道爲什麼,它顯示最大的一些隨機數。最小工作正常 – Royal

1

您可以使用min_elementmax_element標準庫中做到這一點:

#include <algorithm> 

cout << "The smallest number of pancakes was " << *min_element(pancakes, pancakes + 10) << endl; 
cout << "The largest number of pancakes was " << *max_element(pancakes, pancakes + 10) << endl; 
+2

或直接'std :: minmax_element' – Jarod42

+0

我把這個評論,也在程序中,非常簡單和有用。謝謝 – Royal

1

既然你是一個初學者,我會使用一個循環把一個簡單的解決方案。

int max = 0; 
for(i = 0; i < 10; i++) { 
    if(pancakes[i] > max) max = pancakes[i]; 
} 
cout << "Most amount of pancakes eaten by a single person: " << max << endl; 
0

您可以添加std::cout裏面像下面循環,

for (int i = 0; i < 10; i++) 
{ 
    std::cin >> pancakes[i]; 
    std::cout << i+1 <<"st person ate" << " " << pancakes[i] << " " << "pancakes" << std::endl; 
} 

int maxpancakes = pancakes[0]; 
int minpancakes = pancakes[0]; 

for(int i = 0; i < pancakes.length(); i++) 
{ 
    if(pancakes[i] < minpancakes) 
     minpancakes = pancakes[i]; 
    if(pancakes[i] > maxpancakes) 
     maxpancakes = pancakes[i]; 
} 

std::cout << "The smallest pan cake had is :" << minpancakes << std::endl; 
std::cout << "The max pan cake had is :" << maxpancakes << std::endl;