Sorting names

Here is the problem:

1)read each name from a file (25 names)
2)place the names in an array in the order they were read
3)print out the items in the array. (with array index#)
4)sort the items in the array alphabetically. (using either a bubble
sort or a selection sort)
5)print out the sorted array.

Here is the code:

Program project2 (input, output);
uses wincrt;
const
    MAX=25;
var
   ifile, ofile:text;
   inchar:char;
   names:array[1..MAX] of integer;
   i, n: integer;
   ok:boolean;
   a,b,c:string;
   
Procedure test(var infile:text);
begin
     Assign(infile,'a:project.dat');
     Reset(infile);
end;

Procedure swap(a,b:string);
var
   c:string;
begin
     a:=b;
     b:=c;
     c:=a;
end;

begin
     test(ifile);
     reset(ifile);


while not eoln do
begin
repeat

      ok:= true;
      for i:= 1 to n-1 do
      if names[i] > names[i+1] then
begin
     swap(names[i], names[i+1]);
     ok := false
end
until ok
end;
    writeln(ofile);
    readln(ifile);
    test(ifile,ofile);
end;

begin
     Close(ifile);
     Close(ofile);
End.

This is not working!  Help please.
slamAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Johann091297Connect With a Mentor Commented:
here is a little program that should do the trick,

program names_25_sort;

uses crt;

type str30=string[30];
     strarr=array [1..25] of str30;

var c:char;
    f:file of str30;
    s:strarr;
    tmp:string;
    i:integer;

procedure bubbelsort(var s:strarr);
var nog:boolean;
    i:integer;

  procedure swapit(var a,b:str30);
  var c:string;
  begin
    nog:=true;
    c:=a;
    a:=b;
    b:=c;
  end;

begin
  nog:=true;
  while nog do
  begin
    nog:=false;
    for i:= 1 to 24 do
      if s[i]>s[i+1]
      then swapit(s[i],s[i+1]);
  end;
end;

procedure printarr(sa:strarr);
var i:integer;
begin
  for i:=1 to 25 do
    writeln(sa[i]);
end;

procedure loadarr(var sa:strarr);
var i:integer;
begin
  assign(f,'myfile.dat');
  reset(f);
  clrscr;
  for i:=1 to 25 do
    read(f,sa[i]);
  close(f);
end;

begin
  repeat
    clrscr;
    Writeln('Menu !');
    writeln('1.Create new file');
    writeln('2.Print contents');
    writeln('3.Sort and print');
    writeln('4.Quit');
    c:=readkey;
    case c of
      '1':begin
            assign(f,'myfile.dat');
            rewrite(f);
            for i:=1 to 25 do
            begin
              write('Name ',i,': ');
              readln(s[i]);
              write(f,s[i]);
            end;
            close(f);
          end;
      '2':begin
            loadarr(s);
            clrscr;
            printarr(s);
            readkey;
          end;
       '3':begin
             loadarr(s);
             bubbelsort(s);
             clrscr;
             printarr(s);
             readkey;
           end;
   end;
  until c='4';
end.
0
 
kellyjjCommented:
Where are you reading the names into the array, or how are you storing them?
0
 
slamAuthor Commented:
I guess I'm not sure what you are asking?  I'm trying to read a file from my a:drive.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
slamAuthor Commented:
Adjusted points to 100
0
 
kellyjjCommented:
you are trying to read a file from a:   but where in your program do you put what you get into a var??
0
 
slamAuthor Commented:
Johann, I appreciate the help but your program doesn't seem to read the a:drive where the names to be sorted are stored.  My program doesn't read it either, that's part of my problem.
0
 
slamAuthor Commented:
Johann, disregard the previous comment.  After running the program, I get a runtime error(2) for case c of '2' and case c of '3'.  Maybe I need to do a rewrite procedure?  Please tell me what you think.
0
 
Johann091297Commented:
this procedure loads the array;

procedure loadarr(var sa:strarr);
var i:integer;
begin
  assign(f,'myfile.dat');
  reset(f);
  clrscr;
  for i:=1 to 25 do
    read(f,sa[i]);
  close(f);
end;

To solve the problem of the a: drive change the line
  assign(f,'myfile.dat');
 to
  assign(f,'a:\myfile.dat');

The reason why u get a io error is because the file must first be created by calling menu item 1.
If you run the program for the first time, just go to 1.create array (press 1) and the problem of the runtime error should be solved, once this file is created, this program should work perfect from then.


0
 
slamAuthor Commented:
Johann, your program run fine but remember, I need to read the list of names(25)that is already in a file on the a:\.  I don't need to type the names in, I need the program to read the names from the a:/ and then sort the names alphabetically.
0
 
oliasCommented:
Well, without actually compiling and running your code I can pick out some things:
1) The variable "n" is never assigned a value.
2) The 6th line from the end of you example calls test with 2 parameters (it was declared with only one.)
3) You have to call "ReWrite" on an output file.
Hope this helps.
0
 
Johann091297Commented:
ok, i see u use a text file.
i'll fix the prog, i haven't got much time online tonight, it'll be here by tomorrow
0
 
slamAuthor Commented:
Thanks alot Johann!
0
 
Johann091297Commented:
This program should read it from a text file, sort and write it to another text file;
Just remember to change the filenames of the input and output files to the files on your a drive (declared as constants in the beginnning)

Program project2 (input, output);
uses crt;

const
 MAX=25;
 input_file='a:project.dat';{the filename containing
                             the list to be sorted}
 output_file='a:output.dat';{the filename that the
                      sorted list must be written to}

var
 afile:text;
 names:array[1..MAX] of string;
 i:integer;
 ok:boolean;

Procedure swap(var a,b:string);
var c:string;
begin
  c:=a;
  a:=b;
  b:=c;
end;

begin
  {reads in the 25 names from the inpute file}
  assign(afile,input_file);
  reset(afile);
  for i:= 1 to 25 do
    readln(afile,names[i]);
  close(afile);

  {bubble sort on the array names}
  ok:=true;
  while ok do
  begin
    ok:= false;
    for i:= 1 to max-1 do
    if names[i] > names[i+1] then
    begin
      swap(names[i], names[i+1]);
      ok := true
    end
  end;

  {writes to the output file}
  assign(afile,output_file);
  rewrite(afile);
  for i:= 1 to max do
    writeln(afile,names[i]);
  close(afile);
end.

0
 
slamAuthor Commented:
Thanks very much for your help Johann.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.