homework help

Hi everyone,

I have written this code for a problem in my book. It works but I think there must be a better way to write the code in my function findHighest and findLowest. Any suggestions? At this point I am only up to the functions chapter.
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
107
108
109
110
//This program takes five scores, drops the highest and lowest score, then
// provides the average of the three remaining scores.

#include <iostream>
#include <iomanip>
using namespace std;

void getJudgeData (double &);					//prototype for function getJudgeData
void calcScore (double, double, double, double, double);	//prototype for function calcScore
double findLowest (double, double, double, double, double);	//prototype for function findLowest
double findHighest (double, double, double, double, double);	//prototype for function findHighest

int main ()
{
	double judge1, judge2, judge3, judge4, judge5;		//variables to hold judges scores
	
	getJudgeData (judge1);				        //function call passing ref variable
	getJudgeData (judge2);					//function call passing ref variable
	getJudgeData (judge3);					//function call passing ref variable
	getJudgeData (judge4);					//function call passing ref variable
	getJudgeData (judge5);					//function call passing ref variable

	calcScore (judge1, judge2, judge3, judge4, judge5);	//function call passing judges scores

	return 0;
}

//*******************************************************
//Function definition - This function asks for the	*
//judges score and validates user input, then changes	*
//the judge variable.					*
//*******************************************************

void getJudgeData (double &refJudge)
{
	cout << "Enter the judges score: \n";
	cin >> refJudge;
	while (refJudge < 0 || refJudge > 10)
	{
		cout << "Error! Enter a number between 0 and 10: \n";
		cin >> refJudge;
	}
}

//*******************************************************
// Function definition - Function calcScore claculates	*
// the average score after the lowest and highest score	*
//has been dropped.					*
//*******************************************************

void calcScore (double j1, double j2, double j3, double j4, double j5)
{
	double average;

	double low = findLowest (j1, j2, j3, j4, j5);
	double high = findHighest (j1, j2, j3, j4, j5);

	average = ((j1 + j2 + j3 + j4 + j5) - (low + high))/3;

	cout << fixed << showpoint << setprecision(1);
	cout << "Average Score : " << average << endl;


}

//*******************************************************
//Function definition - Function findLowest finds the	*
// lowest score and returns lowest to calScore.		*
//*******************************************************

double findLowest (double s1, double s2, double s3, double s4, double s5)
{
	double lowest;

	if ( s1 < s2 && s1 < s3 && s1 < s4 && s1 < s5)
		lowest = s1;
	else if ( s2 < s1 && s2 < s3 && s2 < s4 && s2 < s5)
		lowest = s2;
	else if ( s3 < s1 && s3 < s2 && s3 < s4 && s3 < s5)
		lowest = s3;
	else if ( s4 < s1 && s4 < s2 && s4 < s3 && s4 < s5)
		lowest = s4;
	else
		lowest = s5;
	
	return lowest;
}

//*******************************************************
//Function definition - Function findHighest finds the	*
//highest score and returns highest to calcScore.	*
//*******************************************************

double findHighest (double s1, double s2, double s3, double s4, double s5)
{
	double highest;

	if ( s1 > s2 && s1 > s3 && s1 > s4 && s1 > s5)
		highest = s1;
	else if ( s2 > s1 && s2 > s3 && s2 > s4 && s2 > s5)
		highest = s2;
	else if ( s3 > s1 && s3 > s2 && s3 > s4 && s3 > s5)
		highest = s3;
	else if ( s4 > s1 && s4 > s2 && s4 > s3 && s4 > s5)
		highest = s4;
	else
		highest = s5;
	
	return highest;
}
Hi! Though you forgot to write what exactly problem you tried to solve, I suppose it was to find max/min among 5 elements.

I would say you need at once read a chapter on "loops" or "cycles" in your book. You could not do in the same way for 1000 elements.

I do not know what book you are reading - I can propose Kernigan and Ritchie's one.

Also I dare to recommend you first problems at my website:

http://codeabbey.com/index/task_list/beginners-problems

Many of them are dedicated to looping. By the way exactly your kind of task is this:

http://codeabbey.com/index/task_view/maximum-of-array

The good news are that your coding style is neat enough, which is really positive. Keep on!
Last edited on
rodiongork,

Thanks for the reply. The book is Starting Out With C++ Tony Gaddis. I am actually taking an online class at the local community college.

I know I can do this with a for loop and an array but that is the next chapter.

