0

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.

2
Contributors
9
Replies
11
Views
6 Years
Discussion Span
Last Post by giggity
0
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 by Nick Evan: Fixed formatting

0
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 by Nick Evan: Fixed formatting

0

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 by Narue: n/a

0

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 by giggity: to expand on the question

0

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.

0

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 by Nick Evan: Fixed formatting

0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.