In: Computer Science
the language is matlab
1) Write a code that will print a list consisting of “triangle,” “circle,” and “square.” It prompts the user to choose one, and then prompts the user for the appropriate quantities (e.g., the radius of the circle) and then prints its area. If the user enters an invalid choice, the script simply prints an error message. For calculating the area, create separate functions for each choice. Name them as calcTriangle, calcCircle, calcSquare respectively, which are only for area calculation. All the input and output should be in the main script. The main script part should include a nested ifelseif statement. Here are two examples of running it (units are assumed to be inches). For test cases, you need to test all three options. >> Problem 4 >> Problem 4 Menu Menu 1. Triangle | Enter 1 for Triangle 1.Triangle | Enter1 for Triangle 2. Circle | Enter 2 for Circle 2. Circle | Enter 2 for Circle 3. Square | Enter 3 for Square 3. Square | Enter 3 for Square Please choose one: 1 Please choose one:2 Enter the height of the Triangle: 5 Enter the radius of the Circle: 5 Enter the base length of the Triangle: 4 The area of the Circle is 78.54 The area of the triangle is 10.00
Hi,
Please find the answer below:
Main Script:
%% ################################
%% MainScript.m
%% This script invokes the other
%% functions to calculate area.
%% ################################
fprintf('Main Menu\n')
fprintf('1) Triangle\n')
fprintf('2) Circle\n')
fprintf('3) Square\n')
user_choice = input('Enter your choice? : ');
% if-else statements
if user_choice == 1
height = input('Enter the height of the Triangle
(inches): ');
base = input('Enter the base of the Tiangle(inches):
');
area = calcTriangle(height,base);
elseif user_choice == 2
radius = input('Enter the radius of the
Circle(inches): ');
area = calcCircle(radius);
elseif user_choice == 3
side = input('Enter the side of the Square(inches):
');
area = calcSquare(side);
else
fprintf('Error: The input choice is wrong.\n');
fprintf('Try Again!');
end
fprintf('Area = %.2f inches^2\n',area);
Functions:
%% ################################
%% calcTriangle.m
%% Input : height, base
%% Output: area
%% ################################
function area = calcTriangle (height,base)
area= 0.5*height*base ;
end
%% ################################
%% calcSquare.m
%% Input : side
%% Output: area
%% ################################
function area = calcSquare(side)
area= side*side;
end
%% ################################
%% calcCircle.m
%% Input : radius
%% Output: area
%% ################################
function area = calcCircle (radius)
area= pi*radius*radius ;
end
Sample Output:
>> AreaMainScript
Main Menu
1) Triangle
2) Circle
3) Square
Enter your choice? : 2
Enter the radius of the Circle(inches): 4
Area = 50.27 inches^2
--------------------------------------------------------------------
Hope this helps.
Kindly like the solution if it is useful to you.
Thanks.