Arrays, min/max values, etc.

The assignment:

In a gymnastics or diving competition, each contestant's score is calculated by dropping the lowest and highest scores and then adding the remaining scores. A judge awards points between 1 and 10, with 1 being the lowest and 10 being the highest. Write a program that will read judges' scores from an input file (there will be between 5 and 10 scores) and output the points received by the contestant, formatted with two decimal places.

For example,

Input file contains: 9.2 9.3 9.0 9.9 9.5 9.5 9.6 9.8

Dropped scores: 9.0 and 9.9

Points received: 56.90

Since you do not know the exact number of scores you will need to count the scores as they are read from the input file and use this as the actual size, or number of elements actually stored in your array. You should refer to the lecture notes for more information on how to read data from a file, store it in an array, and continue until you reach the end of file.


My code:
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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cassert>

using namespace std;

void OpenFile(ifstream &ScoresFile);
void GetScores(ifstream &ScoresFile, double Scores[], int &size);
void MinMax(double Scores[], int size);

int main()
{
	ifstream ScoresFile;
	double Scores[15];
	int size;
	

	OpenFile(ScoresFile);
	GetScores(ScoresFile, Scores, size);
	MinMax(Scores, size);



	

	return(0);
}

// This function will prompt the user to name the input file that will store the scores.
void OpenFile(ifstream &ScoresFile)
{
	string infilename;

	cout << "Please enter the name of your input file: ";
	cin >> infilename;

	ScoresFile.open(infilename.c_str());
	assert(ScoresFile);
}

// This function will get the scores from the file and store them in an array.
void GetScores(ifstream &ScoresFile, double Scores[], int &size)
{
	int i = 0;

	do
	{
		ScoresFile >> Scores[i];
		i++;
	}
	while(ScoresFile);

	size = i;

}

// This function will figure out the minimum and maximum scores.
void MinMax(double Scores[], int size)
{
	double minScore = Scores[0];
	double maxScore = Scores[0];

	double points = 0;	
	int i;

   for(i = 0; i < 15; i++)		
		{
		  points += Scores[i];
		  if (Scores[i] > maxScore)
			 maxScore = Scores[i];
		  if (Scores[i] < minScore)
			 minScore = Scores[i];

		  points = Scores[i] - minScore - maxScore;
		}
   
   cout << points;
}



My problem:

So it takes in the file information (I copied the same numbers out of the assignment and placed them in a text file), it puts it into the array. It calculates the minScore and maxScore (I believe... have tested it and it worked, along with additional garbage code). What I need this whole thing to do is remove the minScore and maxScore from the equation, and add the remaining numbers to create the total score, and output it to the screen. I'm stuck on that portion.

Any help/push in the right direction I highly appreciate. Please no super-fancy advanced coding - what the project says is exactly what the teacher wants. :-) Plus I wouldn't understand it heh...

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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cassert>

using namespace std;

void OpenFile(ifstream &ScoresFile);
void GetScores(ifstream &ScoresFile, double Scores[], int &size);
void MinMax(double Scores[], int size);

int main()
{
	ifstream ScoresFile;
	double Scores[15];
	int size = 0;
	

	OpenFile(ScoresFile);
	GetScores(ScoresFile, Scores, size);
	MinMax(Scores, size);



	

	return(0);
}

// This function will prompt the user to name the input file that will store the scores.
void OpenFile(ifstream &ScoresFile)
{
	string infilename;

	cout << "Please enter the name of your input file: ";
	cin >> infilename;

	ScoresFile.open(infilename.c_str());
	assert(ScoresFile);
}

// This function will get the scores from the file and store them in an array.
void GetScores(ifstream &ScoresFile, double Scores[], int &size)
{

		int i = 0;

		
		while(ScoresFile >> Scores[i])
		{
			i++;
		}

		size = i;
}

// This function will figure out the minimum and maximum scores.
void MinMax(double Scores[], int size)
{
	double minScore = Scores[0];
	double maxScore = Scores[0];

	double points = 0;	
	int i;

   for(i = 0; i < size; i++)		
		{
		  points += Scores[i];
		  if (Scores[i] > maxScore)
			 maxScore = Scores[i];
		  if (Scores[i] < minScore)
			 minScore = Scores[i];
		}
   
   points = (points - minScore - maxScore) / (size - 2 );

   cout << points;
}


I didn't test this much so you will wont to double check me
Topic archived. No new replies allowed.