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
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....
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...
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, .........
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop...
Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop to print the numbers from 42 - 56. Uses a for loop to print the numbers from 87 - 95. Asks the user for 2 numbers. Uses a loop to print all numbers between the given numbers, inclusive. Note: Consider that your user's second number can be lower! (see example below) Note: Also consider that your user might give you two of the same...
PYTHON 1.      Complete the attached template program pattenrs.py to create and display a number of patterns. The...
PYTHON 1.      Complete the attached template program pattenrs.py to create and display a number of patterns. The program works with a single digit. The default value is 6, but user to change it as a menu selection. 2.      Program should display the patterns below. Additionally, you need to create your own pattern. 3.      Do not modify myPatterns6 driver. 4.      Make sure you document the program and its functions. 5.      PatternI is already implemented. 6.      Create five additional functions (similar to PatternI function) to implement Patterns II,...
Python: Write a program that prompts the user to enter a positive integer value, and compute...
Python: Write a program that prompts the user to enter a positive integer value, and compute the following sequence: • If the value is even, halve it. • If it's odd, multiply by 3 and add 1. • Repeat this process until the value is 1, printing out each value. • Then print out how many of these operations you performed. If the input value is less than 1, print a message containing the word Error and exit the program....
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...