Question

Write a bash program. Let the user select arbitrarily a number between 1 to 200. Let the program ...

Write a bash program. Let the user select arbitrarily a number between 1 to 200. Let the program guess that number.

When the number guess is equal to the number selected by the user then the user will input "Y" (standing for yes). When the guess number is less than the selected number, the user inputs "L" (standing for less) and "M" otherwise. Let the program make minimum number of guesses

0 0
Add a comment Improve this question Transcribed image text
Answer #1
function guess {
  [[ -n $BASH_VERSION ]] && shopt -s extglob
  [[ -n $ZSH_VERSION ]] && set -o KSH_GLOB
  local -i max=${1:-200}
  local -i number=RANDOM%max+1
  local -i guesses=0
 
  local guess
  while true; do
    echo -n "Guess my number! (range 1 - $max): "
    read guess
    if [[ "$guess" != +([0-9]) ]] || (( guess < 1 || guess > max )); then
      echo "Guess must be a number between 1 and $max."
      continue
    fi
    let guesses+=1
    if (( guess < number )); then
      echo "Too low!"
    elif (( guess == number )); then
      echo "You got it in $guesses guesses!"
      break
    else
      echo "Too high!"
    fi
  done
}

sample :

$ guess
Guess my number! (range 1 - 100): 50
Too low!
Guess my number! (range 1 - 100): 75
Too high!
Guess my number! (range 1 - 100): 62
Too low!
Guess my number! (range 1 - 100): 69
Too low!
Guess my number! (range 1 - 100): 72
You got it in 5 guesses!
Add a comment
Know the answer?
Add Answer to:
Write a bash program. Let the user select arbitrarily a number between 1 to 200. Let the program ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For this assignment, you will write a program that guesses a number chosen by your user....

    For this assignment, you will write a program that guesses a number chosen by your user. Your program will prompt the user to pick a number from 1 to 10. The program asks the user yes or no questions, and the guesses the user’s number. When the program starts up, it outputs a prompt asking the user to guess a number from 1 to 10. It then proceeds to ask a series of questions requiring a yes or no answer....

  • Write a program to play "guess my number". The program should generate a random number between...

    Write a program to play "guess my number". The program should generate a random number between 0 and 100 and then prompt the user to guess the number. If the user guesses incorrectly the program gives the user a hint using the words 'higher' or 'lower' (as shown in the sample output). It prints a message 'Yes - it is' if the guessed number is same as the random number generated. ANSWER IN C LANGUAGE. ====================== Sample Input / Output:...

  • Write a program that allows a user to play a guessing game. Pick a random number...

    Write a program that allows a user to play a guessing game. Pick a random number in between 1 and 100, and then prompt the user for a guess. For their first guess, if it’s not correct, respond to the user that their guess was “hot.” For all subsequent guesses, respond that the user was “hot”if their new guess is strictly closer to the secret number than their old guess and respond with“cold”, otherwise. Continue getting guesses from the user...

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • Write a C program that asks the user to think of an integer number from 1...

    Write a C program that asks the user to think of an integer number from 1 to 20. Have the computer guess what the number is by using a binary tree to determine the next guess. You can create the binary tree “by hand” by typing in a set of malloc commands and explicitly linking the nodes. (make the tree completely, then traverse the tree) Use a menu to provide the user with a selection to answer the results of...

  • JAVA PROGRAM: Guessing Game Objective The student will write an individualized program that utilizes conditional flow...

    JAVA PROGRAM: Guessing Game Objective The student will write an individualized program that utilizes conditional flow of control structures in Java. Specifications For this assignment, you will write a program that guesses a number chosen by your user. Your program will prompt the user to pick a number from 1 to 10. The program asks the user yes or no questions, and the guesses the user’s number. When the program starts up, it outputs a prompt asking the user to...

  • IN BASIC JAVA and using JAVA.UTIL.RANDOM write a program will randomly pick a number between one...

    IN BASIC JAVA and using JAVA.UTIL.RANDOM write a program will randomly pick a number between one and 100. Then it will ask the user to make a guess as to what the number is. If the guess is incorrect, but is within the range of 1 to 100, the user will be told if it is higher or lower and then asked to guess again. If the guess was outside the valid range or not readable by Scanner class, a...

  • Write a C++ program using "for" loop to allow user to guess a certain number say...

    Write a C++ program using "for" loop to allow user to guess a certain number say 55. The loop will end after 5 guesses or if he guesses the right number, which ever comes first. If user enters a number greater than 55, your program should print "You entered a bigger number". If user enters a number smaller than 55, your program should print "You entered a smaller number". If user enters 55 in less than 5 attempt, your program...

  • 2. Create the program guessgame.py discussed in the video in module 1. Make sure you can...

    2. Create the program guessgame.py discussed in the video in module 1. Make sure you can run it without syntax errors.3. Modify that program so that the user is asked to think of a secret number and the computer guesses that number. Here is the interaction: 1. The computer asks the user for the range.2. The user inputs the lowest and highest numbers in the range. The USER thinks of a secret number in that range. 3. The computer tries...

  • In c# create a program that generates a random number from 1 to 1000. Then, ask...

    In c# create a program that generates a random number from 1 to 1000. Then, ask the user to guess the random number. If the user's guess is too high, then the program should print "Too High, Try again". If the user's guess is too low, then the program should print "Too low, Try again". Use a loop to allow the user to keep entering guesses until they guess the random number correctly. Once they figure it, congratulate the user....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT