Solved

about my source code?

Posted on 1998-10-28
11
237 Views
Last Modified: 2010-04-16
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.

0
Comment
Question by:lewis_loo
  • 2
  • 2
  • 2
  • +5
11 Comments
 
LVL 7

Expert Comment

by:Inteqam
ID: 1215873
I understood the code , but i didn't understand what you want , can you explain more?

0
 
LVL 1

Expert Comment

by:saam
ID: 1215874
i don't understand your question..
but i can give you a tip about your function UserAnswer

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

/saam
0
 
LVL 3

Expert Comment

by:elfie
ID: 1215875
home work assignment?
0
 
LVL 4

Expert Comment

by:davidmwilliams
ID: 1215876
 This is clearly a homework question -- and you're asking us to do too much to bother, for 5 points.
  Here's a hint ... use an array to avoid so much repetition
0
 

Expert Comment

by:flobecker
ID: 1215877
You're right!
Only teachers can imagine such *%&$# questions. REAL coders use arrays!

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:flobecker
ID: 1215878
You're right!
Only teachers can imagine such *%&$# questions. REAL coders use arrays!

0
 
LVL 7

Expert Comment

by:Inteqam
ID: 1215879
lewis_loo :
 are you there?

0
 
LVL 1

Author Comment

by:lewis_loo
ID: 1215880
yeah, Of course it's an assignment but I want to have some knowledge from you all. This source code is already finished, I want you all to build in a complex, difficult and a shortcut to arrange my source code, so I can learn more in Pascal. The program suppose not to use an array and I want to know how to sort it within a short program, If you all have any idea, Please send to me, of course I will learn more from you than my teacher.

0
 
LVL 1

Author Comment

by:lewis_loo
ID: 1215881
yeah, Of course it's an assignment but I want to have some knowledge from you all. This source code is already finished, I want you all to build in a complex, difficult and a shortcut to arrange my source code, so I can learn more in Pascal. The program suppose not to use an array and I want to know how to sort it within a short program, If you all have any idea, Please send to me, of course I will learn more from you than my teacher.

0
 
LVL 3

Expert Comment

by:vikiing
ID: 1215882
You're saying "I want you all to build in a complex, difficult and a shortcut to arrange my source code, so I can learn more in Pascal". The fact of doing things more complicated and difficult will really *NOT* help to learn Pascal, nor any other programming language (to be true, nor any other thing in your life).
0
 
LVL 3

Accepted Solution

by:
daitt earned 0 total points
ID: 1215883
That's good procedures (ofcourse if we don't use arrays). There's not much change I can make:

   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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
Note: This is the third blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   We’ve been talki…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

914 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now