Question

In: Computer Science

the language is matlab 1) Write a code that will print a list consisting of “triangle,”...

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

Solutions

Expert Solution

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.


Related Solutions

draw the vertexes of a triangle and generate a list of the vertices code is in...
draw the vertexes of a triangle and generate a list of the vertices code is in python use turtle graphics
Use Matlab to write the following 1. Write code to determine whether a year is a...
Use Matlab to write the following 1. Write code to determine whether a year is a leap year. Use the mod function. The rules for determining leap years in the Gregorian calendar are as follows: All years evenly divisible by 400 are leap years. Years evenly divisible by 100, but not by 400, are not leap years. Years divisible by 4, but not by 100, are leap years. All other years are not leap years. For example, the years 1800,...
write the code in MATLAB with comments and show the inputs and results of the code...
write the code in MATLAB with comments and show the inputs and results of the code for the question below. Write an .m file in MATLAB, that records audio (you can record your own voice for 20 seconds that was recorded using your phone), then take Fourier transform of the signal (probably FFT).
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The...
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The function will have one int 1D array n and one int size as parameters. The function will display all the values of n on one line. Function #2 Write the function AverageEvenOdd. The function will have one int 1D array n and one int size as parameters. The size of the array is given by the parameter int size. Therefore, the function should work...
The n th Triangle Problem Write a code for finding the n th triangle number of...
The n th Triangle Problem Write a code for finding the n th triangle number of triangle sequences: 1, 3, 6, 10, ..., n. That is, your code should accept an integer number, which indicates the triangle levels, and returns how many dots we need to form a triangle with respect to the given level. For example, consider the Fig 1. For n = 3 (can be also written as T3), your code should returns 6. Provide a single program...
1. Treacherous Triangle Trickery. Consider a charge distribution consisting of an equilateral triangle with a point...
1. Treacherous Triangle Trickery. Consider a charge distribution consisting of an equilateral triangle with a point charge q fixed at each of its vertices. Let d be the distance between the center of the triangle and each vertex, let the triangle’s center be at the origin, and let one of its vertices lie on the x-axis at the point x = −d. 1.1. Compute the electric field at the center of the triangle by explicitly computing the sum of the...
Develop a MATLAB (or programming language of your choice) code to show the flow field for...
Develop a MATLAB (or programming language of your choice) code to show the flow field for lifting cylinder flow using the following specifications. Set your free-stream velocity, cylinder radius, and vortex strength in the code. There are a total of four cases for which you will run your code (or have all four in one script). You will only be changing the vortex strength for the different cases. Therefore, set ?∞ = 1 and ? = 1 for all cases....
show the MATLAB Code with comments and Write an .m file in MATLAB, that records audio...
show the MATLAB Code with comments and Write an .m file in MATLAB, that records audio (you can record your own voice for 20 seconds), takes Fourier transform of the signal (probably FFT).
write a matlab code to simulate fiber optics communication system on matlab simulink
write a matlab code to simulate fiber optics communication system on matlab simulink
Using pseudocode or C++ code, write code to print “small” if the magnitude M of an...
Using pseudocode or C++ code, write code to print “small” if the magnitude M of an earthquake is in the range [0, 3), “medium” if M is in the range [3, 6), “large” if M is in the range [6, 9) and “epic” if M is greater than or equal to 9, where M is input by a user via the keyboard. (in c++)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT