»
 

Go Back   ResellerRatings Store Ratings > ResellerRatings Forums > Tech Support

Reply
 
LinkBack Thread Tools Display Modes
Old 06-09-2003, 08:55 AM   #1 (permalink)
Registered User
 
Join Date: Jun 2003
Posts: 1
cisforme is on a distinguished road
Gaming 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.

cisforme is offline   Reply With Quote
Old 06-24-2003, 05:56 AM   #2 (permalink)
Registered User
 
Join Date: Jun 2003
Location: Nr Winchester UK
Posts: 10
pjaj is on a distinguished road
Well, as far as I can see, your basic pseudocode is sound.
I presume the target language is C
You don't say exactly what is going wrong

There are a few points:
1) Do you need to check that the weights sum to 1.00? Don't forget that testing floating point numbers for equality is not quite as easy as it looks. Better to test -eps < (float1 -float2) < eps where eps is some small floating point number such as 0.00001 in your case.
2) What test data have you used, make some up with good and bad data to check your validation, include missing scores, so that you can see what happens if there are supposed to be n scores and you have n-1 (or for that matter n+1). Unless you are stress testing the code, you don't need vast quantities of data, 2 or 3 sets of scores should be enough.
3) Have you reserved enough space for the name as a string?
4) Put some printf statements at strategic points throughout the code so you can see what it has actually read and calculated.
5) You might want to consider reading a whole line at a time with the fgets function (then it will be ready to print straight to the output or error file) and parsing it with strtok.
pjaj is offline   Reply With Quote
Reply




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Most Active Discussions

Recent Discussions

All times are GMT -6. The time now is 05:31 AM.