How can I solve this question?

/Write a code that will calculate randomly a number between 1 and 10 (without displaying it) and will ask the user to guess it. If the user answers correctly, the program prints “Good” and add 1 to his score, if not, the program prints “sorry”, gives the correct answer, then selects another number. After 10 consecutive games, the programs stops and prints the final score.
Hint: use a random buitin function (rand).
your guess? 7
Sorry, answer was 5
your guess? 6
Sorry, answer was 0
your guess? 3
Sorry, answer was 1
your guess? 9
Sorry, answer was 0
..
your guess? 1
Sorry, answer was 5
your final score is 0

4 comentarios

Emma Swaggy
Emma Swaggy el 21 de Mayo de 2017

plllllllllllllllllllllllllllllllease someone help me i have to submit it by 24/5/2017

Steven Lord
Steven Lord el 22 de Mayo de 2017
Then I advise you to use some or all of the resources listed in this Answer to learn the basics of how to use MATLAB then try to write a solution to this problem yourself, or ask your professor or teaching assistant for help. If you have difficulty when writing the solution, show what you've done and ask a specific question about where you're stuck and you may receive some guidance.
Krishna Maity
Krishna Maity el 20 de Jul. de 2021
A particle travels across a at surface, moving due cast for 3 m, then due north for 9 m, and then returns to the origin. A force field acts on the particle, given by
F(x,y)=sin(²+²)+In(7+xy) j
Find the work done on the particle by F.
Please solve this using MATLAB.

Iniciar sesión para comentar.

Respuestas (2)

Image Analyst
Image Analyst el 21 de Mayo de 2017

1 voto

Hints:
  1. randi() to generate an integer.
  2. input() or inputdlg() to ask the user for a number.
  3. fprintf() or msgbox() to display a message.
  4. "while" to loop until the user gets the correct answer.

4 comentarios

Emma Swaggy
Emma Swaggy el 22 de Mayo de 2017
But many thanks for your reply but I still don't know how to solve it because I don't have any background about it . So if you know how to do it , i will appreciate .
Image Analyst
Image Analyst el 22 de Mayo de 2017
Try finishing this
finalScore = 0;
for k = 1 : 10
computerGuess = rand(1);
userGuess = input(........
if computerGuess == .............
finalScore = finalScore + 1;
else
fprintf('Sorry..............
end
end
fprintf('Final score.................
Thomas Nguyen
Thomas Nguyen el 7 de Abr. de 2018
Editada: Image Analyst el 8 de Abr. de 2018
@ImageAnalyst: rand(r) returns a rxr matrix of random numbers from 0:1, not a random number from 1:10.
Although I guess you did tell her to finish it :w
Image Analyst
Image Analyst el 8 de Abr. de 2018
Editada: Image Analyst el 8 de Abr. de 2018
I had rand(1) in the code, but I should have put randi(10) like I said in the message. Thanks for the catch.

Iniciar sesión para comentar.

Thomas Nguyen
Thomas Nguyen el 7 de Abr. de 2018

0 votos

Code:
function[] = guessingGame()
prompt_right=('Good job, you guessed correctly'); %msg display when player guessed right
count=0; %score
for i=1:10 %creating a loop to run the question 10 times
inp=input('Your guess? '); %taking player's guess
a=randi(5); %1st random factor for equation 'answer'
b=randi(5); %2nd random factor for equation 'answer'
answer=ceil((a+b)*rand); %equation to calculate a random number between 1 and 10
if inp==answer
disp(prompt_right);
count=count+1; %increase score by one if guessed correctly
else
prompt_wrong=['Sorry, the answer was ',num2str(answer)]; %msg display when player guessed wrong
disp(prompt_wrong);
end
end
prompt_final=['Congratz on finnishing the game! Your final score is: ',num2str(count)]; %msg display when the game is over
disp(prompt_final);
end%guessingGame()
Press F5 in your script editor or run guessingGame in the Command Window to run the code

Categorías

Más información sobre Get Started with MATLAB en Centro de ayuda y File Exchange.

Preguntada:

el 21 de Mayo de 2017

Comentada:

el 20 de Jul. de 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by