Solved

Sorting names

Posted on 1997-09-10
14
286 Views
Last Modified: 2010-04-16
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.
0
Comment
Question by:slam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
  • +1
14 Comments
 
LVL 2

Expert Comment

by:kellyjj
ID: 1215698
Where are you reading the names into the array, or how are you storing them?
0
 

Author Comment

by:slam
ID: 1215699
I guess I'm not sure what you are asking?  I'm trying to read a file from my a:drive.
0
 

Author Comment

by:slam
ID: 1215700
Adjusted points to 100
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
LVL 2

Expert Comment

by:kellyjj
ID: 1215701
you are trying to read a file from a:   but where in your program do you put what you get into a var??
0
 

Accepted Solution

by:
Johann091297 earned 100 total points
ID: 1215702
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
 

Author Comment

by:slam
ID: 1215703
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
 

Author Comment

by:slam
ID: 1215704
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
 

Expert Comment

by:Johann091297
ID: 1215705
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
 

Author Comment

by:slam
ID: 1215706
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
 

Expert Comment

by:olias
ID: 1215707
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
 

Expert Comment

by:Johann091297
ID: 1215708
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
 

Author Comment

by:slam
ID: 1215709
Thanks alot Johann!
0
 

Expert Comment

by:Johann091297
ID: 1215710
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
 

Author Comment

by:slam
ID: 1215711
Thanks very much for your help Johann.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The conference as a whole was very interesting, although if one has to make a choice between this one and some others, you may want to check out the others.  This conference is aimed mainly at government agencies.  So it addresses the various compli…
Postmortem reporting allow us to examine mistakes in a way that focuses on the situational aspects of a failure’s mechanism and the decision-making process of individuals proximate to the failure. Read our guide on how to handle IT post-mortem repor…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question