Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 部分填充数组C++;_C++_Arrays_Partial - Fatal编程技术网

C++ 部分填充数组C++;

C++ 部分填充数组C++;,c++,arrays,partial,C++,Arrays,Partial,嗨,第一次问,如果我做错了什么,我很抱歉 对于我的家庭作业,我必须创建一个程序,要求用户输入最多20个或更少的整数,它将以相反的顺序输出它们,并给出数组中最大和最小的整数。用户可以通过以0结尾输入少于20个整数。我把大部分都记下来了,但我有几个问题 部分赋值表示,如果用户输入20个整数,它将自动反转顺序,但在我的程序中,用户必须输入21个整数,然后它将反转顺序 当我试图计算最小值时,它会给我一个巨大的负数,比如-76534534 代码如下: #include <iostream> u

嗨,第一次问,如果我做错了什么,我很抱歉

对于我的家庭作业,我必须创建一个程序,要求用户输入最多20个或更少的整数,它将以相反的顺序输出它们,并给出数组中最大和最小的整数。用户可以通过以0结尾输入少于20个整数。我把大部分都记下来了,但我有几个问题

  • 部分赋值表示,如果用户输入20个整数,它将自动反转顺序,但在我的程序中,用户必须输入21个整数,然后它将反转顺序

  • 当我试图计算最小值时,它会给我一个巨大的负数,比如-76534534

  • 代码如下:

    #include <iostream>
    using namespace std;
    const int max_integers = 20;
    void intro( );
    void array(int array[], int size, int& x);
    void reverse(const int array[], int x);
    void high(const int array[], int x);
    void low (const int array[], int x);
    int main( )
    {
        int input[max_integers];
        int x;
    
        intro( );
        array(input, max_integers, x);
        reverse(input, x);
        high(input, x);
        low(input, x);
        return 0;
    }
    
    void intro ( )
    {
        cout << "This program will read in a list of integers and put them into an array." << endl;
        cout << "The maximum number of integers it will read in is 20.  The program will then " << endl;
        cout << "output the numbers in reverse order and give you the largest and smallest " << endl;
        cout << "integers." << endl;
    }
    
    void array (int input[], int size, int& x)
    {
        cout << "Enter a list of integers (0 to end list).  Max number you can enter is " << size << "." << endl;
        int next;
        int index = 0;
        cin >> next;
        while ((next > 0) && (index < size))
        {
            input[index] = next;
            index++;
            cin >> next;
        }
        x = index;
        cout << endl;
    }
    
    void reverse (const int input[], int x)
    {
        cout << "The integers in reverse order are:" << endl;
        for (int index = 0; x > index; x--)
        {
            cout << input[x-1];
            cout << endl;
        }   
    }
    
    void high (const int input[], int x)
    {
        int high = 0;
        for (int index = 0; x > index; x--)
        {
            if (input[x] > high)
            {
                high = input[x];
            }
        }
    
        cout << "The largest value entered is " << high << "." << endl;
    }
    
    void low (const int input[], int x)
    {
        int low = 999999;
        for (int index = 0; x >= index; x--)
        {
            if ((input[x] < low) && (low > 0))
            {
                low = input[x];
            }
        }
    
        cout << "The smallest value entered is " << low << "." << endl;
    }
    
    #包括
    使用名称空间std;
    常量int max_整数=20;
    void intro();
    无效数组(int数组[],int大小,int&x);
    无效反向(常量int数组[],int x);
    无效高(常数int数组[],int x);
    void low(常量int数组[],int x);
    int main()
    {
    整数输入[最大整数];
    int x;
    简介();
    数组(输入,最大整数,x);
    反向(输入,x);
    高(输入,x);
    低(输入,x);
    返回0;
    }
    无效介绍()
    {
    cout1:
    您在
    while
    循环之前请求一个整数,然后循环20次请求一个数字。使用
    do while
    循环解决此问题:

    do {
        cin >> next;
        input[index] = next;
        index++;
    } while ((next > 0) && (index < size))
    

    谢谢,真不敢相信我没想到。
    int input[max_integers] = { };