Is it possible to use a loop without an array to solve the problem. For the life of me I cannot visualize the logic. Understand I am not asking for the code just a direction.
Hi!

Yes, array is not needed, while loop is.

See, I'll try to express it in some "meta-language":

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# initial value for min, should be greater than anything we will input
min = 10000000

# repetition counter
n = 5

while n > 0
    # read next value
    value = input()
    # assign it to min holder if it is smaller than current min
    if value < min then
        min = value
    end if
    decrement n
end while


You only need to convert it to C++ (see syntax for "while" loop or "for" loop) and add simultaneous calculation for max.
Last edited on
Here check my code out! This is how I would of solved your problem, how I would've coded it!

It's never good to make a forced limitation on something unless it makes sense... For example in your program you limited the number of judge scores to be inputted to 5, then wrote your code to work with just 5 values... It should be only limited to memory and hard drive space!

See the 'Zero One Infinity Rule': http://en.wikipedia.org/wiki/Zero_one_infinity_rule

My code which is a modified version of your code follows the rule, and you can input however many judge scores you want to calculate... The score itself on the other hand makes sense to be only between 0 and 10 as a judge can only choose between those numbers!

Don't fear arrays! You can always use linked lists if you hate arrays!! lol :)

However what I used, vector arrays, are very nice to work with arrays as shown! I think you might enjoy them! (I like to use custom object types with vectors, and with sorting you can sort your whole objects around with data attached by a certain variable of the class object instance, its pretty cool!)

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
107
108
109
110
111
112
113
114
115
116
117
//Modified by SteveCodeMan to help MikaShane
//This program takes ANY number of scores, drops the highest and lowest score, then
//provides the average of the remaining scores.

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
using namespace std;

double getJudgeData();
void calcScore(vector<double> &scores);
double findLowestOrHighest(vector<double> &scores, bool FindLowest);
bool HighestFirstRatherThanLowest;	//How values will be sorted

struct LowestHighestCheck
{
	bool operator()(double &a, double &b)
	{
		bool returnValue;

		if (a < b)
			returnValue = true;
		else if (a > b)
			returnValue = false;
		else
			returnValue = false; //a == b

		if(HighestFirstRatherThanLowest)
			returnValue ^= 1; //flip value

		return returnValue;
    }
};

int main()
{
	vector<double>scoresJudged;
	int numScoresJudged;

	cout << "Enter number of scores judged: \n";
	cin >> numScoresJudged;

	for(int i = 0; i < numScoresJudged; i++)
	{
		scoresJudged.push_back(getJudgeData());
	}

	calcScore (scoresJudged);	//function call passing reference to judges scores vector array
	return 0;
}

//*******************************************************
//Function definition - This function asks for the	*
//judges score and validates user input, then returns	*
//the judge variable.					*
//*******************************************************

double getJudgeData ()
{
	double scoreJudge;

	cout << "Enter the judges score: \n";
	cin >> scoreJudge;
	while (scoreJudge < 0 || scoreJudge > 10)
	{
		cout << "Error! Enter a number between 0 and 10: \n";
		cin >> scoreJudge;
	}

	return scoreJudge;
}

//*******************************************************
// Function definition - Function calcScore calculates	*
// the average score after the lowest and highest score	*
//have been dropped.					*
//*******************************************************

void calcScore(vector<double> &scores)
{
	double average = 0, sum = 0;

	double low = findLowestOrHighest(scores, true);
	double high = findLowestOrHighest(scores, false);

	for(vector<double>::iterator i = scores.begin(); i != scores.end(); ++i)
	{
		sum += *i;
	}

	//sum of numbers divided by how many numbers there! (with low && high taken out of the equation of course, since that's how you want it)
	average = (sum - (low + high)) / scores.size();

	cout << fixed << showpoint << setprecision(1);
	cout << "Lowest: " << low << " Highest: " << high << " Average Score : " << average << "\n";
}

//*******************************************************
//Function definition - Function findLowestOrHighest finds the	*
// lowest / highest score and returns it to calScore.		*
//*******************************************************

double findLowestOrHighest(vector<double> &scores, bool FindLowest)
{
	double LowestOrHighest;

	if(FindLowest)
		HighestFirstRatherThanLowest = false;
	else
		HighestFirstRatherThanLowest = true;

	sort(scores.begin(), scores.end(), LowestHighestCheck());
	
	LowestOrHighest = scores.front();
	return LowestOrHighest;
}
Last edited on
Topic archived. No new replies allowed.