• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1433
  • Last Modified:

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
0
klimaks
Asked:
klimaks
1 Solution
 
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now