0

I'm trying to code a procedure which goes through a record of numbers and finds which one is highest, the code currently is below. The issue I have is that it just seems to list the last score on the record (not the highest). Any help is greatly appreciated.

Procedure FindTopScore(Var TopScores : TTopScores);
Var
Count : Integer;
Highest : Integer;
Name: String;

Begin
     For Count := 1 to MaxSize Do
          If TopScores[Count].Score > Highest Then
     Highest := TopScores[Count].Score;
     Name := TopScores[Count].Name;
       Writeln('Higest score is by ' ,TopScores[Count].Name, ' of ', TopScores[Count].Score);
End;
orange
  • 5,297
  • 12
  • 50
  • 71

3 Answers3

2

You're not outputting Highest, but TopScores[Count].Score. Just use

 Writeln('Highest is ', Highest, ' for ', Name);

Also you should put the name into a variable Name inside the if-statement (it actually is outside).

Addon: If you want all names in case of a tie you can use e.g. the following code

Highest := 0;
For Count := 1 to MaxSize Do Begin
     If TopScores[Count].Score = Highest Then Begin
         Name := Name + ' and ' + TopScores[Count].Name;
     End;
     If TopScores[Count].Score > Highest Then Begin
         Highest := TopScores[Count].Score;
         Name := TopScores[Count].Name;
     End;
 End;
Howard
  • 38,639
  • 9
  • 64
  • 83
  • Ah I see thanks. Also fixed name issue. Thanks for pointing that out. Also say if two people have the same top scores, anyway to output both of them? – orange May 25 '11 at 18:04
  • @Jeff you can e.g. append the new name to variable `Name` if `TopScores[Count].Score = Highest` inside your loop and replace it if it is truely greater. – Howard May 25 '11 at 18:23
  • I'm confused, possible for you to show me the code? I made it = to highest but output is just random ASCII characters. – orange May 25 '11 at 18:56
  • Thanks, I understand now. I was bit confused when you said new name to variable. I get it now. Thanks! – orange May 25 '11 at 21:03
1

In addition to Howard's answer, set '0' to 'Highest' before beginning the loop. Being uninitialized, it is having an arbitrary value, probably higher then the highest score.

Sertac Akyuz
  • 54,131
  • 4
  • 102
  • 169
  • Thanks but what is the advantage of doing this because it works fine without it as well. – orange May 25 '11 at 18:08
  • @Jeff - An uninitialized local variable holds the value of its memory location. For an integer it can be 0 or anything else, it can be 0 now but it might be anything else some other time the procedure is executed. If you're not using a commonly used compiler like FPC, Delphi, check your compilers documentation if this is so or not.. – Sertac Akyuz May 25 '11 at 18:39
0

In addition to the accepted answer, make sure you turn on your warnings and hints, and you'll see:

      testhighest.pp(16,39) Warning: Local variable "Highest" does not seem to be initialized

which is the

        If TopScores[Count].Score > Highest Then

line

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89