2016-03-25 72 views
-2

我試圖創建一個程序,其中用戶被要求輸入10個數字,數字都存儲在一個數組中列表,然後將數組列表放入一個函數中,該函數將返回最大值和最小值。試圖通過函數查找數組的最大值和最小值,其中數組值由用戶輸入

到目前爲止,我只代碼爲最大值,但我不能讓功能在所有的工作,我一直在使用這個鏈接,瞭解通過數組功能:

http://www.tutorialspoint.com/cplusplus/cpp_passing_arrays_to_functions.htm

我想我正確地遵循他們的語法,但我得到的錯誤數組沒有在函數參數中聲明,以及我的函數(minmax)如何不能用作函數,現在我已經超出了困惑!

道歉,如果答案是顯而易見的,我還是新的C++

#include<iostream> 
#include<iomanip> 
using namespace std; 


int minmax(array[]); 

int main() 
{ 

int numbers[10]; 
int input; 

cout << "Please enter ten numbers" << endl; 

for(int i=0;i<10;i++){ 

    cin >> input; 
    numbers[i] = input; 

    mm = minmax(numbers); 

} 

} 

void minmax(array[]){ 


int max = 0; 
for (int i = 0 ; i < size ; i++); 
{ 
    if (list[i] > highNum) 
     max = array[i]; 
     cout << array[i]; 
} 
cout << max; 

} 
+1

'mm'未申報。 – DimChtz

+1

函數定義中數組的數據類型在哪裏? – anukul

+0

'int minmax(array []);'然後'void minmax(array []){...'why void。如果沒有返回,則嘗試'mm = minmax(數字);' – DimChtz

回答

0
#include<iostream> 

using namespace std; 


int minmax(int* array, int size); 

int main() 
{ 
    int numbers[10]; 
    int input, mm; 

    cout << "Please enter ten numbers" << endl; 

    for(int i=0;i<10;i++){ 
     cin >> input; 
     numbers[i] = input; 
    } 

    mm = minmax(numbers, 10); 

} 

int minmax(int array, int size){ 
    int max = 0; 

    for (int i = 0 ; i < size ; i++); 
    { 
     if (array[i] > max) { 
      max = array[i]; 
      cout << array[i]; 
     } 
    } 
    return max; 
} 

應該工作,但你應該(重新)讀取C/C++基礎知識。

另一種解決方案是從std C++ lib中使用<vector><algorithm>

0

假設這不是一項家庭作業,以下是您的方法的一些建議。 (否則,@ Xirema的回答是更好的辦法)
3件事開始:
1)mm沒有定義在你的主。
2)聲明:mm = minmax(numbers);應該移出for循環。
3)此外,本聲明:mm = minmax(numbers);建議您需要通過幾種方法更改minmax的函數原型。見下文。

更換您的最小最大功能(見代碼的解釋註釋)

int minmax(int array[], int size)//need types for arguments 
           //(int) and prototype needs 
           //to return a value (void -> int) 
{ 

    int max = INT_MIN;//<limits.h> initialize to negative 
         // (-2147483647 - 1) for 32 bits 
    for (int i = 0 ; i < size ; i++) 
    { 
     if (array[i] > max) 
     {     //added braces to be explict 
      max = array[i]; // `list` should be `array` 
          //highNum is not needed, use max. 
      cout << array[i];//Note, without curly braces, this 
          //statement would not be executed 
     } 
    } 
    cout << max; 
    return max; //requires a return value 
} 

注意,這保留了你的原始代碼示例會,僅在最大程度返回的結果。

1

假設這是不是一個家庭作業(這可能需要你真正推出自己的「最小/最大」功能),這個代碼就足夠了:

std::vector<int> values(10); 
cout << "Please enter ten numbers" << endl; 

for(int i=0;i<10;i++){ 
    cin >> values[i]; 
} 

int min_value = *std::min_element(values.begin(), values.end()); 
int max_value = *std::max_element(values.begin(), values.end()); 

,或者您需要最大/最小值與單一的函數調用來計算,你可以重寫最後兩行是這樣的:

auto minmax = std::minmax_element(values.begin(), values.end()); 
int min_value = *(minmax.first); 
int max_value = *(minmax.second); 
0

你需要糾正一些錯誤:

#include<iostream> 
#include<iomanip> 
using namespace std; 


int minmax(/*no type specified, should be int*/array[]); 

int main() 
{ 

    int numbers[10]; 
    int input; 
    /*mm is not declared, but is used later, declare it*/ 

    cout << "Please enter ten numbers" << endl; 

    for(int i=0;i<10;i++){ 

     cin >> input; 
     numbers[i] = input; 
     /*if minmax is void, then it cannot be assineg*/ 
     mm = minmax(numbers); 

    } 

} 

/*minmax returns int in declaration, here returns void*/ void minmax(/*no type specified*/array[]){ 


    int max = 0; 
    for (int i = 0 ; i < /*size was not declared, declare it*/ size ; i++); 
    { 
     if (/*list is undeclared, (did you mean array?)*/list[i] > /*highNum not declared, did you mean max?*/highNum) 
      max = array[i]; 
     cout << array[i]; 
    } 
    cout << max; 

} 
+0

嗨,我真的很喜歡你這樣做的方式,因爲這樣學習更容易。我已經按照你的指示去了解我的錯誤,並且聲明似乎是我最大的問題。然而,它不允許我在兩次聲明int的函數,我也不能將數組聲明爲int類型兩次,所以我只是在頂部聲明。但對於實際的函數,我不能使用void或int,我應該使用什麼? – Dante

+0

@丹特。如果你遵循指示,那麼沒有太多東西需要改變。假設minmax返回'int',參數中的數組類型也是'int'。讓我們再爲函數添加一個參數,即大小(未聲明的大小),所以它只會返回您已經輸入的數字的最大值。更改:聲明:int minmax(int array [],int size);',mm賦值:'mm = minmax(數字,i + 1);',main下的函數應該有精確的返回類型和參數。另外請注意重要的事情 - 有'''在循環功能後。刪除它,否則它什麼都不會做。 – xinaiz

相關問題