» 
Calculate averages from input file
Could ANYONE please help me with the following.
Question:
The program reads the grades from a file called scores.dat. The file scores.dat begins with a header line
followed by a set of scores for each student. The format of the header line is:
n weight1 weight2 weight3 weightn
n=the number of scores for each student and weight is the weight of the respective score. Now the header line
is followed by a set of scores, and the format is as follows:
name score1 score2 score3 scoresn
where name is the last name of the student and score1....2....3 is the score. All scores must be between 1 and 100.
The program reads the file scores.dat and writes a file called average.dat that has the following format:
name score1 score2 score3 => nn.nn Average
where name and score1....2.....3's are as before and nn.nn is the weighted average of the student's score
My program should validates its input (make sure it is between 1 and 100), and that everyone has a score
If a student has an invalid score it should write it to cerr and that student's scores ahould not be written to
average.dat.
The weighted ave is calculated as follows:
n
Z scorei x weighti
i=1
___________________
n
Below is what I have in mind...
To determine the weighted average for each student, simply multiply each weight with the corresponding score and add, without dividing by n.
If, for example, the first two lines in the input file looks like this:
3 0.3 0.5 0.2
John 75 80 65
John’s weighted average will be calculated as follows:
(0.3 x 75) + (0.5 x 80) + (0.2 x 65)
Open input file for reading.
Open output file for writing
Read the first item in the file into an integer. This is the number of
tests.
Check that the number of tests does not exceed MAX_TESTS (I set this to 10).
If NumTests > MAX_TESTS, abort.
Set up array of float [NumTests]
Use for(int i = 0; i < NumTests; ++i) loop to read into the array the
weights.
Set up while loop to read in until no more pupil names (then end of file has
been reached) Read in pupil name
float MarkSum = 0.0f;
Use for loop again to read in each mark
Multiply each mark by corresponding weight (from array)
Add this product to MarkSum
Output name and MarkSum to file or screen, I can't remember
end for loop
end while loop
This is what I have in mind, the code does not want to work at all sometimes, so I actually need help on the codeing of it.
Thanks in advance.
|