简体   繁体   English

尝试通过函数查找数组的最大值和最小值,其中数组值由用户输入

[英]Attempting to find max and min values of an array through a function, where the array values are by user input

I'm trying to create a program where the user is asked for 10 numbers, the numbers are all stored in an array list, and then the array list is put through a function, the function is going to return the max and min values.我正在尝试创建一个程序,其中要求用户输入 10 个数字,这些数字都存储在一个数组列表中,然后将数组列表放入一个函数中,该函数将返回最大值和最小值。

So far I only have code for a max value but I can't get the function to work at all, I've been using this link to learn about passing arrays to functions:到目前为止,我只有最大值的代码,但我根本无法使该函数工作,我一直在使用此链接来了解如何将数组传递给函数:

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

And I think I'm following their syntax correctly but I'm getting errors about the array not being declared in the function parameter, and how my function (minmax) simply cannot be used as a function and I'm beyond confused right now!而且我认为我正确地遵循了他们的语法,但是我收到了有关未在函数参数中声明数组的错误,以及我的函数 (minmax) 根本无法用作函数的错误,我现在非常困惑!

Apologies if the answer is obvious, I'm still new to C++如果答案很明显,我很抱歉,我还是 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;

}

Assuming this is not a homework assignment (which would probably require you to actually roll your own "Min/max" function), this code would suffice:假设这不是家庭作业(这可能需要您实际使用自己的“最小/最大”函数),此代码就足够了:

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());

Or if you need min/max values to be calculated with a single function invocation, you can rewrite those last two lines like this:或者,如果您需要使用单个函数调用来计算最小值/最大值,您可以像这样重写最后两行:

auto minmax = std::minmax_element(values.begin(), values.end());
int min_value = *(minmax.first);
int max_value = *(minmax.second);
#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;
}

Should work, but you should (re)read the C/C++ basics.应该可行,但您应该(重新)阅读 C/C++ 基础知识。

Another solution is to use <vector> and <algorithm> from std C++ lib.另一种解决方案是使用 std C++ lib 中的<vector><algorithm>

Assuming this is not a homework assignment, here are some suggestions for your approach.假设这不是家庭作业,这里有一些关于你的方法的建议。 (otherwise, @Xirema's answer is better approach) (否则,@Xirema 的答案是更好的方法)
3 things to start: 3件事要开始:
1) mm is not defined in your main. 1) mm没有在你的 main.js 中定义。
2) the statement: mm = minmax(numbers); 2) 语句: mm = minmax(numbers); should be moved out of the for loop.应该移出 for 循环。
3) Also, this statement: mm = minmax(numbers); 3) 另外,这个语句: mm = minmax(numbers); Suggests you need to change the function prototype of minmax in a couple of ways.建议您需要通过几种方式更改minmax的函数原型。 See below.见下文。

Replace your minmax function (see comments in code for explanation)替换您的 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
}

Note, This preserves the result of what your original code example would have returned, maximum only.请注意,这将保留原始代码示例将返回的结果,仅最大值。

You need to correct some mistakes:你需要纠正一些错误:

#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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM