Question

In: Mechanical Engineering

MATLAB The user is supposed to enter either a ‘yes’ or ‘no’ in response to a...

MATLAB

The user is supposed to enter either a ‘yes’ or ‘no’ in response to a prompt. The script will print “OK, continuing” if the user enters either a ‘y’, ‘Y’,'Yes', or 'yes' or it will print “OK, halting” if the user enters a ‘n’, ‘N’, 'No', or 'no' or will print “Error” if the user enters anything else.

Code this twice once with if and once with switch:

Then recode the example above to keep prompting until an an acceptable yes or no is entered:

What loop should we use?

what should we intialize?

please generate all code for Matlab

Solutions

Expert Solution

If Part Code:-

prompt ="Do you want to continue or not =";
input_user = input(prompt,'s');

if (input_user =='y') || (input_user =='Y') || (input_user =='yes') || (input_user =='YES')
disp("OK, continue");
elseif(input_user =='n') || (input_user =='N') || (input_user =='no') || (input_user =='NO')
disp("OK, halting");
else
disp("Error");
end

Output:-

Do you want to continue or not =Y
OK, continue

Swich Code

clc
clear all
prompt ="Do you want to continue or not =";
input_user = input(prompt,'s');

switch ( input_user)
case {'Y','y','YES','yes'}
    disp("OK, Continue")
case {'N','n','NO','no'}
    disp("OK, Halting")
   otherwise
      disp("Error")
end

Output

Do you want to continue or not =N
OK, Halting

if loop Code:-

clear all
clc
while(1)
prompt ="Do you want to continue or not =";
input_user = input(prompt,"s");

if (input_user =='y') || (input_user =='Y') || (input_user =='yes') || (input_user =='YES')
disp("OK, continue");
break;
elseif (input_user =='n') || (input_user =='N') || (input_user =='no') || (input_user =='NO')
disp("OK, halting");
break;
else
disp("Error");
end
end

Output

Do you want to continue or not =g
Error
Do you want to continue or not =y
OK, continue

swich loop Code

clc
clear all
while(1)
prompt ="Do you want to continue or not =";
input_user = input(prompt,'s');

switch ( input_user)
case {'Y','y','YES','yes'}
    disp("OK, Continue")
    break
case {'N','n','NO','no'}
    disp("OK, Halting")
    break;
   otherwise
      disp("Error")
end

end


Output

Do you want to continue or not =g
Error
Do you want to continue or not =y
OK, Continue


Related Solutions

Implement the working of a mohr circle in matlab. ask user to enter the stresses in...
Implement the working of a mohr circle in matlab. ask user to enter the stresses in x and y direction. take the inputs to a stress tensor. then use formulations to find the eigen value and vectors. MATLAB
q1) using Matlab make a code that allows a user to enter a function like fractional...
q1) using Matlab make a code that allows a user to enter a function like fractional function and cubic liner function then the Matlab send a domain, range, continuity
q1) using Matlab make a code that allows a user to enter a function like fractional...
q1) using Matlab make a code that allows a user to enter a function like fractional function and cubic liner function then the Matlab send a domain, range, continuity
Write a MATLAB program to do the following: 1- Allows the user to enter unlimited number...
Write a MATLAB program to do the following: 1- Allows the user to enter unlimited number of data set. 2- Allows the user to exit the program at any time by entering zero. 3- Calculates and displays the following statistics for the entered data set: a- Count of positive, negative, and total numbers. b- Maximum and Minimum numbers. c- Sum and Average of positive numbers. d- Sum and Average of negative numbers. e- Overall Sum and overall average of all...
In MatLab 1. Ask the user to enter a 1 x 5 vector of numbers. Determine...
In MatLab 1. Ask the user to enter a 1 x 5 vector of numbers. Determine the size of the user entry. 2. Ask the user to enter a 1 x 5 vector of numbers. Determine the size of the user entry. If the user enters a 5 x 1 vector, use a warning statement to inform the user of their error, and set the new input value to be the transpose of the user input value. If the user...
Write a mips assembly language program that asks the user to enter an alphabetic character (either...
Write a mips assembly language program that asks the user to enter an alphabetic character (either lower or upper case)and change the case of the character from lower to upper and from upper to lower and display it.
Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
2005 2012 Response of Interest Yes Yes Sample Size 450 450 Count for Response 90 72...
2005 2012 Response of Interest Yes Yes Sample Size 450 450 Count for Response 90 72 Sample Proportion 0.2000 0.1600 Confidence Interval (in terms of 2005 - 2012) Confidence Coefficient 0.90 Lower Limit -0.0021 Upper Limit 0.0821 Hypothesis Test (in terms of 2005 - 2012) Hypothesized Value Pooled Sample Proportion 0.1800 Test Statistic p-value (Lower Tail) p-value (Upper Tail) p-value (Two Tail) 2005 2012 Response of Interest Yes Yes Sample Size 450 450 Count for Response 90 72 Sample Proportion...
USE MATLAB Write a program in Matlab that would continuously ask the user for an input...
USE MATLAB Write a program in Matlab that would continuously ask the user for an input between 1 and 6, which would represent the result of rolling a die. The program would then generate a random integer between 1 and 6 and compare its value to the value entered by user. If the user’s die is larger, it should display, “Mahahanap mo na ang forever mo. Sana all!” If the user’s die is smaller, it should display, “Gising na friend,...
MATLAB Program a Matlab version of Guess the Word. The word should be determined by user...
MATLAB Program a Matlab version of Guess the Word. The word should be determined by user input. Once the word is determined, the game should begin by displaying blanks (-) where all the letters of the word are. The game should then continue to prompt the player to guess a letter in the word. If the player guesses a letter that is in the word and not already displayed, that letter should be displayed everywhere it appears in the word....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT