Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# Decision Making Program

Posted on 1998-09-18
Medium Priority
176 Views
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
Question by:yllee

LVL 1

Expert Comment

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

Author Comment

ID: 1215611
Yes.
0

LVL 2

Expert Comment

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

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

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): ');
until (shape >= 1) and (shape <= 4);
end;

procedure get_coordinate(message :string;  var coordinate :real);
begin
write('Enter the ',message,': ');
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
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

Question has a verified solution.

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

Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
In this article, the configuration steps in Zabbix to monitor devices via SNMP will be discussed with some real examples on Cisco Router/Switch, Catalyst Switch, NAS Synology device.
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
###### Suggested Courses
Course of the Month9 days, 4 hours left to enroll

#### 877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.