2015-03-02 58 views
0
#include <iostream> 

using namespace std; 

int main() 
{ 
    const int MAXNUM = 10; // this code creates 10 variables in fmax 
    int fmax[MAXNUM], maximum, i, l; // initialize fmax that contains 10 variables , maximum, i for the loop, and l for storing the location 

    cout << "enter 10 numbers: "; 

    maximum = fmax[0]; // this sets the maximum to 0 

    for(i = 0; i < MAXNUM; i++) // this is the code for finding the maximum numbers 
    { 
     cin >> fmax[i]; 
     if(fmax[i] > maximum){ 
      maximum = fmax[i]; 
      l = i; 
     } 
     else{ 
      maximum = maximum; 
      l = l; 
     } 
    } 

    cout << "the maximum number: " << maximum << endl; // outputs the results 
    cout << "the location of the number: " << l << endl; 

    return 0; 
} 

我在這個練習中遇到問題 問題是程序的輸出。 它不顯示 最大數目和所在 它總是這樣表示找到陣列的最大值其位置

最大號碼爲:1987579782

數的位置是:26355764

我需要顯示輸入的最大數量和它的下標,我不知道該怎麼 有什麼錯我的代碼

這裏是練習題

a.write,編譯並運行一個C++程序,將10個整數輸入到一個名爲fmax的數組中,並確定輸入的最大值,程序應該只包含一個循環,最大值應該在輸入數組元素值時被確定(提示。設置最大值等於第一個數組元素,應該在用於輸入剩餘數組值的循環之前輸入),並跟蹤數組中最大元素和最大索引數。

+1

剛想代碼中的小評論:在'if'的'else'語句是unneccesary。如果條件不符合,值不會改變。 – 2015-03-02 05:32:19

回答

4

在使用之前,您需要初始化fmax[MAXNUM]陣列。簡單地聲明一個變量並不能保證它將爲0.目前fmax[0]的值可能是int範圍內的任何值,因爲您尚未將其初始化爲值。

你還需要將l變量初始化爲0(這就是爲什麼你得到了錯誤的位置)

試試這個:

#include <iostream> 

using namespace std; 

int main() 
{ 
const int MAXNUM = 10; // this code creates 10 variables in fmax 
int fmax[MAXNUM], maxinum, i, l = 0; // initialize fmax that contains 10 variables , maxinum, i for the loop, and l for storing the location 

for(i = 0; i < MAXNUM; ++i) 
{ 
    fmax[i] = 0; 
} 

cout << "enter 10 numbers: "; 

maxinum = fmax[0]; // this sets the maxinum to 0 

for(i = 0; i < MAXNUM; i++) // this is the code for finding the maxinum numbers 
{ 
    cin >> fmax[i]; 
    if(fmax[i] > maxinum){ 
     maxinum = fmax[i]; 
     l = i; 
    } 
    else{ 
     maxinum = maxinum; 
     l = l; 
    } 
} 

cout << "the maxinum number: " << maxinum << endl; // outputs the results 
cout << "the location of the number: " << l << endl; 

return 0; 

} 

編輯:既然是規定,即代碼應只包含一個循環,你可能想改變你這樣做的方式。我上面提供的例子並不是一個很好的方法。這是我會怎麼做(不使用任何可能造成混淆的C++標準庫函數)

#include <iostream> 

int main() 
{ 
    std::cout << "Enter 10 numbers: "; 
    const int MAXNUM = 10; 
    int fmax[MAXNUM] = {0}; // this is an easy way to initialize the array elements to zero 
    int maxNum = 0, location; 

    for(int i = 0; i < MAXNUM; i++) { 
     std::cin >> fmax[i]; 
     if(fmax[i] > maxNum) { 
      maxNum = fmax[i]; 
      location = i; 
     } 
    } 

    std::cout << "The maximum number is " << maxNum << std::endl; 
    std::cout << "The location of the number is " << location << std::endl; 

    return 0; 
} 

確保你明白爲什麼這個工程 - 讓我知道如果您有任何問題。

+0

哇感謝它的工作原理,但我有一個問題如何此代碼工作(int i = 0;我 2015-03-02 05:34:02

+0

@KrysselTillada該代碼只是簡單地遍歷'fmax'數組中的每個項目並將其設置爲0 – developerbmw 2015-03-02 05:35:38

+0

但是如果我想要查找值的最小數目? – 2015-03-02 06:00:56

1

要初始化maximum垃圾值:

maxinum = fmax[0]; // this sets the maxinum to 0 

既然你沒有輸入任何內容呢。

我會建議你使用內置的功能std::max_elementalgorithm庫:它會返回指針最大元素,所以,你可以與它一起最大元素的輸出位置值:

#include <algorithm> 

// Your code 
// You should enter the whole array 

auto max_element = std::max_element(std::begin(fmax), std::end(fmax)); 
std::cout << "Position: " << (max_element - std::begin(fmax)) << std::end; 
std::cout << "Value: " << *max_element << std::endl; 
1

局部變量(非類)在C++中默認不會初始化爲零。你的問題是你初始化maxinumfmax[0]的價值,其中在開始是垃圾。如果您從未輸入任何更大的數字,則I的值永遠不會更改,並且也是垃圾。您需要將這些變量明確初始化爲零:

int fmax[MAXNUM] = { 0 }; 
into maxinum = 0, I = 0 
+0

哦,謝謝我明白 – 2015-03-02 05:41:02

0

maxinum = fmax[0]; // this sets the maxinum to 0

這不設置maxinum爲0,你還沒有設置fmax[0]0

你可以這樣做:

#include <iostream> 

using namespace std; 

int main() 
{ 
    const int MAXNUM = 10; // this code creates 10 variables in fmax 
    int fmax[MAXNUM], maxinum, i, l; // initialize fmax that contains 10 variables , maxinum, i for the loop, and l for storing the location 
    cout << "enter 10 numbers: "; 
    for(i = 0; i < MAXNUM; i++) // this is the code for finding the maxinum numbers 
    { 
     cin >> fmax[i]; 
     if(i==0) 
      maxinum = fmax[i]; //....... this will do what you are trying to achieve 
     if(fmax[i] > maxinum){ 
      maxinum = fmax[i]; 
      l = i; 
     }      // the else block you wrote is not necessary :) 
    } 
    cout << "the maxinum number: " << maxinum << endl; // outputs the results 
    cout << "the location of the number: " << l << endl; 
    return 0; 
}