Write a program that allows the user to enter a series of positive integers one at a time and in increasing order (using -1 as a sentinel value), and then computes and displays the median value of the data entered, as well as the number of data values entered. You’ll find that to compute the median, you’ll need to store the input values in an array.

A sample run should look like:

Enter positive integers one at a time increasing order. Enter -1 to indicate when you are done.

Number? -2
Numbers must be positive. Redo that last one.
Number? 5
Number? 23
Number? 12
Numbers must increase. Redo that last one.
Number? 42
Number? 64
Number? -1

You entered 4 positive increasing values.
The median of those values is 32.5.

int main()
{
    const int size = 100;
    int n[100];
    for ( int i=0; i < size ; i++ )
        n[i]=0;
    int i=0;
    while ( n[i] != -1 && i < size )
    {
        cout << "Number? ";
        cin>>n[i];
    }
}

Edited 3 Years Ago by Nick Evan: Fixed formatting

int main()
{
    const int size = 100;
    int n[100];
    for ( int i=0; i < size ; i++ )
        n[i]=0;
    int i=0;
    while ( n[i] != -1 && i < size )
    {
        cout << "Number? ";
        cin>>n[i];
    }
}

Edited 3 Years Ago by Nick Evan: Fixed formatting

Clarification: show me your completed code and I'll show you mine. Or ask a specific question that can't be interpreted as "do it for me".

Edited 5 Years Ago by Narue: n/a

Clarification: show me your completed code and I'll show you mine. Or ask a specific question that can't be interpreted as "do it for me".

How do I make it so that the coumputer would tell the user if he/she entered a number that was not greater than the previous number entered? Please don't just tell me the code because I would like to know how to do it

Edited 5 Years Ago by giggity: to expand on the question

How do I make it so that the coumputer would tell the user if he/she entered a number that was not greater than the previous number entered?

That's more like it! You know what the last number entered was, because it's sitting in n[i - 1] . Just make compare that with the current number, or if i == 0 , skip the test because it's the first number in the list.

So the loop should look something like this?:

while ( n[i] != -1 && i < size )
{
    cout << "Number? ";
    cin >> n[i];
    if ( n[i] <= n[i-1] )
        cout << "Number must increase. Redo that last one ";
}

Edited 3 Years Ago by Nick Evan: Fixed formatting

Give it a try and see how it works.

It works, but now for some reason when I enter -1, it won't end the loop.

This article has been dead for over six months. Start a new discussion instead.