2015-04-05 17 views
-3

我在格式ARR [I]> ARR比較數組值[MAX]C++僅輸出最大數組值和計數

它輸出其中較大者爲[0]的常用3的所有值。

你們能告訴我如何輸出最大的價值嗎?

代碼:

for(int i=0;i<4;i++){ 
     cout<<"Enter how many pancakes eaten by the Person #"<<i<<" : "; 
     cin>>pancake; 
     person[i] = pancake;    
    } 
    for(int i=0;i<4;i++){ 
     if(person[i]>person[i+1]){ 
      pancakeCount = pancake[i]; 
      cout<<"The number of most eaten pancakes is "<<pancakeCount<<" by Person#"<<i<<endl; 
     } 
    } 

的問題是輸出:

輸出:

//list of persons and the number of pancakes they ate. 
    The number of most eaten pancakes is 10 by Person#7 
    The number of most eaten pancakes is 10 by Person#9 
每次我把最大的價值最後一次迭代前

,還輸出了最後一次迭代包含最大的價值是人#7

我只想要最高價值是產出本身。

+1

這個代碼是沒有意義的,你用'煎餅'在第一個循環中沒有索引,但是你使用'pancake [i]'。你需要發佈更多的上下文。如果你不想讓它打印每個循環迭代,你也可能想要將第二個'cout'行移到循環的外部。 – 2015-04-05 03:24:52

回答

0
int pancakeCount(0), max_person(0); 
for(int i=0;i<4;i++){ 
    cout<<"Enter how many pancakes eaten by the Person #"<<i<<" : "; 
    cin>>pancake; 
    person[i] = pancake;    
} 
for(int i=0;i<4;i++){ 
    if(person[i]>pancakeCount){ 
     pancakeCount = pancake[i]; 
     max_person = i; 
    } 
} 

cout<<"The number of most eaten pancakes is "<<pancakeCount<<" by Person#"<<i<<endl; 
0

拆分循環以查找最大值並打印最大值。

代碼:

#include <iostream> 
using namespace std; 

int main(){ 
    int person[4]; 
    int pancake, pancakeMax, pancakeMin; 
    for (int i = 0; i<4; i++) 
    { 
     cout << "Enter how many pancakes eaten by the Person #" << i << " : "; 
     cin >> pancake; 
     person[i] = pancake; 
    } 

    //equate to one of the elements to make sure negative numbers are accomodated 
    pancakeMax = pancakeMin = pancake; 

    // most eaten 
    for (int i = 0; i<4; i++) 
    { 
     if (person[i]>pancakeMax) 
     { 
      pancakeMax = person[i]; 
     } 
    } 
    for (int i = 0; i<4; i++) 
    { 
     if (person[i] == pancakeMax) 
     { 
      cout << "The number of most eaten pancakes is " << pancakeMax << " by Person#" << i << endl; 

     } 
    } 

    //least eaten 
    for (int i = 0; i<4; i++) 
    { 
     if (person[i]<pancakeMin) 
     { 
      pancakeMin = person[i]; 
     } 
    } 
    for (int i = 0; i<4; i++) 
    { 
     if (person[i] == pancakeMin) 
     { 
      cout << "The number of least eaten pancakes is " << pancakeMin << " by Person#" << i << endl; 

     } 
    } 
    return 0; 
} 

您可以在第二循環中,如果你認爲你的邏輯只需要一個值添加break語句。否則,現在,它給所有最大的煎餅吃了。

如果您靶向效率可以合併循環,像這樣:

#include <iostream> 
using namespace std; 

int main(){ 
    int person[4]; 
    int pancake, pancakeMax, pancakeMin; 
    int iMax, iMin; 
    for (int i = 0; i<4; i++){ 
     cout << "Enter how many pancakes eaten by the Person #" << i << " : "; 
     cin >> pancake; 
     person[i] = pancake; 
    } 

    //equate to one of the elements to make sure negative numbers are accomodated 
    pancakeMax = pancakeMin = pancake; 

    for (int i = 0; i<4; i++){ 
     pancake = person[i]; 
     if (pancake>pancakeMax){// most eaten 
      pancakeMax = pancake; 
      iMax = i; 
     } 
     if (pancake < pancakeMin){//least eaten 
      pancakeMin = pancake; 
      iMin = i; 
     } 
    } 

    cout << "The number of most eaten pancakes is " << pancakeMax << " by Person#" << iMax << endl; 
    cout << "The number of least eaten pancakes is " << pancakeMin << " by Person#" << iMin << endl; 

    return 0; 
} 
+0

這工作的人。我很容易理解。如何獲得最少?我的代碼不會輸出最小值或獲取最小值。 – Jepoy 2015-04-05 07:53:36

0

嗨你的代碼是錯誤的algorithim嘗試礦井

for(int i=0;i<4;i++) 
{ 
    std::cout<<"Enter how many pancakes eaten by the Person #"<<i<<" : "; 
    std::cin>>pancake; 
    person[i] = pancake;    
} 
for(int i=0;i<4;i++) 
{ 
    if(person[i]>pancakeCount) 
    { 
     pancakeCount = person[i]; 
     personWhoEatMost = i; 
    } 
} 
std::cout<<"The number of most eaten pancakes is "<<pancakeCount 
     <<" by Person#"<<personWhoEatMost<<std::endl; 
+0

獲取最少數量如何?我正在測試答案的時間,不幸還沒有找到答案。 – Jepoy 2015-04-05 07:52:59

相關問題