help changing this program to include more values

so our project is to write a program that takes 3 user input numbers and finds the median using if then else statements which I did and have attached the code for it but now we have to make it include 5 numbers but I think there is an easier way than the way I have done 3
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>

using namespace std;

int main()
{

    float   number1, number2, number3, median;

    cout << "Please enter three numbers:" << endl;
    cin >> number1 >> number2 >> number3;

    if (number1 > number2 && number2 > number3 || number3 > number2 && number2 > number1)
        median=number2;
    else if (number1 > number3 && number3 > number2 || number2 > number3 && number3 > number1)
        median=number3;
    else if (number2 > number1 && number1 > number3 || number3 > number1 && number1 > number2)
        median = number1;
    cout <<"The median of " << number1 << ", " << number2 << ", and " << number3 << " is " << median << "." << endl;


    return 0;
}
Yes there is, use arrays with for loops to do stuff to them. This is much more scalable.

Have a look in the documentation section top left of this page if you are not sure how to use them.

HTH
we have not started doing loops in my class yet
I find it a little irritating that your teachers have given such an assignment, with out showing you all arrays & for loops. They are easy concepts to learn - not using them just encourages really bad code.

The code would also benefit from calling a sort routine - hopefully one day they will ask you to write your own. Even using the STL std::sort algorithm would require at least an array.
So I am right and there is a really easy way to do this but I am going to have to do it the really hard way and come up with the ridiculous amount of possibilities for all 5 number.
Did your teacher explicitly say you couldn't use arrays & for loops?

Maybe you should post the exact text of the assignment.
Assignment 3.2 - Finding median

Use functional decomposition 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 numbers here is the program a sample of the console program output...

Sample screen output 1 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.


Once you have the three-number case working, extend the program to handle five numbers. Be sure to use proper formatting and appropriate prompts in your code. The output should be labeled clearly and formatted neatly.

Input Validation: No input validation is required for this program.

Turn in your source code followed by the program test output, reflecting the median for user input of five numbers similar to the sample screen output given above.
In his lecture he said "using arrays would be easier but we have not got there."
Jumping Juniper Berries - what a pain !!

Use functional decomposition


So he wants you to use functions.

For the 3 number problem, you could read the input and keep track of the min & max values. The median is the other one.

But this won't work for 5 values.

You could use variables to keep track of the position of the original variables. You can also work out the average and compare things to it. It's all a bit of a cluster F, but it is what they want I suppose. It's a very long way round to do sorting.

Use functions to do these things - that's what the assignment requires.

I can't help thinking if they want you to write functions there would be better examples to work with.

Best of luck !!
Thanks for taking a look at I was hoping to not have to go through every possibility.
If I were given this cockamamie assignment, I'd probably do something like the following:
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
#include <iostream>
#include <algorithm>

using namespace std;

int medianOf( int n1, int n2, int  n3, int  n4, int n5 )
{
    if ( n1 > n2 ) std::swap(n1, n2) ;
    if ( n2 > n3 ) std::swap(n2, n3) ;
    if ( n3 > n4 ) std::swap(n3, n4) ;
    if ( n4 > n5 ) std::swap(n4, n5) ;
    // n5 is now the largest value of the 5.

    if ( n1 > n2 ) std::swap(n1, n2) ;
    if ( n2 > n3 ) std::swap(n2, n3) ;
    if ( n3 > n4 ) std::swap(n3, n4) ;
    // n4 is the second largest value of the 5.

    if ( n1 > n2 ) std::swap(n1, n2) ;
    if ( n2 > n3 ) std::swap(n2, n3) ;
    // n3 is the third largest value of the 5.
    // we can stop here, because the third largest value is the median.

    return n3 ;
}


int main()
{
    float n1, n2, n3, n4, n5 ;
 
    std::cout << "Please enter five numbers: " ;
    std::cin >> n1 >> n2 >> n3 >> n4 >> n5 ;

    std::cout << "median is " << medianOf(n1, n2, n3, n4, n5) ;
}
I assume this would be an array?
No array involved.

With std::array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <array>
#include <iostream>
#include <algorithm>

int main()
{
    std::array<float, 5> num ;

    std::cout << "Please enter five numbers: " ;

    for ( unsigned i=0; i<5; ++i )
        std::cin >> num[i] ;

    std::sort(num.begin(), num.end()) ;

    std::cout << "median is " << num[2] ;
}


With a c-style array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <algorithm>

int main()
{
    float num[5] ;

    std::cout << "Please enter five numbers: " ;

    for ( unsigned i=0; i<5; ++i )
        std::cin >> num[i] ;

    std::sort(num, num+5) ;

    std::cout << "median is " << num[2] ;
}
Last edited on
Okay then what is std::swap?
Wow that is way more simple
Okay then what is std::swap?


swap is exactly what it sounds like. It swaps the values of the two variables that are fed to it.


if ( n1 > n2 ) std::swap(n1, n2) ;

could be rewritten as:

1
2
3
4
5
6
    if ( n1 > n2 )
    {
        int temp = n1 ;
        n1 = n2 ;
        n2 = temp ;
    }


gotcha thank you.
Topic archived. No new replies allowed.