Program to print a median.

Hey, everyone. So my program is supposed to print the median of five numbers inserted and upon compiling and running it, it prints all five "if" statements regarding which one would be the median. I'm sure there's a much easier way than what I'm trying and I'm sure that my method makes no sense, but it's all I came up with.

Thanks in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  #include <iostream>
using namespace std;

int main()
{
	int number1, number2, number3, number4, number5;
	
	cout << "Hey again, user! More work I need help with, your grading C++ students' programs can wait, this is urgent!." << endl;
	cout << "I need a median of five consecutive integers. So pick five of your favorite numbers under 100 say, and insert them one at a time followed by enter." << endl;
	cin >> number1;
	
	cout << "Cool, I've got your first integer. Now gimme your second one." << endl;
	cin >> number2;
	
	cout << "Great. Third now." << endl;
	cin >> number3;
	
	cout << "Fourth. (You're getting there I promise.)" << endl;
	cin >> number4;
	
	cout << "Dig deep! One more integer!" << endl;
	cin >> number5;
	
	if(number1 > number2, number3 && number1 < number4, number5){
		cout << "Cool, thanks! The median is " << number1 << endl;
	}
	
	if(number2 > number1, number3 && number2 < number4, number5){
		cout << "Cool, thanks! The median is " << number2 << endl;
	}
	
	if(number3 > number1, number2 && number3 < number4, number5){
		cout << "Cool, thanks! The median is " << number3 << endl;
	}
	
	if(number4 > number1, number2 && number4 << number3, number5){
		cout << "Cool thanks! The median is " << number4 << endl;
	}
	
	if (number5 > number1, number2 && number5 < number3, number4){
		cout << "Cool thanks! The median is " << number5 << endl;
	}
	
	
	
	
}
if(number1 > number2, number3 && number1 < number4, number5){

Coma operator doesnt work that way. Im not sure what you want it to do, can you tell me in words?
So in the if statement, if number1 is greater than number2 and number3 and at the same time is less than number4 and number5, than print out number1 as the median. But every time I run the code it prints the cout 5 times. Plus there are so many combinations for the numbers being less than or greater than the others
Have you studied vectors or arrays yet?

We've studied arrays very briefly and started functions a few days ago. I assumed those had something to do with it, but I assumed I could calculate a freaking median fairly simply :/
Since finding the median requires that the elements be sorted so you can pick the middle element(s) an array would probably make this much easier.

https://en.wikipedia.org/wiki/Median

I thought arrays had to have the numbers already specified in the program or scope? I wasn't aware that inputted values could be used in an array
I thought arrays had to have the numbers already specified in the program or scope?

No, the sizes of arrays must either be known at compile time or you must use dynamic memory allocation.

However the elements of an array can be populated during program execution.

Last edited on
Oh okay, gotcha. I'll go look at stuff on arrays and see if it helps. Thank you
Topic archived. No new replies allowed.