Solved

Decision Making Program

Posted on 1998-09-18
5
163 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

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

Suggested Solutions

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…
A customer recently asked me about anti-malware and the different deployment options available for his business. Daily news about cyberattacks, zero-day vulnerabilities, and companies that suffered a security breach made him wonder if the endpoint a…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 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

15 Experts available now in Live!

Get 1:1 Help Now