im in desperate need of help. for school i have to write a program that reads 5 names of students and a grade for each then writes the names of the students in order of grades starting with the highest. i missed part of the class when this was taught so any help will do. i have to use an array and a bubble sort for this.
Comments
:
Well, a buble sort is basically a nested for-do loop, in which you compare two items in order from an array. In code it looks something like this:
[code]
for i:=1 to 4 do
for j:=i to 5 do begin
{ Compare the items here }
end;
[/code]
This way iterates through the entire array several times and moves one item up or down the sorting order. The code above has been written for 5 items in an array[1..5].
The comparison is a simple if-then statement, which sets the order for the two items:
[code]
for i:=1 to 4 do
for j:=i to 5 do begin
if Names[j]>Names[j+1] then
{ exchanges names };
end;
[/code]
Names is an array of strings. In this code, if name[j] ought to come after name[j+1], and thus be exchanged.
The exchange part of the code is quite simple:
[code]
for i:=1 to 4 do
for j:=i to 5 do begin
if Names[j]>Names[j+1] then begin
TempName:=Names[j];
Names[j]:=Names[j+1];
Names[j+1]:=TempName;
end;
end;
[/code]
This pseudo-code sorts an array of five names alphabetically. To sort a list of grades, a similar code is used.
: :
: Well, a buble sort is basically a nested for-do loop, in which you compare two items in order from an array. In code it looks something like this:
: [code]
: for i:=1 to 4 do
: for j:=i to 5 do begin
: { Compare the items here }
: end;
: [/code]
: This way iterates through the entire array several times and moves one item up or down the sorting order. The code above has been written for 5 items in an array[1..5].
: The comparison is a simple if-then statement, which sets the order for the two items:
: [code]
: for i:=1 to 4 do
: for j:=i to 5 do begin
: if Names[j]>Names[j+1] then
: { exchanges names };
: end;
: [/code]
: Names is an array of strings. In this code, if name[j] ought to come after name[j+1], and thus be exchanged.
: The exchange part of the code is quite simple:
: [code]
: for i:=1 to 4 do
: for j:=i to 5 do begin
: if Names[j]>Names[j+1] then begin
: TempName:=Names[j];
: Names[j]:=Names[j+1];
: Names[j+1]:=TempName;
: end;
: end;
: [/code]
: This pseudo-code sorts an array of five names alphabetically. To sort a list of grades, a similar code is used.
:
[code]
for i:=1 to 5 do begin
write('Give ',i,'th name: ');
readln(Names[i]);
end;
[/code]
: :
: Well, a buble sort is basically a nested for-do loop, in which you compare two items in order from an array. In code it looks something like this:
: [code]
: for i:=1 to 4 do
: for j:=i to 5 do begin
: { Compare the items here }
: end;
: [/code]
: This way iterates through the entire array several times and moves one item up or down the sorting order. The code above has been written for 5 items in an array[1..5].
: The comparison is a simple if-then statement, which sets the order for the two items:
: [code]
: for i:=1 to 4 do
: for j:=i to 5 do begin
: if Names[j]>Names[j+1] then
: { exchanges names };
: end;
: [/code]
: Names is an array of strings. In this code, if name[j] ought to come after name[j+1], and thus be exchanged.
: The exchange part of the code is quite simple:
: [code]
: for i:=1 to 4 do
: for j:=i to 5 do begin
: if Names[j]>Names[j+1] then begin
: TempName:=Names[j];
: Names[j]:=Names[j+1];
: Names[j+1]:=TempName;
: end;
: end;
: [/code]
: This pseudo-code sorts an array of five names alphabetically. To sort a list of grades, a similar code is used.
:
Actually there is a small error in code. You should
use [b]i[/b] instead of [b]j+1[/b]. So the code should
look like this:
[code]
ListSize:=5;
for i:=1 to (ListSize-1) do
for j:=i to ListSize do begin
if Names[j]>Names[i] then begin
TempName:=Names[j];
Names[j]:=Names[i];
Names[i]:=TempName;
end;
end;
[/code]
Iby
:
Simple:
Your sorting routine swaps array records.
If the record is only one element no problem.
If the record has more elemets (any number)
everything remains the same except that you
have to swap the WHOLE RECORD and not just
the element you use as sorting criteria.
Example:
[code]
type student=record
first_name:string;
last_name:string;
mothers_maiden_name:string;
shue_size:real;
grade:real;
cell_phone:string;
IQ:byte;
popularity:longint;
got_laid:boolean;
end;
var class:array[1..25] of student;
temp:student; { we need one extra for swapping }
i,j:byte;
{ .... }
for i:=1 to 19 do { search all records }
for j:=i to 20 do
if class[i].grade<class[j].grade then { compare grades }
begin { swapp the whole record }
temp:=class[i];
class[i]:=class[j];
class[j]:=temp;
end;
[/code]
Iby
PROGRAM FiveGrades (input, output);
TYPE
grades=array [1..5] of integer;
names=array [1..5] of string;
CONST
length=5;
VAR
continue:char;
data:names;
intone:integer;
inttwo:integer;
intthree:integer;
intfour:integer;
intfive:integer;
score:grades;
tempone:integer;
temptwo:integer;
temp:integer;
////////////////////////////////////////////////////////////////
//GET DATA
////////////////////////////////////////////////////////////////
PROCEDURE GetData (var score:grades;
var data:names);
//GIVEN: nothing
//TASK: asks for names and grades
//RETURN: score and data
Var
temp:integer;
Begin
{?}
End;
////////////////////////////////////////////////////////////////
//SORT
////////////////////////////////////////////////////////////////
PROCEDURE Sort (var intfour:integer;
var intfive:integer);
//GIVEN: score
//TASK: sorts grades
//RETURN: sorted list
Var
intone:integer;
inttwo:integer;
Begin
For inttwo:= 1 to length-1 Do
begin
intfive:=inttwo;
for intone:=inttwo+1 to length do
if score[intone] > score[intfive] then
intfive:=intone;
intfour:=score[intfive];
score[intfive]:=score[inttwo];
score[inttwo]:=intfour;
end;
End;
/////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//PRINT DATA
////////////////////////////////////////////////////////////////
PROCEDURE PrintData (var score:grades;
var data:names);
//GIVEN: score and grades
//TASK: prints final data
//RETURN: nothing
Var
temp:integer;
tempone:integer;
temptwo:integer;
Begin
{?}
End;
////////////////////////////////////////////////////////////////
BEGIN
REPEAT
GetData (score, data);
Sort (intfour, intfive);
PrintData (score, data);
writeln;
writeln;
write ('Do you want to continue?(y or n)');
writeln;
readln (continue);
writeln;
writeln;
UNTIL (continue='n') or (continue='N');
END.
type student=record
name:string;
grade:real;
end;
var class:array[1..5] of student;
temp:student; { we need one extra for swapping }
i,j:byte;
procedure get_data;
begin
for i:=1 to 5 do begin
writeln;
write('Student #',i,' name:');readln(class[i].name);
write('Student #',i,' score:');readln(class[i].grade);
end;
end;
procedure sort_by_grade;
begin
for i:=1 to 4 do { search all records }
for j:=i to 5 do
if class[i].grade<class[j].grade then { compare grades }
begin { swapp the whole record }
temp:=class[i];
class[i]:=class[j];
class[j]:=temp;
end;
end;
procedure display_data;
begin
writeln('Results are sorted by grade:');
writeln('Nr. Grade Student Name');
writeln('---------------------------');
for i:=1 to 5 do
writeln(i,' ',class[i].grade:2:2,' '+class[i].name);
end;
{ main program }
begin
get_data;
sort_by_grade;
display_data;
end.
[/code]
: type student=record
: name:string;
: grade:real;
: end;
:
: var class:array[1..5] of student;
: temp:student; { we need one extra for swapping }
:
: i,j:byte;
:
: procedure get_data;
: begin
: for i:=1 to 5 do begin
: writeln;
: write('Student #',i,' name:');readln(class[i].name);
: write('Student #',i,' score:');readln(class[i].grade);
: end;
: end;
:
: procedure sort_by_grade;
: begin
: for i:=1 to 4 do { search all records }
: for j:=i to 5 do
: if class[i].grade<class[j].grade then { compare grades }
:
: begin { swapp the whole record }
: temp:=class[i];
: class[i]:=class[j];
: class[j]:=temp;
: end;
: end;
:
: procedure display_data;
: begin
: writeln('Results are sorted by grade:');
: writeln('Nr. Grade Student Name');
: writeln('---------------------------');
: for i:=1 to 5 do
: writeln(i,' ',class[i].grade:2:2,' '+class[i].name);
: end;
:
:
: { main program }
:
: begin
: get_data;
: sort_by_grade;
: display_data;
: end.
: [/code]
:
program and it works just fine. Do you know how to
compile it? Or you still have problem understanding sorting?
The idea in any sorting algorythm is to:
1. go through all records,
2. compare and then
3. swap them where needed (three step swap - a,b,c).
Difference between sorting algorythms is only in loops.
Rest is the same. It doesn't matter if you are
sorting one array or multiple arrays.
Check the following procedure which was used in previous
post:
[code]
procedure sort_by_grade;
begin
for i:=1 to 4 do { (1) search all records }
for j:=i to 5 do
if class[i].grade<class[j].grade then { (2) compare grades }
begin { (3) swapp the record(s) }
temp:=class[i]; { A copy one variable to temp }
class[i]:=class[j]; { B copy second variable to first one }
class[j]:=temp; { C copy temp to second variable }
end;
end;
[/code]
It is doing exactly what was mentioned in the begining:
It has [b]for[/b] loop(s) ([b]i[/b] and [b]j[/b]),
it has one comparison ([b]if[/b]), and it has swap
section ([b]begin ..... end;[/b]) with the famous
three step rotation (ABC). Remember this well since
we will use them later.
Ok let's do some spagetti code:
[code]
{ create arrays and one temp variable of same type for each array }
var grade : array[1..5] of real;
temp_grade : real;
name : array[1..5] of string;
temp_name : string;
last_name : array[1..5] of string;
temp_last_name : string;
smart : array[1..5] of boolean;
temp_smart : boolean;
i,j : byte; { these are needed for loops }
{ etc. }
for i:=1 to 4 do { (1) same loops as before }
for j:=i to 5 do
if grade[i]<grade[j] then { (2) same compare as before }
begin { (3) swap ALL records!!! }
temp_grade := grade[i]; { A1 remember ABC from swap thing above ?? }
temp_name := name[i]; { A2 }
temp_last_name := last_name[i]; { A3 }
temp_smart := smart[i]; { A4 }
;
;
grade[i] := grade[j]; { B1 }
name[i] := name[j]; { B2 }
last_name[i] := last_name[j]; { B3 }
smart[i] := smart[j]; { B4 }
;
;
grade[j] := temp_grade; { C1 }
name[j] := temp_name; { C2 }
last_name[j] := temp_last_name;{ C3 }
smart[j] := temp_smart; { C4 }
end;
[/code]
If you still don't get it, try to find someone else to
help you since I'm quite exhausted...
Iby
: