?
Solved

pascal bubble sort problem

Posted on 1999-12-12
6
Medium Priority
?
472 Views
Last Modified: 2010-04-16
Having just started to learn Pascal, I am unable to identify the error in the sort procedure. Many thanks for any assistance.

program BubblesortOf;

const
    MAX = 10;
type
    NAME_ARRAY = array [ 1 .. MAX] of string;


procedure Input_Names( var name : NAME_ARRAY);
                       {              To input names into array }
var
   i : integer;         { index for each array element}
begin
     for i:= 1 to MAX do
         begin
              writeln('Name ',i:3,' is :');
              readln(name[i]);
         end;
end;{Input_Names}

procedure SortNames( name : NAME_ARRAY );

var
   i : integer;         { index for each array element}
   N : integer;         {number of names}
begin
   N := MAX;
   while N >= 2 do
       begin
            i:= 1;
            while i < N do
                 begin
                 if name[i] > name [i+1] then
                        begin
                             swap(name[i],name[i+1])
                        end; {end if}
                  i := i+1;
                  end; {end while}
            N := N -1;
       end; {end while}
end;

procedure Output_Names( name : NAME_ARRAY);
                       {              To output names into array }
var
   i : integer;         { index for each array element}
begin
     for i:= 1 to MAX do
         begin
              writeln('Name ',i:3,' is :',name);
         end;
end;{Output_Names}


var
    name:  NAME_ARRAY;                { array to hold names}
begin
    Input_Names (name );
    SortNames (name );
    Output_Names (name );
end. {IntroToArrays}

0
Comment
Question by:mnssaun
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:_lychee_
ID: 2275262
the mistakes are:

1) in the sort procedure, declare the argument as
procedure SortNames(var name:NAME_ARRAY)

this is cos if u don't anyway changes u make to name will not be 'saved' when u exit the procedure...

2) in the sort procedure, u need to declare i in the var statement

var
i: integer;

cos u use i inside the procedure without declaring it

3) swap(a, b) is only for integers (at least in BP7)... if u wanna swap, u should declare a temp string and manually assign like so:

var
temp: string;
.....
temp:=name[i]; name[i]:=name[i+1]; name[i+1]:=temp;

4) in output_names, the writeln statement should be:

writeln(('Name ',i:3,' is :',name[i]);

note the [i]...
0
 
LVL 2

Accepted Solution

by:
_lychee_ earned 200 total points
ID: 2275263
the correct program after the changes above:

program BubblesortOf;

const
    MAX = 10;
type
    NAME_ARRAY = array [ 1 .. MAX] of string;


procedure Input_Names( var name : NAME_ARRAY);
                       {              To input names into array }
var
   i : integer;         { index for each array element}
begin
     for i:= 1 to MAX do
         begin
              writeln('Name ',i:3,' is :');
              readln(name[i]);
         end;
end;{Input_Names}

procedure SortNames( var name : NAME_ARRAY );

var
   index, i : integer;         { index for each array element}
   N : integer;         {number of names}
   temp: string;
begin
   N := MAX;
   while N >= 2 do
       begin
            i:= 1;
            while i < N do
                 begin
                 if name[i] > name [i+1] then
                        begin
                        temp:=name[i];
                        name[i]:=name[i+1];
                        name[i+1]:=temp;
                        end; {end if}
                  i := i+1;
                  end; {end while}
            N := N -1;
       end; {end while}
end;

procedure Output_Names( name : NAME_ARRAY);
                       {              To output names into array }
var
   i : integer;         { index for each array element}
begin
     for i:= 1 to MAX do
         begin
              writeln('Name ',i:3,' is :',name[i]);
         end;
end;{Output_Names}


var
    name:  NAME_ARRAY;                { array to hold names}
begin
    Input_Names (name );
    SortNames (name );
    Output_Names (name );
end. {IntroToArrays}


0
 
LVL 2

Expert Comment

by:_lychee_
ID: 2275268
oh... the declaration
var
index: integer;

is not necessary (in sort)

i think u declared it instead of i?
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 

Author Comment

by:mnssaun
ID: 2275513
Edited text of question.
0
 

Author Comment

by:mnssaun
ID: 2275547
Works fine, thanks for your help lychee
0
 

Expert Comment

by:tzelve
ID: 2283230
your programm became:


program BubblesortOf;

const
    MAX = 10;
type
    NAME_ARRAY = array [ 1 .. MAX] of string;


procedure Input_Names( var name : NAME_ARRAY);
                       {              To input names into array }
var
   i : integer;         { index for each array element}
begin
     for i:= 1 to MAX do
         begin
              writeln('Name ',i:3,' is :');
              readln(name[i]);
         end;
end;{Input_Names}

procedure SortNames( name : NAME_ARRAY );

var
   i,j : integer;         { index for each array element}
   N : integer;         {number of names}
begin
   for I:=1 to max do
      begin
       For J:=I+1 to max do
          begin
           if name[i] > name [j] then
                        begin
                             swap(name[i],name[j])
                       end; {end if}
           end; {end while}
       end; {end while}
end;

procedure Output_Names( name : NAME_ARRAY);
                       {              To output names into array }
var
   i : integer;         { index for each array element}
begin
     for i:= 1 to MAX do
         begin
              writeln('Name ',i:3,' is :',name);
         end;
end;{Output_Names}


var
    name:  NAME_ARRAY;                { array to hold names}
begin
    Input_Names (name );
    SortNames (name );
    Output_Names (name );
end. {IntroToArrays}


0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

In-App Messaging has revolutionized the way we look at marketing. It has also changed the way we use Apps. If In-App Messaging is used well then you will find that it can drive a lot of traffic to specific areas of your site. It also helps to improv…
An ASP.NET Web Form User Control is not newly introduced in ASP.NET. In fact, it was an old technology yet still playing a role to generate web content, especially when we want to use it to have a better and easy way to control part of the web conte…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Suggested Courses

598 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