Question

In: Electrical Engineering

this has to be coded in c ++ • Write a program which generates a DIFFERENT...

this has to be coded in c ++

• Write a program which generates a DIFFERENT random number between 1 and 100 everytime you run the program. Please go online and see how poeple are trying to
solve this. You can submit their code. Make sure you include a comment which shows
from where the code was taken. You do not need to understand the code but if you
can, thats great. This program will be used in the upcoming labs so you are strongly
encouraged to do it now so that you can use it later.
• Using the code from Part 1, play a game where you have to guess the random number
generated. When the user makes an incorrect guess, print a message which says that
you either guessed too low or too high. The user gets a total of 5 chances to guess
the number. If the user manages to guess the number within 5 chances, then print a
message saying that the user won the game and took %d chances

Solutions

Expert Solution

c++ code

#include <iostream>

#include <cstdlib>

using namespace std;

void game(int rand_num)

{

    for (int i=0;i<5;i++){

        int choice;

        cout<<"Enter your guess "<<endl;

        cin>>choice;

        if(choice ==rand_num){ // if choice matches

            cout<<"User Won the game in "<<i+1<< "chances" <<endl;

            break;

        }

        else

        {

            if(rand_num>choice)

            {

                cout<<"you guessed low" <<endl; //to check the condition

               

            }

            else

            {

            cout<<"you guessed high"<< endl;

        }

    }

}

}

int main()

{

    int number= (rand() % 100) + 1; //will generate random numbers 0-99 and then scaled to 0-100 by +1

    game(number);

    cout<<"Actual number is "<<number;

  

    return 0;

}

OUTPUT

NOTE: Rand() can generate any number but sometimes generates number in a compiler .


Related Solutions

Write a C++ program that 1) generates a vector containing 10 different random integers with values...
Write a C++ program that 1) generates a vector containing 10 different random integers with values between 1 and 100, then 2) calculates the average value in that vector in floating point format with 1 decimal place. Output the vector values and the average value to cout. Your program output should look like this: Vector values: 3, 78, 55, 37, 8, 17, 43, 60, 94, 1 Average value: 39.6
C++ DO not use arrays to write this program. Write a program that repeatedly generates three...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three random integers in the range [1, 100] and continues as follows: If the right-most digit of all the three integers is equal, the program displays them in ascending order on the screen and continues. If the generated integers have different right-most digits, they are not displayed and the program continues. The program terminates once the right-most digits of all the three random numbers are...
*Write in C* Write a program that generates a random Powerball lottery ticket number . A...
*Write in C* Write a program that generates a random Powerball lottery ticket number . A powerball ticket number consists of 5 integer numbers ( between 1-69) and One power play number( between 1-25).
Write a program in C++ coding that generates a random number between 1 and 500 and...
Write a program in C++ coding that generates a random number between 1 and 500 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Count the number...
C++. Write a program that generates a random number between 5 and 20 and asks the...
C++. Write a program that generates a random number between 5 and 20 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display Too high. Try again. If the user’s guess is lower than the random number, the program should display Too low, Try again. The program should use a loop that repeats while keeping a count of the number of guesses the user makes until...
Write a C++ program that randomly generates N integer numbers (such that N is entered by...
Write a C++ program that randomly generates N integer numbers (such that N is entered by the user) and then stores them to a text file (myNumbers.txt) sorted in increasing (non-decreasing) order. Again, please notice that the size of the data (N) is known during the run time, not the compile-time (needs to be entered by the user after running the program).
Write a program in C to perform the following: Generates an array of 10 double random...
Write a program in C to perform the following: Generates an array of 10 double random values between 1.0 and 100.0 – assume these are prices for 10 items that a store sells Generates an array of 10 integer random values between 0 and 200 – assume that these are the number of items/units (first array) sold Generate another array called “itemSale” which would contain the total sale for each item. Calculate the total sale for this store and display...
Program must be in C++! Write a program which: Write a program which uses the following...
Program must be in C++! Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to...
Write a program in C++ that generates and displays the first N three digit odd numbers....
Write a program in C++ that generates and displays the first N three digit odd numbers. Whereas the number N is provided by the user.
Random Number Guessing Game Write a program in C++ that generates a random number between 1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess is lower than the random number, the program should display “Too low. Try again.” The program should use a loop that repeats until the user correctly guesses the random number....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT