Solved

Decision Making Program

Posted on 1998-09-18
5
167 Views
Last Modified: 2010-04-16
Could anyone help for following program.  It is my Pacsal Coursework.  You can send the source code directly to my email address : yllee@po.pacific.net.sg.  Thank you very much in advance.
----------------------------------------------------------
Write a program that determines two integers inputs by users can be a rectangle, triangle, circle or a square. Compare the area & perimeter of each geometric figure.
0
Comment
Question by:yllee
5 Comments
 
LVL 1

Expert Comment

by:girts063098
ID: 1215610
You mean user can input coordinates of rectangle, triangle, circle or a square?
0
 

Author Comment

by:yllee
ID: 1215611
Yes.
0
 
LVL 2

Expert Comment

by:kellyjj
ID: 1215612
You should try to do your own homework. Then if you need help w/ a certain part or something then post that.
0
 

Expert Comment

by:elkiors
ID: 1215613
Program flow decisions are easy :

I agree with kellyjj, but here are a few things to get you started.

This is the most basic form of an IF..THEN Decision:

IF A=B THEN
BEGIN
  { Do your code for this decision here }
END;

or you could expand it to do :

IF A=B THEN
BEGIN
  { Do your code for this decision here }
END
ELSE
BEGIN
  { Do something else instead }
END;

you can nest these constructs together ie.

IF A=B THEN
BEGIN
  { Do your code for this decision here }
END
ELSE
BEGIN
  { Do something else instead }
  IF C=D THEN
  BEGIN
    { Do your code for this decision here }
  END
  ELSE
  BEGIN
    { Do something else instead }
  END;
END;

but then you should know this already because your tutors should have given you this info.

have fun

elkiors,
0
 
LVL 5

Accepted Solution

by:
scrapdog earned 100 total points
ID: 1215615
Try this program.  The only coordinates needed for the square are the left and right sides, and no coordinates are needed for the circle (only a radius).  Since your specs were very vague, it is hard to know exactly what you want.  If you are only learning pascal do a trace and see exactly what us going on.



program shapes;

const
  circle = 1;
  square = 2;
  rectangle = 3;
  triangle = 4;

function triangle_area(x1,y1,x2,y2,x3,y3 :real) :real;
var
  upper, lower, left_most, right_most, width, height,
  triangle_1, triangle_2, triangle_3 :real;
begin
  lower := y1;
  if y2 < lower then lower := y2;
  if y3 < lower then lower := y3;
  upper := y1;
  if y2 < upper then upper := y2;
  if y3 < upper then upper := y3;
  left_most := x1;
  if x2 < left_most then left_most := x2;
  if x3 < left_most then left_most := x3;
  right_most := x1;
  if x2 > right_most then right_most := x2;
  if x3 > right_most then right_most := x3;
  width := right_most - left_most;
  height := upper - lower;
  triangle_1 := abs((x2 - x1) * (y2 - y1) / 2);
  triangle_2 := abs((x3 - x2) * (y3 - y2) / 2);
  triangle_3 := abs((x1 - x3) * (y1 - y3) / 2);
  triangle_area := abs(width * height - triangle_1 - triangle_2 - triangle_3);
end;

function triangle_perimeter(x1,y1,x2,y2,x3,y3 :real) :real;
var
  dist_1, dist_2, dist_3  :real;
begin
  dist_1 := sqrt((x2-x1) * (x2-x1) + (y2-y1) * (y2-y1));
  dist_2 := sqrt((x3-x2) * (x3-x2) + (y3-y2) * (y3-y2));
  dist_3 := sqrt((x1-x3) * (x1-x3) + (y1-y3) * (y1-y3));
  triangle_perimeter := dist_1 + dist_2 + dist_3;
end;

procedure get_user_shape(var shape :integer);
begin
  repeat
    write('Enter shape (1 = circle, 2 = square, 3 = rectangle, 4 = triangle): ');
    readln(shape);
  until (shape >= 1) and (shape <= 4);
end;


procedure get_coordinate(message :string;  var coordinate :real);
begin
  write('Enter the ',message,': ');
  readln(coordinate);
end;


procedure get_shape_data(    shape :integer;
                         var area  :real;
                         var perimeter :real);

var
  radius, left_side, right_side, x1, y1, x2, y2, x3, y3 :real;
begin
  case shape of
    circle:  begin
               get_coordinate('radius of circle',radius);
               area := radius * radius * pi;
               perimeter := radius * pi * 2;
             end;
    square:  begin
               get_coordinate('x coordinate of left side of square', left_side);
               get_coordinate('x coordinate of right side of square', right_side);
               area := (right_side - left_side) * (right_side - left_side);
               perimeter := (right_side - left_side) * 4;
             end;
    rectangle: begin
                 get_coordinate('x coordinate of upper left corner of rectangle', x1);
                 get_coordinate('y coordinate of upper left corner of rectangle', y1);
                 get_coordinate('x coordinate of lower right corner of rectangle', x2);
                 get_coordinate('y coordinate of lower right corner of rectangle', y2);
                 area := abs((x2 - x1) * (y2 - y1));
                 perimeter := abs((x2 - x1) * 2 + (y2 - y1));
               end;
    triangle: begin
                get_coordinate('x coordinate of first vertex', x1);
                get_coordinate('y coordinate of first vertex', y1);
                get_coordinate('x coordinate of second vertex', x2);
                get_coordinate('y coordinate of second vertex', y2);
                get_coordinate('x coordinate of third vertex', x3);
                get_coordinate('y coordinate of third vertex', y3);
                area := triangle_area(x1,x2,y1,y2,x3,y3);
                perimeter := triangle_perimeter(x1,x2,y1,y2,x3,y3);
               end;
  end; {case}
end;

procedure output_result(shape :integer;
                        area, perimeter :real);
begin
  writeln;
  write('The area of the ');
  case shape of
    circle:     write('circle');
    square:     write('square');
    rectangle:  write('rectangle');
    triangle:   write('triangle');
  end;
  writeln(' is ',area:5:4);
  writeln('The perimeter is ',perimeter:5:4);
end;

var
  shape :integer;
  area, perimeter :real;

begin
  get_user_shape(shape);
  get_shape_data(shape, area, perimeter);
  output_result(shape, area, perimeter);
end.


scrapdog!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The advancement in technology has been a great source of betterment and empowerment for the human race, Nevertheless, this is not to say that technology doesn’t have any problems. We are bombarded with constant distractions, whether as an overload o…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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