#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的數組中,並確定輸入的最大值,程序應該只包含一個循環,最大值應該在輸入數組元素值時被確定(提示。設置最大值等於第一個數組元素,應該在用於輸入剩餘數組值的循環之前輸入),並跟蹤數組中最大元素和最大索引數。
剛想代碼中的小評論:在'if'的'else'語句是unneccesary。如果條件不符合,值不會改變。 – 2015-03-02 05:32:19