Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Can you make this source code into 5 procedures and 1 function into an arrangement order?

this source code is to arrange an input into an ascending order or descending order.

uses crt;

var n1,n2,n3,n4,n5 : real;

ans:string;

opt:char;

procedure ascending(var a1,a2,a3,a4,a5 :real); forward;

procedure descending(var d1,d2,d3,d4,d5 : real); forward;

Procedure Choice;

begin

write(opt);

writeln;

case opt of

'1' : begin

ascending(n1,n2,n3,n4,n5);

writeln(n1:8:2,n2:8:2,n3:8:2,n4:8:2,n5:8:2);

end;

'2' : begin

descending(n1,n2,n3,n4,n5);

writeln(n1:8:2,n2:8:2,n3:8:2,n4:8:2,n5:8:2);

end;

end;

end;

function UserAnswer:boolean;

begin

readln(ans);

if (pos('N',ans)=0) and (pos('n',ans)=0) then UserAnswer:=true

else UserAnswer:=false;

end;

procedure order(asc:boolean; var x,y:real);

var temp: real;

begin

if asc then begin

if x>y then begin

temp:=x;

x:=y;

y:=temp;

end

end

else if not asc then begin

if x<y then begin

temp:=y;

y:=x;

x:=temp;

end;

end;

end;

procedure input(var n1,n2,n3,n4,n5:real);

begin

write('Enter 1st number : ');readln(n1);

write('Enter 2nd number : ');readln(n2);

write('Enter 3rd number : ');readln(n3);

write('Enter 4th number : ');readln(n4);

write('Enter 5th number : ');readln(n5);

end;

procedure ascending(var a1,a2,a3,a4,a5 :real);

begin

order(true,n1,n2);

order(true,n1,n3);

order(true,n1,n4);

order(true,n1,n5);

order(true,n2,n3);

order(true,n2,n4);

order(true,n2,n5);

order(true,n3,n4);

order(true,n3,n5);

order(true,n4,n5);

end;

Procedure descending(var d1,d2,d3,d4,d5:real);

begin

order(false,n1,n2);

order(false,n1,n3);

order(false,n1,n4);

order(false,n1,n5);

order(false,n2,n3);

order(false,n2,n4);

order(false,n2,n5);

order(false,n3,n4);

order(false,n3,n5);

order(false,n4,n5);

end;

Function GetUserChoice:char;

begin

writeln('Press `1` for ascending order or `2` for decending order');

write('What order would you like the numbers to appear ? ');

GetUserChoice:=readkey;

end;

begin

repeat

clrscr;

input(n1,n2,n3,n4,n5);

repeat

opt:=GetUserChoice;

choice;

until (opt in ['1','2']);

write('Would you like to view the Descending order ? ');

if UserAnswer then begin

descending(n1,n2,n3,n4,n5);

writeln(n1:8:2,n2:8:2,n3:8:2,n4:8:2,n5:8:2);

end;

writeln;

write('Continue ? ');

until not UserAnswer;

End.

this source code is to arrange an input into an ascending order or descending order.

uses crt;

var n1,n2,n3,n4,n5 : real;

ans:string;

opt:char;

procedure ascending(var a1,a2,a3,a4,a5 :real); forward;

procedure descending(var d1,d2,d3,d4,d5 : real); forward;

Procedure Choice;

begin

write(opt);

writeln;

case opt of

'1' : begin

ascending(n1,n2,n3,n4,n5);

writeln(n1:8:2,n2:8:2,n3:8

end;

'2' : begin

descending(n1,n2,n3,n4,n5)

writeln(n1:8:2,n2:8:2,n3:8

end;

end;

end;

function UserAnswer:boolean;

begin

readln(ans);

if (pos('N',ans)=0) and (pos('n',ans)=0) then UserAnswer:=true

else UserAnswer:=false;

end;

procedure order(asc:boolean; var x,y:real);

var temp: real;

begin

if asc then begin

if x>y then begin

temp:=x;

x:=y;

y:=temp;

end

end

else if not asc then begin

if x<y then begin

temp:=y;

y:=x;

x:=temp;

end;

end;

end;

procedure input(var n1,n2,n3,n4,n5:real);

begin

write('Enter 1st number : ');readln(n1);

write('Enter 2nd number : ');readln(n2);

write('Enter 3rd number : ');readln(n3);

write('Enter 4th number : ');readln(n4);

write('Enter 5th number : ');readln(n5);

end;

procedure ascending(var a1,a2,a3,a4,a5 :real);

begin

order(true,n1,n2);

order(true,n1,n3);

order(true,n1,n4);

order(true,n1,n5);

order(true,n2,n3);

order(true,n2,n4);

order(true,n2,n5);

order(true,n3,n4);

order(true,n3,n5);

order(true,n4,n5);

end;

Procedure descending(var d1,d2,d3,d4,d5:real);

begin

order(false,n1,n2);

order(false,n1,n3);

order(false,n1,n4);

order(false,n1,n5);

order(false,n2,n3);

order(false,n2,n4);

order(false,n2,n5);

order(false,n3,n4);

order(false,n3,n5);

order(false,n4,n5);

end;

Function GetUserChoice:char;

begin

writeln('Press `1` for ascending order or `2` for decending order');

write('What order would you like the numbers to appear ? ');

GetUserChoice:=readkey;

end;

begin

repeat

clrscr;

input(n1,n2,n3,n4,n5);

repeat

opt:=GetUserChoice;

choice;

until (opt in ['1','2']);

write('Would you like to view the Descending order ? ');

if UserAnswer then begin

descending(n1,n2,n3,n4,n5)

writeln(n1:8:2,n2:8:2,n3:8

end;

writeln;

write('Continue ? ');

until not UserAnswer;

End.

but i can give you a tip about your function UserAnswer

UserAnswer := (pos('N',ans)=0) and (pos('n',ans)=0)

/saam

Here's a hint ... use an array to avoid so much repetition

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

procedure order(asc:boolean; var x,y:real);

var temp: real;

begin

if (asc xor (x<y)) then begin

temp:=x;

x:=y;

y:=temp;

end

end;

You don't need the forward procedures. Just simply move order, ascending, descending procedures above others.

And your interface confuses me. What do you really want? You want to let the users sort once (ascending or descending), then ask them if they want to sort descending (maybe descending sorting is much more important than ascending one??). Perhaps:

begin

repeat

clrscr;

input(n1,n2,n3,n4,n5);

repeat

opt:=GetUserChoice;

choice;

until not (opt in ['1','2']);

write('Continue ? ');

until not UserAnswer;

End.

Then the user can simply enter other number in order to quit this set of numbers.

And ofcourse, the comment of saam (about UserAnswer) is worth seeing.

Good luck.

daitt