Solved

Sorting names

Posted on 1997-09-10
14
284 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
TMemoryStream -> PNGImage -> TIcon convertion 22 1,270
Determine Drive Types 2 478
Neural Networks with Delphi 3 1,655
Delphi need modify hand cursor windows 4 571
Enabling the Skype for Business Meeting Scheduler in Hybrid OWA
Active Directory security has been a hot topic of late, and for good reason. With 90% of the world’s organization using this system to manage access to all parts of their IT infrastructure, knowing how to protect against threats and keep vulnerabil…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

740 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