sorting values in Turbo Pascal

Hello,

this is my problem :
I've got a file with at each line a number from 1 till 25. I'm looking for code that can count how many times the numbers occure. (e.g. number 1 : 125 times, number 2 : 289 times)
Then this should be sorted from the highest to the lowest. So place 1 is for the number that is most counted in the file. (In the example number 2 would get place 1, number1 place2)

Thnx
klimaksAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

binkzzCommented:
First of all, we'll need to get the totals. If you open up the files and get the values, you can then simply do a

totalcount[number_found_in_file] := totalcount[number_found_in_file] + 1;

At the end of it, fi. totalcount[1] would hold the total number of 1's found in the file. All you have to do at this point is sort it, there are several way you can go about this but shouldn't be too hard.

If you want an exmaple, please say and I'll put one up.
0
HypoCommented:
If I got you right, this is the code you're looking for...
GetListFromFile, count's the number of time each value (> 0, <= MaxValue) occurs and stores it into the List variable. Sortlist sort's the List so that Index[1] holds the most counted number, Index[2] the second most, etc, etc.
List[Index[1]] holds the number of times the most counted value occured in the file, List[Index[2]] the second most...

Program CountValuesInFile;

uses crt,dos;

const MaxValue = 25;

var List  : array[1..MaxValue] of word;
    Index : array[1..MaxValue] of word;
    lp    : word;


Function GetListFromFile(Filename : string; var Lst : array of word) : Boolean;
var F : text;
    V : word;
begin
 Fillchar(Lst,sizeof(Lst),0);
 assign(F,Filename);
 If IOResult = 0 then begin
  Reset(F);
  If IOResult = 0 then begin
  repeat
   Readln(F,V);
   if (V > 0) and (V <= MaxValue) then inc(Lst[V-1]);
  until EOF(F);
  Close(F);
  end else writeln('IO Error : ',IOResult);
 end else writeln('IO Error : ',IOResult);
end;

Procedure SortList(var Lst,Indx : array of word);
var Lp1,Lp2,T : word;

begin
  For Lp1 := 1 to MaxValue do Indx[Lp1-1] := Lp1;
  For Lp1 := 0 to MaxValue-2 do
   For Lp2 := Lp1 to MaxValue do
    If Lst[Indx[Lp1]-1] < Lst[Indx[Lp2]-1] then begin
     T := Indx[Lp1];
     Indx[Lp1] := Indx[Lp2];
     Indx[Lp2] := T;
    end;
end;

begin
 clrscr;
 if GetListFromFile('Numbers.LST',List) then begin
{ for lp := 1 to MaxValue do List[lp] := random(1000);}
 for lp := 1 to MaxValue do writeln(lp : 2,' : ',List[lp]:3);
 SortList(List,Index);
 end;

 readkey;
 clrscr;
 for lp := 1 to MaxValue do writeln(Index[lp] : 2,' : ',List[Index[lp]]:3);
 readkey;
end.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Pascal

From novice to tech pro — start learning today.