2014-03-30 43 views
0

如何讓我的輸出從1,2,3,4,5改爲說明第一,第二,第三,第四,第五?另外,最後,當它聲明哪個輸入是最小的時候,我怎樣才能說出它是否是輸入1-5,而不是實際的輸入數字?如何將輸出從1,2,3,4,5更改爲第一,第二等?

#include <iostream> 
using namespace std; 
int main() 
{ 
int num[5]; 
int i = 0; 
int small=0; 

    cout <<"Enter the first number: "; 
    cin >> num[0]; 
    small = num[0]; 
    { 
     for(i = 1; i < 5; i++) 
      { 
      cout << "Enter the number "<< i + 1 <<" number: "; 
      cin >> num[i]; 
       if (num[i] < small)   
        small = num[i]; 
      } 
    } 
cout<<endl<<endl; 
cout<<"Entry No. "<<small<<" is the minimum number"<<endl<<endl; 

return 0; 
} 
+0

這是'i'你正在尋找的循環中,所以找到一個方法來拯救它,讓它循環外部訪問。 –

+0

@GermaineJason你不能在數組中混合類似的類型。 –

+0

@BlueIce對不起noob錯誤! – GermaineJason

回答

0

你想不僅找到最小num[i],也相應i。這很簡單。關鍵是用

if (num[i] < small) { // when num[i] is the smallest so far 
    small = num[i];  //  save it 
    smalli = i;   //  and save the matching i 
} 

更換循環

if (num[i] < small)   
    small = num[i]; 

的膽量,我懷疑你能找出變量的聲明和初值。

要打印基數的話,你可以在提示符下使用

const char* cardinals[] = { "first", "second", "third", "fourth", "fifth" }; 

,然後顯示cardinals[i]。和/或在末尾cardinals[smalli]

std::cout << "\n\nThe " << cardinals[smalli] " number was the minimum, " << small << "\n\n"; 
0

請嘗試以下

#include <iostream> 

int main() 
{ 
    const size_t N = 5; 

    const char *serial_num[N] = { 'first", "second", "third", "fourth", "fifth" }; 
    int num[N]; 

    size_t small; 

    for (size_t i = 0; i < N; i++) 
    { 
     std::cout << "Enter the " << serial_num[i] << " number: "; 
     std::cin >> num[i]; 

     if (i == 0 || num[i] < num[small]) small = i; 
    } 

    std::cout << "\n\nEntry No. " << serial_num[small] << " is the minimum number\n" << std::endl; 

    return 0; 
} 
相關問題