Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Averaging 4 highest scores problem

Status
Not open for further replies.

LadyPeeper

Programmer
Sep 8, 2000
8
US
I'd like to create a program where I read four score records for one player. So each player has a set of four records, each record contains a score, along with his player number. I want to throw out the lowest score and average the three highest, then print out that average, with the player number next to it.

How do I code this? I keep going around in circles trying to figure this out how to get the three highest or even look for the lowest.

Also, if the player number for the set of four score records is out of sequence, I'd still like to print out the player number and calculate the average and to print out that there was a sequence error. I don't think that it'll be a problem figuring that out once I get the averaging thing going.

Anyway, any advice on how to approach this is greatly appreciated.

:)
 
LadyPeeper,

the input-file should be sorted on:
1. playernumber,
2. score,
both ascending.

If you start reading this file, you will allways get all four records for each player in sequence, and sorted on score as well.
Now, for each player, read the first record (= lowest score), discard it, then read the next three, everage the scores and print the result for that player.
Process every next player in the same fashion, until end-of-file, and you're done !
Also, if at any time the next playernumber is lower than the previous one, or if a score for a player is lower than his (her?) previous score, you immediately know that there's a sequence error.

If you need further detail, please let it know.

Success !
 
Hmmm, I don't get this part:

"Now, for each player, read the first record (= lowest score), discard it, then read the next three, everage the scores and print the result for that player."

How do you know if the first record is the lowest record? Or are you intentionally making it the lowest record and comparing the other 3 to it?

I'm a newbie so bear with me.
 
RonaldB's solution suggests that the file be sorted into ascending player, score sequence so that the first one is always the lowest and can be ignored. If you have to use the file in the order that it is in, try this instead:

For each player:
Set score sum to zero
Set low score to 99999 or whatever (number of 9's should be total number of digits.
Do 4 times
Add player score to score sum
If player score < low score
Set low score to player score
End if
End do
Average score = (score sum - low score) / 3
Betty Scherber
Brainbench MVP for COBOL II
 
Okay, that sounds close to what I'm doing. And yes, I have to leave the file in the order that it is in so I can't really do a SORT.

What if I do this:

Example:
I set the score sum to zero.
The first record is read in and it's score is 90.
Move 90 to low score AND score-hold.
Read next record which has a score of, say, 80.
I move 80 to low score AND move the 90 in score-hold into score sum, then move 80 into score-hold.
Read the third record which has a score of 95.
I just add 95 to score sum because it's higher than the current low score of 80.
Read fourth record in which has a score of 60.
I move 60 into low score and move 80 (that was in score-hold) to score sum.
Average = Score sum/3

So, in essence, I use the first number as my low score and compare the following three to it and move them around accordingly.

Does this make sense?

 
This makes good sense and it should work fine. If you initialize the total score to zero before processing each set of four, then all the logic will be the same, ie adds instead of sometimes move sometimes add. This might make your code a little simpler. Betty Scherber
Brainbench MVP for COBOL II
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top