Could use some feedback/help with my code

So I was supposed to make a program that rates ski jumping. User inputs the length of the jump and the scores that 5 different referees gave him for it - and "the final score" is jump length * 0.9 + average score that the judges gave him, but I have to remove the highest and lowest scores from the judges and calculate the average from the remaining 3.

Not sure whether this was the way to do it, so could really use some feedback / advice. (basically i tried to reorganize the given values in ascending order and removed 1st and 5th value and divided by 3)

(English is not my native language and this is my first post on the forums, so apologies upfront for the mistakes you'll find)

Thank you!


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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  #include <iostream>
using namespace std;

void sort(double &a, double &b, double &c, double &d, double &e)
{
	if (a > b)
	{
		double tmp = a;
		a = b;
		b = tmp;
	}
	if (a > c)
	{
		double tmp = a;
		a = c;
		c = tmp;
	}
	if (a > d)
	{
		double tmp = a;
		a = d;
		d = tmp;
	}
	if (a > e)
	{
		double tmp = a;
		a = e;
		e = tmp;
	}
	if (b > c)
	{
		double tmp = b;
		b = c;
		c = tmp;
	}
	if (b > d)
	{
		double tmp = b;
		b = d;
		d = tmp;
	}
	if (b > e)
	{
		double tmp = b;
		b = e;
		e = tmp;
	}
	if (c > d)
	{
		double tmp = c;
		c = d;
		d = tmp;
	}
	if (c > e)
	{
		double tmp = c;
		c = e;
		e = tmp;
	}
	if (d > e)
	{
		double tmp = d;
		d = e;
		e = tmp;
	}

	return;
}

int main()
{
	double ref1, ref2, ref3, ref4, ref5, length, score;
	char exit;

	do
	{
		cout << "Enter the length of the jump: ";
		cin >> length;
		cout << "Ref 1 score: ";
		cin >> ref1;
		cout << "Ref 2 score: ";
		cin >> ref2;
		cout << "Ref 3 score: ";
		cin >> ref3;
		cout << "Ref 4 score: ";
		cin >> ref4;
		cout << "Ref 5 score: ";
		cin >> ref5;

		sort(ref1, ref2, ref3, ref4, ref5);
		score = (ref2 + ref3 + ref4) / 3;	

		cout << "Total score for the jump is: " << score + length*0.9 << endl;
		cout << "Do you want to run this again? (Y/N):";

		cin >> exit;
		if (exit == 'y' || exit == 'Y')
			system("cls");

	} while (exit == 'Y' || exit == 'y');


	system("pause");
	return 0;
}
The one thing not to do is spend time sorting. The average without the outliers is just
(total - smallest - largest) / ( N - 2 )
where N-2 is 3 in this instance.

You can keep a running tally of smallest, largest and total as you enter the scores.

(It might also make for less repetitive code if you either did the input in a loop or asked for all scores in one go, simply separated by spaces.)

> basically i tried to reorganize the given values in ascending order and removed 1st and 5th value and divided by 3

That is fine.

This is another way of doing the same thing:

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

int main()
{
    int ref1 = 5, ref2 = 7, ref3 = 4, ref4 = 7, ref5 = 6 ;

    const int total5 = ref1 + ref2 + ref3 + ref4 + ref5 ;

    // total after removing one high score and one low score
    // http://en.cppreference.com/w/cpp/algorithm/max
    // http://en.cppreference.com/w/cpp/algorithm/min
    const int total3 = total5 - std::max( { ref1, ref2, ref3, ref4, ref5 } )
                              - std::min( { ref1, ref2, ref3, ref4, ref5 } ) ;
    // note: also see http://en.cppreference.com/w/cpp/algorithm/minmax

    const double average = total3 / 3.0 ;

    std::cout << total5 << ' ' << total3 << '\n'  // 29 18
              << average << '\n' ; // 6
}
Those comments really helped, as I thought - I wasted a lot of time there sorting those values. Thanks alot for both of you!
Topic archived. No new replies allowed.