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

IN MATLAB thanks in advance!! Using functions and global variables as well as ot

ID: 3738188 • Letter: I

Question

IN MATLAB thanks in advance!!

Using functions and global variables as well as other features of matlab create a function called Cookingtimer that adjusts the timer based on the entered food items from a list of items.

(a) Using one fprintf create a menu of 3 food items (pizza, cake, and stake) were they are listed in a column in that specific order.

(b) Create a global variable called ’food’ and using a input ask the user enter one of the items from you menu. Suppress the output of the variable.

(c) Create a function called Cookingtimer that has no inputs and outputs. (d) In the function, set up the global variable ’food’ again.

(e) In the function, Using the global variable food that you created check it against your 3 menu items. (Hint: When comparing strings be sure to use double quotes (”) around the words and not single quotes (’)). • If the first item on the menu is entered tell the user it will take 5 seconds and pause the code for 5 seconds. For the second item, tell the user that it will take 10 seconds and pause the code for 10 seconds. Do the same for item 3, but the time is 15 seconds. Use fprintf() to tell the use the time required for each item. (Hint: you may have to use if statements and look up the pause() function). • If none of the menu items are entered, tell the user to choose from your menu.

(f) Call your function in the main code and create a fprintf telling the user their food is ready. Be sure to include what the food item is in the output.

(g) Using tic and toc time time how long it takes your function and the output to run.

Explanation / Answer

Given below are the steps to follow ...Please do rate the answer if it helped. Thank you.


Step1 : Save the following code in a file named Cookingtimer.m

function Cookingtimer()
global food
if strcmp(food,"pizza")
t = 5;
elseif strcmp(food,"cake")
t = 10;
elseif strcmp(food, "stake")
t = 15;
else
fprintf("Invalid food item specified! ");
return;
end

fprintf("Your %s will be ready in %d seconds ", food, t);
pause(t);
fprintf("Your %s is ready!! ", food);
end

--------------------------------
Step2 : Save the following code in any other file say test.m

global food;
fprintf("Today's menu ");
fprintf(" pizza cake stake ")
food = input("What would you like to have? ", "s");
tic
Cookingtimer();
elapsed = toc;
fprintf("The function took %f seconds ", elapsed);

--------------------------------
Step3: Execute test.m

output
======
Today's menu
pizza
cake
stake
What would you like to have? > pizza
pizza
Your pizza will be ready in 5 seconds
Your pizza is ready!!
The function took 5.010349 seconds