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

Problem 5 (20 Points) Write a program that asks a user a value of X. Your progra

ID: 3806713 • Letter: P

Question

Problem 5 (20 Points)

Write a program that asks a user a value of X.

Your program then asks the user to enter an answer for 3*X^2-10.

If the user answers correctly, your program would say

If the user answers incorrectly, your program would say either

or

depending on the value of the guess. Your program then asks the user to guess again.

Sometimes, the user would enter an irrational values (for X or for the answer). To avoid infinite guessing game, if the error is less than 1, your program should exit by saying

Run a few different cases to demonstrate that your code is running correctly.

Submission: 1 m-file and a printout of the command window.

Explanation / Answer

Solution:

X = input('Enter a value for X: '); %Getting value of X
P = 3*(X^2)-10; %Computing 3*X^2-10 actually
while(1) %Using While loop.
Y = input('Enter answer for 3*X^2-10: '); %Getting 3*X^2-10 value from user
if(Y==P) %If equals to actual value then this message
disp('The answer is correct!Bye.');
break;
elseif(abs(Y-P)<1) %if absolute difference is less than 1 then this message
disp('The answer is close enough.Bye.');
break;
elseif(Y>P) %if greater then this message
disp('The answer is incorrect – you guessed too high.');
disp('Guess Again!!!');
elseif(Y<P) %If lesser then this message
disp('The answer is incorrect – you guessed too low.');
disp('Guess Again!!!');
end
end

Output:

Enter a value for X: 3
Enter answer for 3*X^2-10: 25
The answer is incorrect – you guessed too high.
Guess Again!!!
Enter answer for 3*X^2-10: 14
The answer is incorrect – you guessed too low.
Guess Again!!!
Enter answer for 3*X^2-10: 19
The answer is incorrect – you guessed too high.
Guess Again!!!
Enter answer for 3*X^2-10: 16
The answer is incorrect – you guessed too low.
Guess Again!!!
Enter answer for 3*X^2-10: 18
The answer is incorrect – you guessed too high.
Guess Again!!!
Enter answer for 3*X^2-10: 17
The answer is correct!Bye.

---------------------------------------------------------------------------------------------

Enter a value for X: 2
Enter answer for 3*X^2-10: 10
The answer is incorrect – you guessed too high.
Guess Again!!!
Enter answer for 3*X^2-10: 1
The answer is incorrect – you guessed too low.
Guess Again!!!
Enter answer for 3*X^2-10: 1.2222222
The answer is close enough.Bye.

-------------------------------------------------------------------------------------------

Enter a value for X: 2
Enter answer for 3*X^2-10: 2
The answer is correct!Bye.