Help with arrays!

The goal of this program is to display the average of five scores without the highest and lowest. I keep encountering Complier Error C2664 on lines 20-25 and that I must initialize "double &score". Any help would be greatly appreciated.

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
#include <iostream>
#include <iomanip>
using namespace std;


void getScore(double &score, int, double);
double calcAverage(double, double, double);
double findLowest(double scoreArray[5]);
double findHighest(double scoreArray[5]);

int main()
{
	
	double &score;
	double total;
	int i;



	getScore(&score, i, total);
	getScore(&score, i, total);
	getScore(&score, i, total);
	getScore(&score, i, total);
	getScore(&score, i, total);
	 
	return 0;
}


void getScore( double &score , int i, double total)
{
	
	cout << "Enter a judge score\n";

	cin >> score;

	while (score < 0 || score > 10)
	{
		cout << "Please enter a valid judge score that is between 0 to 10\n";
		cin >> score;
	}
	
}
double findHighest(double scoreArray[5]) {
	int count;
	double highest;
	highest = scoreArray[0];
	for (count = 1; count < 5; count++)
	{
		if (scoreArray[count] > highest)
			highest = scoreArray[count];
	}
	return highest;
}
double findLowest(double scoreArray[5]) {
	int count;
	double lowest;
	lowest = scoreArray[0];
	for (count = 1; count < 5; count++)
	{
		if (scoreArray[count] > lowest)
			lowest = scoreArray[count];
	}
	return lowest;
}
double calcAverage(double scoreArray[5], double lowest, double highest) {
	double avg, sum = 0;
	int total;
	for (total = 0; total < 5; total++)
		sum += scoreArray[5];

	avg = (sum - (highest + lowest)) / 3;

	cout << "Your score is " << avg << endl;
	return avg;
}
Last edited on
Remove the & from 'size' from line 13 to 25.

& is not a valid character that can be used to name an identifier, If you meant it to be a variable name.
But I know you meant to pass by reference.

The only place where you need to mention & to pass a variable by reference is while defining the function.
i.e this: void getScore( double &score , int i, double total)
That's all. Everywhere else you use the regular variable name.

When you mention & the variable inside the function points to the original variable, hence not copying the variable, and also remember that you can modify the original variable when you pass by reference.
1
2
3
4
5
	getScore(&score, i, total);
	getScore(&score, i, total);
	getScore(&score, i, total);
	getScore(&score, i, total);
	getScore(&score, i, total);


should be
1
2
3
4
5
	getScore(score, i, total);
	getScore(score, i, total);
	getScore(score, i, total);
	getScore(score, i, total);
	getScore(score, i, total);

Like I had said.

Also why did you change void getScore(double &score, int, double); to void getScore(double * const &score, int, double);

void getScore(double &score, int, double); is correct
Last edited on
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
#include <iostream>
#include <iomanip>
using namespace std;


void getScore(double &score, int, double);
double calcAverage(double, double, double);
double findLowest(double scoreArray[5]);
double findHighest(double scoreArray[5]);

int main()
{
	double lowest, score, lowest, highest, scoreArray[5];
	
	double total= 0;
	int i = 0;



	getScore(score, i, total);
	getScore(score, i, total);
	getScore(score, i, total);
	getScore(score, i, total);
	getScore(score, i, total);
	calcAverage(scoreArray[5], lowest, highest);
	return 0;
}


void getScore( double &score , int i, double total)
{
	
	cout << "Enter a judge score\n";

	cin >> score;

	while (score < 0 || score > 10)
	{
		cout << "Please enter a valid judge score that is between 0 to 10\n";
		cin >> score;
	}
	
}
double findHighest(double scoreArray[5]) {
	int count;
	double highest;
	highest = scoreArray[0];
	for (count = 1; count < 5; count++)
	{
		if (scoreArray[count] > highest)
			highest = scoreArray[count];
	}
	return highest;
}
double findLowest(double scoreArray[5]) {
	int count;
	double lowest;
	lowest = scoreArray[0];
	for (count = 1; count < 5; count++)
	{
		if (scoreArray[count] > lowest)
			lowest = scoreArray[count];
	}
	return lowest;
}
double calcAverage(double scoreArray[5], double lowest, double highest) {
	double avg, sum = 0;
	int total;
	for (total = 0; total < 5; total++)
		sum += scoreArray[5];

	avg = (sum - (highest + lowest)) / 3;

	cout << "Your score is " << avg << endl;
	return avg;
}



