954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ help (arrays)

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.

giggity
Newbie Poster
6 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

giggity
Newbie Poster
6 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 
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];
}
}

giggity
Newbie Poster
6 posts since Mar 2011
Reputation Points: 10
Solved Threads: 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".

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
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

giggity
Newbie Poster
6 posts since Mar 2011
Reputation Points: 10
Solved Threads: 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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
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 ";
}

giggity
Newbie Poster
6 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

Give it a try and see how it works.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
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.

giggity
Newbie Poster
6 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: