Solved

Sorting names

Posted on 1997-09-10
14
279 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
  • 7
  • 4
  • 2
  • +1
14 Comments
 
LVL 2

Expert Comment

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

Author Comment

by:slam
Comment Utility
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
Comment Utility
Adjusted points to 100
0
 
LVL 2

Expert Comment

by:kellyjj
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Expert Comment

by:Johann091297
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks alot Johann!
0
 

Expert Comment

by:Johann091297
Comment Utility
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
Comment Utility
Thanks very much for your help Johann.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
A procedure for exporting installed hotfix details of remote computers using powershell
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now