Question

(a) Design an algorithm that reveals some secret integer number from the set {1, 2, ......

(a) Design an algorithm that reveals some secret integer number from the set {1, 2, ... , n} by guessing random numbers within the given range until the secret number has been guessed. Numbers should be replaced after being guessed such that ​it is possible to guess 2 and then 2 again​, assuming 2 is in the given range. The algorithm should return both the secret number as well as the number of guesses taken.

(b) If possible, calculate the tightest Big-​O​ approximation for the average runtime complexity of the algorithm from part (a). If it is not possible, explain why not. Note: assume that choosing a random number takes ​O(1)​ time.

(c) If possible, calculate the tightest Big-​O​ approximation for the worst case runtime complexity of the algorithm from part (a). If it is not possible, explain why not. Note: assume that choosing a random number takes ​O(1)​ time.

(d) In a single run, is it possible that of the algorithm from part (a) finds the secret number in fewer guesses than a standard binary search algorithm? If so, please provide a concrete example of one such situation.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1->

assuming set containing secret intezer is all intezers from 1 to N

implementation of algorithm in c++:

#include <iostream>
#include <algorithm>
#include <random>
#include <time.h>
using namespace std;
#define N 20 //let n be 20
int main()
{
    srand(time(0));
    int secret_number = 2;                 //let secret number be 2
    int count = 0;                         //number of guesses taken
    int current_guess = rand() % N + 1;    //choosing random between 1 to n
    count++;                               //increement count
    while (current_guess != secret_number) //repeat till we encounter secret number
    {
        current_guess = rand() % N + 1; //again choosing random number between 1 to n
        count++;                        //increement count
    }
    cout << "Secret number is " << secret_number << " and number of gueses taken is " << count;
}


2nd and 3rd Q are more or less same

Q asks if we can approximate runtime complexity of this algorithm, but the answer is we can't
atleast for this algorithm because we are initializing rand() using srand(time(0)) which always
generates different random intezer each time program runs.

screenshot is attatched which states different number of guesses each time we run program

But if we assumes random number generator generates purely random intezer from 1 to n
based on probabilities,

    then runtime complexity both average as well as worst case is O(n).


4->

    Yes it is possible that this algorithm takes fewer guesses than binary search algorithm
    example is shown in screenshot. Where n is 20 and secret number is 2.

    here complexity of binary search algorithm is log(n), so log(20) ie more than 4 but
in screenshot below it can be seen that in some cases number of guesses is 3 also which
is less than 4.

PS E: fixer> Secret number is PS E:\fixer> .\a.exe Secret number is PS E:\fixer> .\a.exe iSecret number is PS E:\fixer> .\a.e

Add a comment
Know the answer?
Add Answer to:
(a) Design an algorithm that reveals some secret integer number from the set {1, 2, ......
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
  • Exercise 1 Use Top-Down Design to “design” a set of instructions to write an algorithm for...

    Exercise 1 Use Top-Down Design to “design” a set of instructions to write an algorithm for “travel arrangement”. For example, at a high level of abstraction, the algorithm for “travel arrangement” is: book a hotel buy a plane ticket rent a car Using the principle of stepwise refinement, write more detailed pseudocode for each of these three steps at a lower level of abstraction. Exercise 2 Asymptotic Complexity (3 pts) Determine the Big-O notation for the following growth functions: 1....

  • python code for guessing game users enter the number the computer will guess 8:38 PM 100...

    python code for guessing game users enter the number the computer will guess 8:38 PM 100 nstructure.com 1. Number guessing game : (5 points) 1) Requirements: Your program will ask the user to enter a number between 1 and 1000. Then it will try to guess the correct number with the guaranteed minimal average number of guesses. Which search algorithm should you use? 2) Expected Output: Enter a number between 1 and 1000? 784 Computer guesses: 500 Type 'l' if...

  • <Java> 1-Write a program that converts from a string into an integer number. For example: For...

    <Java> 1-Write a program that converts from a string into an integer number. For example: For "123" the program should return 123 For "131", The program should return 131 For "-121" the program should return -121 For "1e3" the program should print an Invalid number For "a1" the program should print an invalid number Do not use Integer.parse function 2. Generate a random number in [0, 100]. Write an efficient guess procedure that takes no more than 7 guesses to...

  • (d) Consider an algorithm A, whose runtime is dependent on some "size" variable n of the...

    (d) Consider an algorithm A, whose runtime is dependent on some "size" variable n of the input. Explain the difference between the two statements below, and give an explicit example of an algorithm for which one statement is true but the other is false. 1. The worst case time complexity of A is n2. 2. A is O(n). (e) Give an example of an algorithm (with a clear input type) which has a Big-Oh (0) and Big-Omega (12) bound on...

  • 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...

  • 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...

  • please c++ with functions *Modify the Guessing Game Write the secret number to a file. Then...

    please c++ with functions *Modify the Guessing Game Write the secret number to a file. Then write each user guess and answer to the file. (on separate lines) Write the number of guesses to the file at the end of the game. After the game is finished, ask the user if they want to play again. If 'n' or 'N' don't play again, otherwise play again! NOTE: your file should have more than one game in it if the user...

  • In PYTHON 3- Implement a subclass (described below) of "Word Guess", a variant of the game...

    In PYTHON 3- Implement a subclass (described below) of "Word Guess", a variant of the game Hangman. In this game, a word is first randomly chosen. Initially, the letters in the word are displayed represented by "_”.   For example, if the random word is "yellow”, the game initially displays "_ _ _ _ _ _”. Then, each turn, the player guesses a single letter that has yet to be guessed. If the letter is in the secret word, then the...

  • //SCROLL TO BOTTOM FOR WAYS TO GET A MEETING AND AN EXCEEDING var secretWord = "secretword"...

    //SCROLL TO BOTTOM FOR WAYS TO GET A MEETING AND AN EXCEEDING var secretWord = "secretword" function setup() { createCanvas(400, 400); background(220); } function draw() {    rect(100,150,100,70) text("Click to \nstart hangman",110,175)    rect(225,150,100,70) text("Click to \nguess letter",235,175)    } function mousePressed(){ if(mouseOnRect(100,150,100,70)){ //begin hangman game //create variable to hold secret word as an array split by letter //create variable to hold display word as an array of * or _ that takes the place of each letter //get the...

  • Can you add code comments where required throughout this Python Program, Guess My Number Program, and...

    Can you add code comments where required throughout this Python Program, Guess My Number Program, and describe this program as if you were presenting it to the class. What it does and everything step by step. import random def menu(): #function for getting the user input on what he wants to do print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the...

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