visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
316 experts, 1194 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: Pascal :: BUBBLE SORT - DESCENDING ORDER


By: aggygirl U.S.A.  Date: 25/05/2003 00:00:00  English French  Points: 125 Status: Answered
Quality : Excellent
Hallo!

Can anyone try to help on Correction of my program so that i can get my numbers to be arranged into Descending order? the following is my program

program Bubble ;
uses wincrt;


var
one_dim :array[1..10] of integer;
comparison :boolean;
nos, Passes, Temp,n :integer;
reply,order : char;

procedure ascending;
begin
for nos := 1 to n do

begin
writeln('Please! enter the number..Then press enter');
readln(one_dim[nos]);
end;
writeln;
writeln('The sorted Numbers in Ascending Order');

Passes := 1;{initialize to}
comparison := true;
while (Passes <= n - 1) and (comparison) do
begin
nos := 1;
comparison := false;
while nos <= n - 1 do
begin
if one_dim[nos] > one_dim[nos +1] then
begin{swap elements}
Temp := one_dim[nos];
one_dim[nos] := one_dim[nos + 1];
one_dim[nos + 1] := Temp;
comparison := true;{set flag to}
end;
nos := nos + 1;
end;
Passes := Passes + 1;
end;
for nos := 1 to n do

begin
writeln(one_dim[nos]);
end;
end;


procedure descending;
begin
for nos := 1 to n do
begin
writeln('Please! enter the number..Then press enter');
readln(one_dim[nos]);
end;
writeln;
writeln('The sorted Numbers in Descending Order');
Passes := 1;{initialize to}
comparison := true;
while (Passes >= n + 1) and (comparison) do
begin
nos := 1;
comparison := false;
while nos >= n + 1 do
begin
if one_dim[nos] < one_dim[nos - 1] then
begin{swap elements}
Temp := one_dim[nos];
one_dim[nos] := one_dim[nos + 1];
one_dim[nos + 1] := Temp;
comparison := true;{set flag to}
end;
nos := nos + 1;
end;
Passes := Passes + 1;
end;
for nos := 1 to n do

begin
writeln;
writeln(one_dim[nos]);
end;
end;

begin{main Program}
repeat
writeln('how many Numbers do you wish to sort?');
readln(n);
clrscr;
writeln;
writeln('Please choose the desirable order');
writeln('1: Ascending Order');
writeln('2: Descending Order');
readln(order);
writeln;

case order of
'1': ascending;
'2': descending;
end;
writeln('Do you wish to Test more data?'); {Prompt for user reply}
readln (reply);
until not (reply IN ['Y', 'y']);
writeln;
writeln(' ----------------------------------------- ');
writeln('| EXIT .. by closing the Window !! |');
writeln(' ----------------------------------------- ');
end. {End of the Program Bubble sort}

By: VGR Date: 25/05/2003 17:41:00 English  Type : Answer
your program works very fine, you just have to be logical :

procedure descending;
begin
for nos := 1 to n do
begin
writeln('Please! enter the number..Then press enter');
readln(one_dim[nos]);
end;
writeln;
writeln('The sorted Numbers in Descending Order');
Passes := 1;{initialize to}
comparison := true;
while (Passes <= n - 1) and (comparison) do
begin
nos := 1;
comparison := false;
while nos <= n - 1 do
begin
if one_dim[nos] < one_dim[nos + 1] then
begin{swap elements}
Temp := one_dim[nos];
one_dim[nos] := one_dim[nos + 1];
one_dim[nos + 1] := Temp;
comparison := true;{set flag to}
end;
nos := nos + 1;
end;
Passes := Passes + 1;
end;
for nos := 1 to n do

begin
writeln(one_dim[nos]);
end;
end;


the procedures are 99,99% the same, only the test direction changes...
By: aggygirl Date: 26/05/2003 00:13:00 English  Type : Comment
Thanks for the help
By: VGR Date: 26/05/2003 00:16:00 French  Type : Comment
dis-moi si ça marche 8-)
By: aggygirl Date: 26/05/2003 00:22:00 English  Type : Comment
Delete The Above Question, I have already get the Solution, so I don't want more Answers for the Question.
By: aggygirl Date: 26/05/2003 00:24:00 English  Type : Comment
Hallo!
Can anyone Please help me to correct my Program in "Procedure Delete Record "as Follows, as everytime I run my program I get the RunTime Error 001:0160 which I dont understand how to correct it.
**********************************************************************
PROGRAM:-


program student;
uses wincrt;

type studentrecord = record
studref: integer;
surname: string[15];
initial: char;
course : string[15];
end;

var
studfile,studfile1:file of studentrecord;
filename,tempname :string;
studrec :studentrecord;
loop :integer;
studref :integer;
choice :char;
reply :char;

{**************************************************************
Procedure Name: Listing the records
Input : Invoke by Main body of the program.
Task : To list all the records in the file
Output : Listing the records.
*****************************************************************}

procedure listrec;

var rec:integer;

begin
reset(studfile);
repeat
read(studfile,studrec);
begin
writeln('Student Reference :',studrec.studref);
writeln('Surname :',studrec.surname);
writeln('Initial :',studrec.initial);
writeln('Course :',studrec.course);
writeln;
end
until eof(studfile);
end;

{**************************************************************
Procedure Name: Delete Record
Input : Invoke by Main body of the program.
Task : To delete the file selected
Output : Deleted and ready to insert new records or quit.
*****************************************************************}

procedure delrec;

