2016-03-25 70 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

相关问题