Write a script that will prompt the user for the radius and height of a cone, error check the user’s input for the radius and the height, and then calculate and print the volume of the cone, in the form of a table, for 7 cones.
2 views (last 30 days)
Show older comments
Kindly help! in this program im a newbie
1 Comment
Rik
on 1 Dec 2018
I could do your homework for you, but then you would stay a newbie. So instead I'm going to try to help you solve this on your own.
Try to break this down into steps. What do you need to do? How do you prompt a user for imput? What values are allowed as input? How can you return a custom error message to explain the user what to do? The formula is math, so you should be able to figure that one out. Then for the final step, how to print text to the command window in Matlab?
All of these questions should be relatively easy to answer with Google. Try to answer as many of the questions I put here, and try to write some code that solves those parts. If you get stuck, try the rest first and then comment here what you were unable to figure out. It is generally easier to apply fixes to code than to write from scratch.
Answers (1)
Image Analyst
on 1 Dec 2018
Hint: first Read this link
See this code and adapt as needed, like putting into a for loop that iterates 7 times, and uses the table() function:
% Ask user for two floating point numbers.
defaultValue = {'45.67', '78.91'};
titleBar = 'Enter a value';
userPrompt = {'Enter floating point number 1 : ', 'Enter floating point number 2: '};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end % Bail out if they clicked Cancel.
% Convert to floating point from string.
usersValue1 = str2double(caUserInput{1})
usersValue2 = str2double(caUserInput{2})
% Check usersValue1 for validity.
if isnan(usersValue1)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue1.
usersValue1 = str2double(defaultValue{1});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue1);
uiwait(warndlg(message));
end
% Do the same for usersValue2
% Check usersValue2 for validity.
if isnan(usersValue2)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue2.
usersValue2 = str2double(defaultValue{2});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue2);
uiwait(warndlg(message));
end
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!