Programming Fundamentals Assignment No 02

Here is the Programming Fundamentals Assignment No 02

Question:

Write a program that generates a number between 1 and 100, inclusive. Then let the user try to guess the generated number. Start the guess at the midpoint (50) and ask the system if the randomly generated number is higher, lower, or equal to the guess. If the system indicates lower, guess the new midpoint (25). If it indicates higher, guess the new midpoint (75). Continue efficiently guessing higher or lower until they indicate equal, then print the number of guesses required to guess the number and end the program.

Code:

Here is the Programming Fundamentals Assignment No 02

#include<iostream>

using namespace std;
int main()
{

int random_num,guess_num=50,no_tries=0,min=0,max=100,x;     
srand(time(0));
random_num = min+(rand() % (max-min+ 1));

while(guess_num!=random_num)
{

    if(random_num>guess_num)
    {
        x=x/2;
        if(x<1)
        x=1;
        guess_num=guess_num+x;              
    }
    if(random_num<guess_num)
    {
        x=x/2;
        if(x<1)
        x=1;
        guess_num=guess_num-x;
    }
        no_tries++; 
}



    cout<<"THE RANDOM NUMBER IS "<<guess_num<<" WHICH IS GUESSED IN "<<no_tries<<" TRIES."<<endl;
    return 0;




return 0;

}

 

For more details about Huffman coding click here

For other assignments and quizzes click here 

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *