2015-05-02 21 views
0
#include <iostream> 

using namespace std; 

int main() 
{ 
    int personPancake[10]; 
    int small, big; 

    for (int c = 0; c < 10; c++) 
    { 
     cout << "Enter how many pancakes person " << c + 1 << " ate: "; 
     cin >> personPancake[c]; 
    } 

    big = small = personPancake[0]; 

    for (int c = 0; c < 10; c++) 
    { 
     if (personPancake[c] > big) 
     { 
      big = personPancake[c]; 
     } 

     if (personPancake[c] < small) 
     { 
      small = personPancake[c]; 
     } 
    } 

    cout << "Biggest: " << big << endl; 
    cout << "Smallest: " << small << endl << endl; 
} 

這是我有atm的代碼,我已經想出了最小和最大的數字,你可以看到。我需要幫助找出具有最大和最小值的元素的索引。我如何找出數組中哪個元素具有最高值?

+0

聲明另外兩個變量big_index和small_index,並將它們初始化爲0.每當更新big的值時,更新big_index的值並將其設置爲c。每當你更新'small'的值時,更新'small_index'的值並將其設置爲'c'。 –

+0

首先,刪除輸入法並使用'pancake [5] = {12,34,56,45,67};''這樣你就不必每次輸入10個數字來測試函數。稍後放回輸入法。 –

回答

2

您可以設置兩個其他變量來保存當前最小和最大索引。所以在你的if語句中...

int biggestIndex, smallestIndex; 

if (personPancake[c] > big) 
    { 
     biggestIndex = c; 
     big = personPancake[c]; 
    } 

    if (personPancake[c] < small) 
    { 
     smallestIndex = c; 
     small = personPancake[c]; 
    } 
+0

謝謝,我正在尋找什麼!我不能相信我在這裏坐了很長時間,並沒有想到這一點。 – Kyurino

0

不要存儲personPancake的值[c]。 將c的索引存儲爲大或小。

然後改變你的比較使用personPancake [大或小]。

0

嘗試

#include <iostream> 

using namespace std; 

int main() 
{ 
int personPancake[10]; 
int small, big; 
int indexsmall=0,indexbig=0; 
for (int c = 0; c < 10; c++) 
{ 
    cout << "Enter how many pancakes person " << c + 1 << " ate: "; 
    cin >> personPancake[c]; 
} 

big = small = personPancake[0]; 

for (int c = 0; c < 10; c++) 
{ 
    if (personPancake[c] > big) 
    { 
     big = personPancake[c]; 
     indexbig=c; 
    } 

    if (personPancake[c] < small) 
    { 
     small = personPancake[c]; 
     indexsmall=c; 
    } 
} 

cout << "Biggest: " << big << endl; 
cout << "Index Biggest: " << indexbig << endl; 
cout << "Smallest: " << small << endl << endl; 
cout << "Index Smallest: " << indexsmall << endl << endl; 

}

0

您需要存儲c當你發現的最大和最小的號碼你爲big以同樣的方式和small

int bigIdx, smallIdx; 

for (int c = 0; c < 10; c++) 
    { 
     if (personPancake[c] > big) 
     { 
      big = personPancake[c]; 
      bigIdx = c; 
     } 

     if (personPancake[c] < small) 
     { 
      small = personPancake[c]; 
      smallIdx = c; 
     } 
    } 
0

添加變量持有指數

int main() 
{ 
    int personPancake[10]; 
    int small, big, smallIndex, bigIndex; 

    for (int c = 0; c < 10; c++) 
    { 
     cout << "Enter how many pancakes person " << c + 1 << " ate: "; 
     cin >> personPancake[c]; 
    } 

    big = small = personPancake[0]; 
    bigIndex = smallIndex = 0; 

    for (int c = 0; c < 10; c++) 
    { 
     if (personPancake[c] > big) 
     { 
      big = personPancake[c]; 
      bigIndex = c; 
     } 

     if (personPancake[c] < small) 
     { 
      small = personPancake[c]; 
      smallIndex = c; 
     } 
    } 

    cout << "Biggest: " << big << endl; 
    cout << "Smallest: " << small << endl; 
    cout << "Biggest Index: " << bigIndex << endl; 
    cout << "Smallest Index: " << smallIndex << endl << endl; 
} 
相關問題