Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Today, you are going to practice using loops, strings, and learning new function

ID: 3572187 • Letter: T

Question

Today, you are going to practice using loops, strings, and learning new functions in Matlab. Specifically, you are going to create a script that can answer users questions about the volumes of simple geometric shapes. Your script should:

Repeatedly prompt the user to ask a questions. If the user types 'exit' your script should end (using a = input('Some prompt', 's') takes input as a string.)

The format of the user's questions can very but they will always contain key elements:

They can ask you to find the volume of a cone, a cylinder, a sphere or a cube. Regardless of how they phrase their question, it will always identify the type of shape they are asking about.

If they ask about a cone, a cylinder, or a sphere their question will have the phrase "radius of " followed by an integer number. If they ask about a cone, a cylinder or a cube their question will contain the phrase "height of " followed by an integer number.

Your script should be able to answer questions as different as: "cube height of 12?" and "I was wondering with a height of 15 and maybe a radius of 83 what is the volume of a cone?"

You, of course, have to answer their questions.

Your script should never crash or hang. If a user asks you a poorly formed question, like one that does not contain a shape or another needed component, you should inform them you can't answer their question and prompt them to ask something else.

You have all the coding skills to complete this project but Matlab provides you with tools that might make your job much easier. You may want to spend some time investigating Matlab functions that work with strings. Oh, and use Matlab's pi.

Explanation / Answer

a = input('Enter your query: ', 's');
data = strsplit(a, ' ');
if (strmatch('cylinder', data)>0)
    r = strmatch('radius', data);
    h = strmatch('height', data);
    radius = str2double(data{r+2});
    height = str2double(data{h+2});
    vol = pi*radius*radius*height;
elseif (strmatch('cone', data)>0)
    r = strmatch('radius', data);
    h = strmatch('height', data);
    radius = str2double(data{r+2});
    height = str2double(data{h+2});
    vol = pi*radius*radius*height/3.0;
elseif (strmatch('sphere', data)>0)
    r = strmatch('radius', data);
    radius = str2double(data{r+2});
    vol = pi*radius^3;
elseif (strmatch('cube', data)>0)
    h = strmatch('height', data);
    height = str2double(data{h+2});
    vol = pi*height^3;
end
printf('Volume: %f', vol);