var
delrec, y:integer;
studentrecord,tempname:string;

begin

filename := 'studfile.dat';
tempname := 'studfile1.dat';
assign(studfile,'c:\studfile.dat');
reset(studfile);
assign(studfile1,'c:\studfile1.dat');
rewrite(studfile1);
writeln('enter the record to delete');
readln(studref);

while not (eof(studfile)) do

begin
read(studfile,studrec);
If studrec.studref <> studref then
write(studfile1,studrec);
end;
close(studfile);
erase(studfile);
close(studfile1);
rename(studfile1,'c:\studfile.dat');
writeln('The record',studref,'has been Deleted');

end;

{**************************************************************
Procedure Name: Adding Record
Input : Invoke by Main body of the program.
Task : It adds the new records into the student file
Output : Add the new record into the student file.
*****************************************************************}

procedure addrec;

begin
writeln;listrec;
writeln('You Have require to Add More Records in your file: ');
seek(studfile ,filesize(studfile));
writeln('Record ' ,filesize(studfile)+1);
write('Please enter the Student Reference: ');
readln(studrec.studref);
write('Please enter the student Surname: ');
readln(studrec.surname);
write('Please enter the Initial of First Name: ');
readln(studrec.initial);
write('Please enter the Course taken by the student: ');
readln(studrec.course);
write(studfile,studrec);
writeln;
clrscr;
writeln('---------------------------------------------------------');
writeln('New Student List after adding the New Student is as Follows:- ');
writeln('__________________________________________________________________');
writeln;
listrec;
end;

{**************************************************************
Procedure Name: Search Record
Input : Invoke by Main body of the program.
Task : It allows the user to select the record reference
to be searched from the student file.
Output : Display the searched record into the screen.
*****************************************************************}

procedure seacrec;

begin
loop:= 0;
writeln('Please enter student Reference number to be searched: ');
readln(studref);
writeln('Record: ':5, 'Student Reference: ':20, 'Surname: ':15, 'Initial: ':6, 'Course: ':12);
reset(studfile);
while not(eof (studfile)) do
begin
loop := loop + 1;
read(studfile,studrec);
If studrec.studref = studref then
writeln(loop:5,studrec.studref:20 , studrec.surname:19 , studrec.initial:5, studrec.course:19);
end;
end;


{**************************************************************
Procedure Name: Quit the Program
Input : Invoke by Main body of the program.
Task : It stops the program from further actions.
Output : The program stops and lets you close the window.
*****************************************************************}


procedure Quit;

begin
Halt;
end;{end of procedure quit}



{**************************************************************
Procedure Name: Create New Records
Input : Invoke by Main body of the program.
Task : To create new data into the File.
Output : New record is created into the file.
*****************************************************************}

procedure create;
begin
assign(studfile,'stud.dat');
rewrite(studfile);
writeln('Data into disk file.....');
writeln;

for loop := 1 to 2 do
begin
writeln('Record',loop);

write('Please enter the Student Reference >');
readln(studrec.studref);
write('Please enter the Surname >');
readln(studrec.surname);
write('Please enter the Initial of the Student >');
readln(studrec.initial);
write('Please enter the Course taken by the student >');
readln(studrec.course);
write(studfile,studrec);
end;
close(studfile);
writeln;
end;

{**************************************************************
Procedure Name: Main Menu
Input : Invoke by Main body of the program.
Task : It display to use the choices he can do,
create new data, list the records,
deleting, and search the records.
Output : Display the Main Menu of the program and
awaits user inputs.
*****************************************************************}

Procedure Main_menu;

begin
repeat
ClrScr;
writeln('THE PROGRAM FOR RECORDING THE STUDENT DATA');
writeln;
writeln(' -------------------------------------------------------- ');
writeln('| MAIN MENU |');
writeln('| *********************************** |');
writeln('| PLEASE!!! CHOOSE FROM THE MENU |');
writeln('| ************************************ |');
writeln('| |');
writeln('| 1: Create and initialise the file records |');
writeln('| 2: listing Records |');
writeln('| 3: Deleting the records |');
writeln('| 4: Adding the Records |');
writeln('| 5: Search for the records |');
writeln('| 6: Quit |');
writeln('| |');
writeln(' ------------------------------------------------------------- ');
write(' Please enter your choice > ');
readln(choice);

case choice of
'1': create;
'2': listrec;
'3': delrec;
'4': addrec;
'5': seacrec;
'6': quit
Else
write('Invalid choice, please enter 1,2,3,4,5 or 6');

end;
writeln;
writeln('Do you wish to continue?');
writeln('reply Y/y or N/n or choose 6 to QUIT');
readln(reply);
until not(reply in ['Y','y']);
writeln('The end of Program Service');
writeln('Close The Window to start again');
end;

{**************************************************************}
{ Main Body of the program}
{*************************************************************}
begin
Main_menu;
end.

By: fertugrul Date: 16/07/2006 21:41:05 English  Type : Comment
You have written rename( studfile1, 'studfile.dat');
I don't know what parameters the "rename" procedure takes but, i think you must have written "rename( 'studfile1.dat', 'studfile.dat');". If this is not correct you may try to rename the file *before* you close it. Try the further one first. I hope this helps.
By: VGR Date: 22/07/2006 07:55:56 English  Type : Comment
thanks but no, the rename() procedure takes the "file handle" ("file pointer" in Pascal) as a first parameter.

Do register to be able to answer

EContact
browser fav
page generated in 309.327840 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page