Question

Hi first time asking so sorry if I do anything wrong

For my homework assignment I have to create a program that will ask the user to input up to 20 or less integers and it will output them in reverse order and give the largest and smallest integer in the array. The user can enter less than 20 integers by terminating it with 0. I have most of it down, but I have a few questions.

  1. Part of the assignment states that if the user enters 20 integers it will automatically reverse the order, but in my program, the user has to enter 21 integers and then it will reverse it.

  2. When I try to figure out smallest value, it gives me a huge negative number like -76534534.

Here is the code:

#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;
}
Was it helpful?

Solution

1: You ask for a integer before the while loop and then loop 20 times asking for a number. Solve this by using a do-while loop:

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

2: Your values in the array are uninitialized if the user enters less then 20 numbers. This is undefined behavior. You can fix this by default initializing:

int input[max_integers] = { };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top