Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Decision Making Program

Posted on 1998-09-18
5
Medium Priority
?
173 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 300 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Whoever said that “a picture is worth one thousand words” observed a fact that can dramatically affect your marketing success. Most people tend to learn visually, so many publishers commonly acknowledge the effectiveness of visual learning by using…
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

704 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