Description
The purpose of this challenge is examine the output and write the C++ source code that produces the given output and interaction.
Requirements
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
int v1, v2, v3;
srand(time(NULL));
// these are examples on how to use the rand() function
// v1, v2 and v3 are assigned random numbers below in the
// ranges shown in the comments
v1 = rand() % 100; // v1 in the range 0 to 99
v2 = rand() % 100 + 1; // v2 in the range 1 to 100
v3 = rand() % 30 + 1985; // v3 in the range 1985-2014
return 0;
}
Sample Interaction / Output
$ [ run your program ] You have $20.00 in your account You must guess my number (it is between 1 and 100) You have a maximum of seven tries to guess my number OK, I have my number How much do you want to wager that you can guess it in seven attempts? 5.00 OK, guess my number: 30 Your number is higher than mine. You have six attempts left OK, guess my number: 20 Your number is lower than mine. You have five attempts left OK, guess my number: 24 Your number is higher than mine. You have four attempts left OK, guess my number: 22 You guessed it! You now have $25.00 in your account Play again (y/n)? y You have $25.00 in your account You must guess my number (it is between 1 and 100) You have a maximum of seven tries to guess my number OK, I have my number How much do you want to wager that you can guess it in seven attempts? 10.00 OK, guess my number: 40 Your number is lower than mine. You have six attempts left OK, guess my number: 55 Your number is lower than mine. You have five attempts left OK, guess my number: 65 Your number is lower than mine. You have four attempts left OK, guess my number: 75 Your number is lower than mine. You have three attempts left OK, guess my number: 85 Your number is lower than mine. You have two attempts left OK, guess my number: 95 Your number is lower than mine. You have one attempt left OK, guess my number: 100 Your number is higher than mine You failed to guess my number You now have $15.00 in your account Play again (y/n)? n $ [ back to the command prompt ]
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int v1, v2;
char ch;
srand(time(NULL));
double amt=20,w;
while(amt>0)
{
int flag=0;
v2 = rand() % 100 + 1; // v2 in the range 1 to 100
cout<<"You have $"<<amt<<"in your
account\n";
cout<<"You must guess my number (it is between 1 and
100)\n";
cout<<"You have a maximum of seven tries to guess my
number\n";
cout<<"OK, I have my number\n";
cout<<"How much do you want to wager that you can guess it in
seven attempts? ";
cin>>w;
for(int i=7;i>0;i--)
{
cout<<"OK, guess my number: ";
cin>>v1;
if(v1>v2)
{
cout<<"Your number is higher than mine. ";
}
else if(v1<v2)
{
cout<<"Your number is lower than mine. ";
}
else
{
cout<<"You guessed it!\n";
amt=amt+2*w;
flag=1;
break;
}
if(i!=1)
{
cout<<"You have "<<(i-1)<<" attempts
left\n";
}
}
if(flag==0)
cout<<"You failed to guess my number\n";
amt=amt-w;
cout<<"You now have $"<<amt<<" in your
account\n";
cout<<"\n";
cout<<"Play again (y/n)? ";
cin>>ch;
if(ch!='n'&&ch!='N')
{
break;
}
}
return 0;
}
Description The purpose of this challenge is examine the output and write the C++ source code...
C++ Guessing game. Create a project titled Lab4_Guess with a single file titled guess.cpp Program the following game. The computer selects a random number between 1 and 100 and asks the user to guess the number. If the user guesses incorrectly, the computer advises to try larger or smaller number. The guessing repeats until the user guesses the number. The dialog should look as follows (user input is shown in bold): Guess a number between 1 and 100: 50 try...
Has to be written in C++. Visual studio.
Write a C++ program to realize the game of guessing the number. Generally, the game will generate a random number and the player has to find out the number. In each guess, the program will give you a feedback as your guess is correct, too large, or too small. Then the player play repetitively until find out the number. Specifically, the game plays as the following. a). When the game is started,...
Part 1: Python code; rewrite this code in C#, run the program and submit - include comments number= 4 guesscount=0 guess=int(input("Guess a number between 1 and 10: ")) while guess!=number: guesscount=guesscount+1 if guess<number: print("Your guess is too low") elif guess>number: print("Your guess is too high") else: print("You got it!!") guess=int(input("Guess again: ")) print("You figured it out in ",guesscount," guesses") Part 2: C++ code; rewrite the following code in C#. Run the program and submit. - include comments #include <iostream> using...
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...
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...
C++ CS : if die's job is to output an error message alarmingly and terminate the program. You may copy my function definition, or use your own: // The following 4 lines should be present if we have a die function: #include <iostream> // cout, endl #include <string> // string #include <cstdlib> // exit, EXIT_FAILURE using namespace std; bool die(const string & msg){ cout <<"Fatal error: " <<msg <<endl; exit(EXIT_FAILURE); } 1. Tier Prompt the user, then input the student's...
Can someone upload a picture of the code in
matlab
Write a "Guess My Number Game" program. The program generates a random integer in a specified range, and the user (the player) has to guess the number. The program allows the use to play as many times as he/she would like; at the conclusion of each game, the program asks whether the player wants to play again The basic algorithm is: 1. The program starts by printing instructions on the...
the goal is to create a c++ programs with small functions where the main delegates series of functions where the real work takes place. In this programming assignment, you are not allowed to use global variables. Avoid using break (unless working with a switch statement). Limit your functions to no more than 30 statements of code (not including comments or blank lines). Never use a “return” in the middle of a loop! Program Assignment: When I went to Paris over...
Need some help with this C++ code. Please screenshot the code if possible. Purpose: Demonstrate the ability to create and manipulate classes, data members, and member functions. This assignment also aims at creating a C++ project to handle multiple files (one header file and two .cpp files) at the same time. Remember to follow documentation and variable name guidelines. Create a C++ project to implement a simplified banking system. Your bank is small, so it can have a maximum of...
Not in C++, only C code please
In class, we have studied the bisection method for finding a root of an equation. Another method for finding a root, Newton's method, usually converges to a solution even faster than the bisection method, if it converges at all. Newton's method starts with an initial guess for a root, xo, and then generates successive approximate roots X1, X2, .... Xj, Xj+1, .... using the iterative formula: f(x;) X;+1 = x; - f'(x;) Where...