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

Write a program which accepts a number, between 0 and 128, from player 1 and the

ID: 3631221 • Letter: W

Question

Write a program which accepts a number, between 0 and 128, from player 1 and then accepts guesses from player 2. If player 2’s guess is above or below player 1's number, print out a message, "too high" or "too low" respectively.

When the number is guessed correctly the program will printout "correct" and the number of tries. Exclude the numbers 0 and 128. How many tries are required to guarantee a solution? (HINT: binary search) Discuss your results in the “Comments” section.

Explanation / Answer

Dont copy it with the dotted lines. # .data # variable declarations follow this line .align 2 title: .asciiz " Welcome to the take a guess game. " divbar: .asciiz " =================================================" newline: .asciiz " " p1prompt: .asciiz " Player 1, please enter the number to be guessed between 0 and 128, exclusive: " p2prompt: .asciiz " Player 2, please guess a number between 0 and 128, exclusive: " invalidNb: .asciiz " Invalid number! The number has to be between 0 and 128, exclusive!!! " sayHigh: .asciiz " Guessed number is too high, try again. " sayLow: .asciiz " Guessed number is too low, try again. " validTries: .asciiz " Number of valid tries: " sayGuess: .asciiz " Congratulations, you guessed the number!!!" playAgain: .asciiz " Do you want to play again? 1 for yes, 0 for no " .text # instructions follow this line .globl main main: #divbar title li $v0, 4 la $a0, divbar syscall #title li $v0, 4 la $a0, title syscall addi $t0, $0, 0 #player 1 number init addi $t1, $0, 0 #player 2 number init addi $t2, $0, 0 #counter of tries init addi $t3, $0, 0 #continue init inputPlayer1: #player 1 prompt li $v0, 4 la $a0, p1prompt syscall #read int from console li $v0, 5 syscall move $t0, $v0 blt $t0, 1, invalidInput1 #if $t1 < 1, branch to invalidInput1 bgt $t0, 127, invalidInput1 #if $t1 > 127, branch to invalidInput1 #larger than 127 since 128 is not included and less than 1 since 0 is not included. loop: inputPlayer2: #player 2 prompt li $v0, 4 la $a0, p2prompt syscall #read int from console li $v0, 5 syscall move $t1, $v0 #move integer in $v0 to $t1 blt $t1, 1, invalidInput2 #if $t1 < 1, branch to invalidInput2 bgt $t1, 127, invalidInput2 #if $t1 > 127, branch to invalidInput2 addi $t2, $t2, 1 #increase counter of tries by one bgt $t1, $t0, tooHigh #if $t1 > $t0, number is too high blt $t1, $t0, tooLow #if $t1 < $t0, nubmer is too low j guessed #else ($t1 == $t0), jump to main tooHigh: li $v0, 4 la $a0, sayHigh #output say higher string syscall j loop tooLow: li $v0, 4 la $a0, sayLow #output say higher string syscall j loop guessed: li $v0, 4 la $a0, sayGuess #output you guessed syscall li $v0, 4 la $a0, validTries #output number of tries syscall move $a0, $t2 li $v0, 1 syscall li $v0, 4 la $a0, playAgain #play again string syscall li $v0, 5 #read integer syscall move $t2, $v0 beq $t2, 1, main #if $t3 == 1, branch to main j exit #else ($t3 == 0) jump to exit invalidInput1: #tells you to enter a valid number li $v0, 4 la $a0, invalidNb #invalid number string syscall j inputPlayer1 #jump to inputPlayer1: invalidInput2: #tells you to enter a valid number li $v0, 4 la $a0, invalidNb #invalid number string syscall j inputPlayer2 #jump to inputPlayer2: exit: li $v0, 10 # system call code for exit = 10 syscall # call operating system ------------------------------------------------------------------------------------------------------------- The maximum number of tries required is 7, using logarithm base 2 of 128 (binary search). I'm taking 64 as the middle of the whole thing to make it easier, because the list is odd. By logic, when you get to a point, for example, taking the half point between 124 and 128, which is 126, you input it and get number is too low, then the number to be guessed is 127 since 128 is not included in the set. Please Rate
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote