Hello everyone need help with a problem!

Hey guys, I am extremely new to c++ so if you think this is a stupid question and are going to rant at me please don't reply. My teacher has assigned us with a project to write a program that will let the user input 5 numbers into a data set and the program will find the mean, median, maximum and minimum of the 5 numbers that the user inputs into the program. I am having trouble with the median part of the program, I know that I can use an array, but we haven't learned that yet and our teacher told us specifically to not use them and all we can use is conditional statements to write the program basically if and else only. I really want to learn how to program so please help me out guys and thanks for reading!

TL:DR I need help writing a program that will find the median of 5 numbers using only conditional statements.

Also if you have any comments about my code so far feel free to comment and help me to improve it and make it more elegant.

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
  //Joshua Harlan - Comp Sci 1 - Assignment 2

#include <iostream>
using namespace std;

int main(){
	int input1, input2, input3, input4, input5;
	cout << "Hello, please enter your 5 favorite numbers. Press the enter key after each number. ";

	cin >> input1;
	int max = input1;
	int min = input1;

	cin >> input2;
	if (input2 > max){
		max = input2;
	}
	else{
		min = input2;
	}

	cin >> input3;
	if (input3 > max){
		max = input3;
	}
	else if (input3 < min){
		min = input3;
	}

	cin >> input4;
	if (input4 > max){
		max = input4;
	}
	else if (input4 < min){
		min = input4;
	}

	cin >> input5;
	if (input5 > max){
		max = input5;
	}
	else if (input5 < min){
		min = input5;
	}
	cout << "Minimum number in data set: " << min << endl;
	cout << "Maximum number in data set: " << max << endl;

	int mean = (input1 + input2 + input3 + input4 + input5) / 5;
	cout << "Mean of data set: " << mean << endl;

	int median;
		system("pause");

> I am having trouble with the median part of the program

The median of a finite list of numbers can be found by arranging all the observations from lowest value to highest value and picking the middle one (e.g., the median of {3, 3, 5, 9, 11} is 5). https://en.wikipedia.org/wiki/Median


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
#include <iostream>
#include <utility> // for std::swap

int main()
{
    int a, b, c, d, e ;

    std::cin >> a >> b ;
    if( a > b ) std::swap(a,b) ; // post-condition a <= b
    // note: std::swap(a,b) ; may be written as { int temp = a ; a = b ; b = temp ; }

    std::cin >> c ;
    if( b > c ) std::swap(b,c) ; // post-condition b <= c
    if( a > b ) std::swap(a,b) ; // post-condition a <= b, b <= c

    std::cin >> d ;
    if( c > d ) std::swap(c,d) ; // post-condition c <= d
    if( b > c ) std::swap(b,c) ; // post-condition b <= c, c <= d
    if( a > b ) std::swap(a,b) ; // post-condition a <= b, b <= c, c <= d

    std::cin >> e ;
    if( d > e ) std::swap(d,e) ; // post-condition d <= e
    if( c > d ) std::swap(c,d) ; // post-condition c <= d, d <= e
    if( b > c ) std::swap(b,c) ; // post-condition b <= c, c <= d, d <= e
    if( a > b ) std::swap(a,b) ; // post-condition a <= b, b <= c, c <= d, d <= e

    std::cout << "numbers: " << a << ' ' << b << ' ' << c << ' ' << d << ' ' << e << '\n'
              << "    min: " << a << '\n'
              << "    max: " << e << '\n'
              << " median: " << c << '\n'
              << "average: " << (a+b+c+d+e) / 5.0 << '\n' ;
}
Topic archived. No new replies allowed.