1. Write a Matlab program that will allow the user to find area and circumferenc
ID: 666397 • Letter: 1
Question
1. Write a Matlab program that will allow the user to find area and circumference of a circle. Selections and data entry will be done by presenting the user with a menu (using menu function) and provide her/him with the following choices:
Enter the radius
Display the area
Display the circumference
Exit
Other Requirements:
Selecting choice 2 or 3 before entering the radius will result in an error message. The program will terminate only when Exit has been selected
Calculations will be done by functions.
Function circleInfo() will ask for the radius and will return it.
Function circleArea() will accept a parameter, the radius, and returns the area.
Function circleCircum() will accept a parameter, the radius, and returns the circumference.
Results are displayed with 2 decimal place accuracy.
Partial Help with the menu:
flag =0;
%Calculation
while (1) choice=menu('Circle and Cylinder', 'Enter the radius','Display the area', 'Display the circumference','Exit'); switch(choice)
case 1
r=circleInfo();
flag=1;
case 2 if (flag==0)
disp('you need to enter the radius first');
else
area=circleArea(r);
end
Explanation / Answer
% circleInfo:
function radius = circleInfo()
radius = input('Enter the radius')
% circleArea:
function area = circleArea(radius)
area = 3.14 * radius * radius
% circleCircum
function circum = circleCircum(radius)
circum = 2 * 3.14 * radius
% main.m
flag = 0;
while (1)
choice=menu('Circle and Cylinder', 'Enter the radius','Display the area', 'Display the circumference','Exit');
switch(choice)
case 1
r=circleInfo();
flag=1;
case 2
if (flag==0)
disp('you need to enter the radius first');
else
area=circleArea(r);
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.