Question

Create a C++ program that generates a random number from 1 to 100 using the rand()...

Create a C++ program that generates a random number from 1 to 100 using the rand() function. Give the user a total 5 chances to guess the number. Stop the program and indicate the user guessed the correct number and should be applauded. Also, please find out which guess was the closest to the actual number. For example, if the rand() produces 57 and the user guessed 10, 80, 52, 33 and 44 in their respective turns then print out the user didn’t guess the number and should a 1 mile for closest guess (57 – 52 = 5 miles). (10 points)

Homework Answers

Answer #1

NOTE: rand() and cin may or may not work in all the compilers, mostly in old compilers like turboc++ as it dont have header file for rand().

First we will calculate a random value and store it in the random int variable.

Then we will take user input using for loop. if input matches random value then will print message and break the loop else will go on taking inputs and storing in array.

After taking the inputs if no guess matches we move into the next part by checkig count==5 .

here we will calculate 1st shortest distance between 1st user guess and the random value generated. Then we will calculate temporat random value for the remaining guesses using a loop and check each with shortest in seacrh of new shortest. once we check for all the guesses we have the shortest distace so we print the required message.

----------------------------------------------------------------------------------code----------------------------------------------------------------------------

#include <iostream>
#include<conio>
using namespace std;

int main()
{
int random,num,i=0,count=0,shortest=0,temp;
int arr[5];
   clrscr();
random = rand() % 100 + 1; //generating random number between 1 to 100;

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

   cout<<"\nEnter the guess : ";
   cin>>num;
   if(num==random) ///checking for input as the random number. if yes then breaking with output
   {
   cout<<"Congratulation you guessed the correct number";
   break;
   }
   else //else adding numbers into an array for future use
   {
   arr[count]=num;
   count++;
   }
}

if(count==5) //if count is 5 then array is full means no guess was correct

{
  
   if(arr[0]>random) //checking for bigger value between random and 1st user input

//calculates shortest distance
   {
   shortest = arr[0]-random;
   }
   else
   {
   shortest =random-arr[0];
   }

//calculateing temporary shortest distance between all guess and random

   for(i=1;i<5;i++)
   {
   if(arr[i]>random)
   {

       temp = arr[i]-random;
   }
   else
   {
       temp = random-arr[i];
   }

//comparing shortest value known to temporat shortest

   if(temp<shortest)
   {
       shortest=temp;
   }
   }

//printing final output
   cout<<"user did not guessed the number and should a "<<shortest<<" mile for closest guess";
}
getch();
return 0;
}

Know the answer?
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
Similar Questions
Write a c++ program to pull a random number between 1-100 inclusive. Ask the user to...
Write a c++ program to pull a random number between 1-100 inclusive. Ask the user to guess the number. If the random number is higher than the guess, print "Higher". If the random number is less than the guess, print "Lower". If the random number is equal to the quess, print "Correct!". Create a variable to count the number of guesses and intitialize it to zero. int guesses=0; Increase this variable by 1 every time the user enters a new...
1)Write a program that asks a user for a number. If (and only if) the number...
1)Write a program that asks a user for a number. If (and only if) the number is greater than zero print “The number is valid” 2)Write a program that asks a user for a grade. If (and only if) the grade is greater than zero and less than 101, print “The grade is valid” 3)Write a program that asks a user how many widgets they want to buy and prints out what the user should pay. Widgets costs 10 dollars....
Write a c++ program that has the user guess a number between 1 and 100, you...
Write a c++ program that has the user guess a number between 1 and 100, you program should output correct guess if the user guess the right number, too high if they guess a number higher, too low if the guess a number lower and handle invalid input
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program that comes up with a random number and the player has to guess it. The program output can be like this: I am thinking of a number between 1 and 20. Take a guess. 10 Your guess is too low. Take a guess. 15 Your guess is too low. Take a guess. 17 Your guess is too high. Take a guess. 16 Good job!...
3) Write a program to print out a random number from 1..100. USING ASSEMBLY LANGUAGE
3) Write a program to print out a random number from 1..100. USING ASSEMBLY LANGUAGE
Code a program to reproduce Madame Esmerelda’s clairvoyant test: Mme. Esmerelda conjures up a random number...
Code a program to reproduce Madame Esmerelda’s clairvoyant test: Mme. Esmerelda conjures up a random number 1-10 in her mind and commands the user to guess what it is. If the user’s guess is equal to (remember your double equal sign!) the conjured number, Mme. Esmerelda extends a job offer as a clairvoyant assistant. Add a user-driven loop that allows the user to play repeatedly if desired (but your program should conjure a new random number with every loop iteration)....
Create a program that generates a file of random numbers, and then prints them in neat...
Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers. I) First, you would need to generate a file of random numbers that consists of N random numbers (100 < N < 1000). Each random digit should be a real number (type double) between 0 and 50. This file and its digits would now serve as...
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into...
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into an array and copy the integers from the array into a Binary Search Tree (BST). The program prints out the following: The number of comparisons made to search for a given integer in the BST And The number of comparisons made to search for the same integer in the array Question 3 Run the program developed in Question 2 ten times. The given values...
R-S-P Requirement: - write one C++ program that mimics the Rock-Scissor-Paper game. This program will ask...
R-S-P Requirement: - write one C++ program that mimics the Rock-Scissor-Paper game. This program will ask user to enter an input (out of R-S-P), and the computer will randomly pick one and print out the result (user wins / computer wins). - User's input along with computer's random pick will be encoded in the following format: -- user's rock: 10 -- user's scissor:          20 -- user's paper:            30 -- comp's rock:            1 -- comp's scissor:        2 -- comp's...
write a C++ program that display a prime numbers between 1 and 100 number. Plase print...
write a C++ program that display a prime numbers between 1 and 100 number. Plase print the remaining primes by “dots” For gexample The output should appear like The prime numbers between 1 and 100 are: 1, 2, 3, 5, 7, .........
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT