home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

Calculate averages from input file

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1500
Discussions: 200,989, Posts: 2,379,858, Members: 246,356
Old June 9th, 2003, 11:55 AM   Digg it!   #1 (permalink)
Junior Member
 
Join Date: Jun 2003
Posts: 1
Exclamation
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 June 24th, 2003, 08:56 AM     #2 (permalink)
Member
 
pjaj's Avatar
 
Join Date: Jun 2003
Location: Nr Oxford UK
Posts: 162
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
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (3063)
Charges against non-tippers dropped.. (20)
Delete an OS (17)
Nvidia GTX 260 problem (9)
Laptop with wireless problem. (12)
windows vista security holes (19)
Wireless Televisions. (12)
CPU fan stops spinning randomly (11)
Regular Build (11)
Point and Shoot Camera Suggestions. (8)
windows 7 problem (7)
[F@H SPAM 11/16/09] ! 1/2 months to.. (39)
Internet Lost (5)
Multiple Restarts Required at Boot (5)
Recent Discussions
How to converter MTS Video to HD mpeg.. (0)
Can a page file be "too big".. (0)
Point and Shoot Camera Suggestions. (8)
Looking for new motherboard (0)
Size after cutting 700Mb file is 2.5 .. (0)
Delete an OS (17)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (39)
windows vista security holes (19)
updating BIOS via winflash, claims fi.. (1)
New Server Configuration Suggestions (0)
Desktop Calendar Application (0)
cheap gaming laptop? (12)
Unallocated Space (2)
help me pls laptop just stopped worki.. (1)
C# + LINQ Help (7)
Nvidia GTX 260 problem (9)
Dynex DX E-402 (3)
EVGA 9800 gtx help with finding a goo.. (12)
Multiple Restarts Required at Boot (5)
cell phone won't work (0)
Is the PSU I received dead? (15)
Can't open Word (12)
Steam ID's, Gamertags etc... (4)
Games, Cables, PCI cards, and more fo.. (6)
Dept. of HS: NSA 'Helped' Develop Vis.. (17)


All times are GMT -4. The time now is 05:34 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



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