Solved

Sorting names

Posted on 1997-09-10
14
281 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
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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Knowing where your website is hosted is as important as the features you receive, the monthly fee, and the support you receive. Due diligence should be done when choosing your next hosting provider.
Adding Computers to AD groups through an SCCM Task Sequence
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 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