# pascal bubble sort problem

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 :');
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}

###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
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
Commented:
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 :');
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

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
oh... the declaration
var
index: integer;

is not necessary (in sort)

i think u declared it instead of i?
0
Author Commented:
Edited text of question.
0
Author Commented:
Works fine, thanks for your help lychee
0
Commented:

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 :');
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
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Pascal

From novice to tech pro — start learning today.