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 two scripts(must be titled “primeSumLoopS” and “primeSumCallS”) in MATLAB that prompts the user to enter...
Write two scripts(must be titled “primeSumLoopS” and “primeSumCallS”) in MATLAB that prompts the user to enter an integer x that 2< x <10000, then the program calculates and displays the sum of the all the prime numbers that are smaller than or equal to x. For example, if x is 11, then the output is 28, which is 2+3+5+7+11. Use two methods: the 1st script uses nested loop and the 2nd script calls another function, primeF.
Program IN C Prompt the user to enter month number and year. Retain an appropriate response...
Program IN C Prompt the user to enter month number and year. Retain an appropriate response for invalid month numbers, but don’t worry about validity of year. 2.Determine the number of days in the month entered (ignore leap years). 3.Use the answer to step 2 in one FOR-LOOP to ask the user to enter a FAHRENHEIT temperature (integer or floating) for each day of the month utilizing a prompt that includes the DATE. a. Prompt the user to enter the...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT