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.

Recommended Answers

All 9 Replies

Done. Show me yours and I'll show you mine.

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];
    }
}
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];
    }
}

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".

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

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 ";
}

Give it a try and see how it works.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.