Program for Finding Median of 5 Numbers

closed account (NU9yT05o)
Hello, I was wondering how to make it so that my code can handle 5 numbers instead of 3. I would like a hint but nothing overboard. I can't use arrays or loops, only if statements.

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
  /* this program is going to find the mean of 3 numbers. Preparations already made for the program
to be expanded to 5 numbers.*/
#include <iostream>
using namespace std;

int main()
{
    //declare the variables
    double number1,
           number2,
           number3,
           number4,
           number5,
           median;

    //Explain the program and prompt for input
    cout << "Please input three numbers and this program will find the ";
    cout << "mean of those numbers.\n\n";

    cout << "First Number: ";
    cin >> number1;
    cout << "Second Number: ";
    cin >> number2;
    cout << "Third Number: ";
    cin >> number3;
    /*cout << "Fourth Number: ";
    cin >> number4;
    cout << "Fifth Number: ";
    cin >> number5;*/


    //using if else statements find the median
    if (number2 > number1 && number1 > number3 || number3 > number1 && number1 > number2)
        median = number1;
    else if (number1 > number2 && number2 > number3 || number3 > number2 && number2 > number1)
        median=number2;
    else if (number1 > number3 && number3 > number2 || number2 > number3 && number3 > number1)
        median=number3;


    cout << "\nThe median of " << number1 <<", " << number2 << ", and " << number3 /*<< ", " << number4 << ", and " << number5 */<< " is... "<< median;



    return (0);
}
un-comment all the parts about number4,number5
Add else if statements for each.

I think you did this already..
closed account (NU9yT05o)
I guess I'm struggling on the last else if statements...
closed account (2UD8vCM9)
You sure you're not allowed to use arrays or loops?

Seems like you'll have to do a ridiculous amount of comparisons.

Unless you sort the numbers, but then you still have to do a lot of comparisons for that..
closed account (NU9yT05o)
We haven't covered them yet in class so no. He did mention that arrays make this a breeze. Would it be easier to have them input three numbers, then find the median of those numbers. Have them input two more numbers and find the median of that? That's the only way that I can see making it easier on me.
closed account (2UD8vCM9)
No, because that's not how median works. Are you sure he didn't want you to find the mean?

I just can't imagine he'd assign something like this without teaching arrays. Yeah it's easier with arrays, but it's something that should only be solved using arrays/for loops.
closed account (2UD8vCM9)
I didn't really do much testing, but something like this might work.

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
using namespace std;

int main()
{
	//declare the variables
	double number1,
		number2,
		number3,
		number4,
		number5,
		median;

	//Explain the program and prompt for input
	cout << "Please input three numbers and this program will find the ";
	cout << "mean of those numbers.\n\n";

	cout << "First Number: ";
	cin >> number1;
	cout << "Second Number: ";
	cin >> number2;
	cout << "Third Number: ";
	cin >> number3;
	cout << "Fourth Number: ";
	cin >> number4;
	cout << "Fifth Number: ";
	cin >> number5;


	if (number1 > number2) //Swap first slot if necessary
		swap(number1, number2);
	if (number1 > number3)
		swap(number1, number3);
	if (number1 > number4)
		swap(number1, number4);
	if (number1 > number5)
		swap(number1, number5);

	//Swap second slot if necessary
	if (number2 > number3)
		swap(number2, number3);
	if (number2 > number4)
		swap(number4, number4);
	if (number2 > number5)
		swap(number2, number5);

	//Swap third slot if necessary
	if (number3 > number4)
		swap(number3, number4);
	if (number3 > number5)
		swap(number3, number5);

	//Swap fourth slot if necessary
	if (number4 > number5)
		swap(number4, number5);


	cout << "\nThe median of " << number1 << ", " << number2 << ", and " << number3 << ", " << number4 << ", and " << number5  << " is... " << number3;

	system("pause");

	return (0);
}


It sorts them without using a loop.
closed account (NU9yT05o)
Finding median

Use selection control structures to write a C++ program that determines the median of three input numbers. The median is the middle number when the three numbers are arranged in order by size. However, the user can input the values in any order, so your program must determine which value is between the other two.

For example, given an appropriate prompt, if the user enters three "unique" real numbers here is a sample of the console program output...

Sample screen output using Code:Blocks:

Please enter three numbers:
41.52
27.18
96.03

The median of 41.52, 27.18, and 96.03 is 41.52.

Process returned 0 (0x0) execution time : 10.082 s
Press any key to continue.

10 points possible if you have the above portion of the program working showing the correct median number given three real number input values.

Once you have the three-number case working, extend the program to handle five numbers. An additional 5 extra credit points will be provided if you are able to show the correct median number given five real number input values. Note: You may develop and implement your algorithm incrementally (i.e., with three numbers input and then the remaining two numbers) or choose to write your algorithm based on five input numbers


That is the assignment he sent everyone through an email.
closed account (NU9yT05o)
Yeah so far it's working great. The only thing I saw in your code would be this line

if (number2 > number4)
swap(number4, number4);

That would be changed to

if (number2 > number4)
swap(number2, number4);

Right?
closed account (2UD8vCM9)
Yeah sorry, copy/paste fail on my part. XD
closed account (NU9yT05o)
All good. Thanks again. So it would take wayyyyyy too many lines of code to get my program to perform the same task for 5 numbers as it did for 3?
closed account (2UD8vCM9)
if I understand right, you would have 5 if/else if's per number so it would be 25 if/elseif blocks.
closed account (NU9yT05o)
*Sigh* Thanks again man. Just so I am clear on something is the "swap()" something I will be learning early on, or...?
closed account (2UD8vCM9)
Your teacher should teach you that. All it does is swap the value of two variables.
I think you'll find it easier to reason about if you don't try to do everything in large if conditions. For instance, your median of three could be rewritten like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    if (number1 > number2)
    {
        if (number1 > number3)
        {
            if (number2 > number3)
                median = number2;
            else
                median = number3;
        }
        else
            median = number1;
    }
    else
    {
        if (number2 > number3)
        {
            if (number3 > number1)
                median = number1;
            else
                median = number3;
        }
        else
            median = number2;
    }


Clearly that's going to be ugly for a five variable attempt. ;)

What you might want to do is just count the number of other variables that are less than a particular variable. Assuming no duplicates are entered, the median for a group of five variables will be the variable with two numbers less than it.

Here's how that would look for the median-of-three case:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

int main()
{
    double a, b, c;

    std::cout << "Please enter 3 numbers.\n> ";
    std::cin >> a >> b >> c;

    unsigned less_than_a = (b <= a) + (c <= a);
    unsigned less_than_b = (a <= b) + (c <= b);
    unsigned less_than_c = (a <= c) + (b <= c);

    double median;
    if (less_than_a == 1)
        median = a;
    else if (less_than_b == 1)
        median = b;
    else
        median = c;

    std::cout << "The median of " << a << ", " << b << " and " << c << " is " << median << '\n';
}


It doesn't get too much worse in the median-of-five version.

Last edited on
closed account (NU9yT05o)
Yeah I tried the nested "if else" statements and it was crazy hard to follow. I'm still learning about this topic so my code wasn't going to be the best. So I looked unsigned and is that necessary? The only thing that I could think of would be for the == 1. I have to admit that I like your version better than mine.
Topic archived. No new replies allowed.