I Believe I fixed my first issue and this is where I am at currently. I now only get one compiler error #C2086 on line 13.

Last edited on
I really appreciate all the help Nwb.
"C2086" is not something that everyone has memorized. The meaning is "redefined identifier".

You've defined lowest twice on line 13.

And getScore should just return the score.
1
2
3
4
5
    double getScore() {
        double score = 0;
        ...
        return score;
    }


Presumably you mean to call it in a loop, something like:
1
2
    for (int i = 0; i < 5; i++)
        a[i] = getScore();


And then when you pass the array, don't include the square brackets and the size, just the array name.
double lowest, score, lowest, highest, scoreArray[5]; should be double score, lowest, highest, scoreArray[5];

You had defined lowest twice.

Also why does getScore take 'i' and 'total'? I think you meant to pass the array scoreArray and i is the index. Then the cin statement should be cin >> scoreArray[i] or just assign score at the end and initialize score in the function.

In calcAverage why are you taking highest and lowest and what is highest and lowest and where are they assigned? Average should be sum by number of values.. right?





Last edited on
I think I have addressed the issues afore mentioned. I no longer have compiler errors but I believe I have logic errors somewhere. When I execute the program it displays the input validation error after the fifth score is entered.
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
#include <iostream>
#include <iomanip>
using namespace std;


double getScore(double &score, int, double);
double calcAverage(double, double, double);
double findLowest(double scoreArray[5]);
double findHighest(double scoreArray[5]);

int main()
{
	double  score, lowest = 0, highest = 0, scoreArray[5];
	
	double total= 0;
	int i = 0;
	getScore(score, i, total);
	getScore(score, i, total);
	getScore(score, i, total);
	getScore(score, i, total);
	getScore(score, i, total);
	
	cout << "Your score is: " << calcAverage(scoreArray[5], lowest, highest) << endl;
	return 0;
}


double getScore( double &score , int i, double total)
{
	double scoreArray[5];

	for (int i = 0; i < 5; ++i)
	{
		cout << "Please enter a judge score: " << endl;
		cin >> scoreArray[i];

		total += scoreArray[i];
		return score;
	}

	

	if (score < 0 || score > 10)
	{
		cout << "Please enter a valid judge score that is between 0 to 10\n";
		cin >> score;
	}
	
}
double calcAverage(double, double, double)
{
	return 0.0;
}
double findHighest(double scoreArray[5]) {
	int count;
	double highest ;
	highest = scoreArray[0];
	for (count = 1; count < 5; count++)
	{
		if (scoreArray[count] > highest)
			highest = scoreArray[count];
	}
	return highest;
}
double findLowest(double scoreArray[5]) {
	int count;
	double lowest ;
	lowest = scoreArray[0];
	for (count = 1; count < 5; count++)
	{
		if (scoreArray[count] > lowest)
			lowest = scoreArray[count];
	}
	return lowest;
}
double calcAverage(double scoreArray[5], double lowest, double highest) {
	double avg, sum = 0;
	int total;
	for (total = 0; total < 5; total++)
		sum += scoreArray[5];

	avg = ((sum - highest) + (sum -lowest)) / 3.0;

	cout << "Your score is " << avg << endl;
	return avg;
}
Last edited on
In calcAverage I need to find the average of the middle three scores. dropping lowest and highest.
@trevsatt, did you read my post?
Please read http://www.cplusplus.com/doc/tutorial/arrays/
All of it. Carefully.
First, you need to Remove the 'size' from line 13 to 25.

“&” is not a valid character that can be used to name an identifier
Referring to your latest code:

I urge you to write comments above each of your functions explaining (1) what the function is supposed to do, (2) what the parameters mean and (3) what the function returns. This will help make it clear in your mind what the code is supposed to do. More importantly, it will help you see when the code doesn't do what it's supposed to, or when the arguments or return value aren't right.

Then go look at where you call each function. Have you called it correctly?

Programming requires being absolutely crystal clear about this stuff. You have nearly all the logic that the program needs, but you often aren't calling it right or your computing things in the wrong place.
Topic archived. No new replies allowed.