O slideshow foi denunciado.
Seu SlideShare está sendo baixado. ×

Write a function named guessme- The function should ask you to guess a.docx

Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio

Confira estes a seguir

1 de 2 Anúncio

Write a function named guessme- The function should ask you to guess a.docx

Baixar para ler offline

Write a function named guessme. The function should ask you to guess a number between 1 and 100. The function should allow three attempts to guess the number. It should respond with \'Guess a lower number\', \'Guess a higher number\',\'YOU WIN !!!\',or \'No, the number is __\' . Use rng(\'shuffle\') and randi() to generate the number.
Solution
% This is a guessing game
counter=0;
real_number=rand(1);
real_number=100*real_number;
number=ceil(real_number);
fprintf(\'I am thinking a number between 1 and 100.\ \');
guess=input(\'Guess a number: \');
while guess~= number
counter = counter + 1;
if guess>number
fprintf(\'Your guess is too high!\ \');
guess=input(\'Guess another number: \');
else
fprintf(\'Your guess is too low!\ \');
guess=input(\'Guess another number: \');
end
end
counter= counter + 1;
fprintf(\'Congratulations! You guessed the correct number in %g shots. \ \', counter);
.

Write a function named guessme. The function should ask you to guess a number between 1 and 100. The function should allow three attempts to guess the number. It should respond with \'Guess a lower number\', \'Guess a higher number\',\'YOU WIN !!!\',or \'No, the number is __\' . Use rng(\'shuffle\') and randi() to generate the number.
Solution
% This is a guessing game
counter=0;
real_number=rand(1);
real_number=100*real_number;
number=ceil(real_number);
fprintf(\'I am thinking a number between 1 and 100.\ \');
guess=input(\'Guess a number: \');
while guess~= number
counter = counter + 1;
if guess>number
fprintf(\'Your guess is too high!\ \');
guess=input(\'Guess another number: \');
else
fprintf(\'Your guess is too low!\ \');
guess=input(\'Guess another number: \');
end
end
counter= counter + 1;
fprintf(\'Congratulations! You guessed the correct number in %g shots. \ \', counter);
.

Anúncio
Anúncio

Mais Conteúdo rRelacionado

Semelhante a Write a function named guessme- The function should ask you to guess a.docx (20)

Mais de lez31palka (20)

Anúncio

Mais recentes (20)

Write a function named guessme- The function should ask you to guess a.docx

  1. 1. Write a function named guessme. The function should ask you to guess a number between 1 and 100. The function should allow three attempts to guess the number. It should respond with 'Guess a lower number', 'Guess a higher number','YOU WIN !!!',or 'No, the number is __' . Use rng('shuffle') and randi() to generate the number. Solution % This is a guessing game counter=0; real_number=rand(1); real_number=100*real_number; number=ceil(real_number); fprintf('I am thinking a number between 1 and 100. '); guess=input('Guess a number: '); while guess~= number counter = counter + 1; if guess>number fprintf('Your guess is too high! '); guess=input('Guess another number: '); else fprintf('Your guess is too low! '); guess=input('Guess another number: ');
  2. 2. end end counter= counter + 1; fprintf('Congratulations! You guessed the correct number in %g shots. ', counter);

×