1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Brrunno [24]
3 years ago
14

In this warm up project, you are asked to write a C++ program proj1.cpp that lets the user or the computer play a guessing game.

The computer randomly picks a number in between 1 and 100, and then for each guess the user or computer makes, the computer informs the user whether the guess was too high or too low. The program should start a new guessing game when the correct number is selected to end this run (see the sample run below).
Check this example and see how to use functions srand(), time() and rand() to generate the random number by computer.

Example of generating and using random numbers:

#include
#include // srand and rand functions
#include // time function
using namespace std;

int main()
{
const int C = 10;

int x;
srand(unsigned(time(NULL))); // set different seeds to make sure each run of this program will generate different random numbers
x = ( rand() % C);
cout << x << endl;
x = ( rand() % C ); // generate a random integer between 0 and C-1
cout << x << endl;
return 0;
}
Computers and Technology
1 answer:
Nana76 [90]3 years ago
6 0

Answer:

#include <iostream>

#include <time.h>

using namespace std;

int main()

{

// Sets the random() seed to a relatively random number

srand(time(0));

// Variables are defined

int RandomNumber = rand() % 100 + 1, UserSelection, Tries = 5;

// Gets a number from the user

cout << " Guess a number between 1 and 100, you have five tries to find the correct number.\n :";

cin >> UserSelection;

// Prevents the user from selecting a number out of bounds

while (UserSelection > 100 || UserSelection < 1)

{

 cout << " I said between 1 and 100. select a new number BETWEEN 1 AND 100!\n :";

 cin >> UserSelection;

}

// Stuck in while till they guess right, or run out of tries

while (UserSelection != RandomNumber)

{

 // kicks user from the loop when they run out of tries

 Tries -= 1;

 if (Tries == 0)

 {

  break;

 }

 // Tells the user they got the wrong number and how many tries they have left

 cout << " The Number was not correct, you have " << Tries << " more Guess(es) left.";

 // Tells the user if they are above the number

 if (UserSelection > RandomNumber)

 {

  cout << " Try guessing a little lower\n :";

 }

 // Tells the user if they are bellow the number

 else if (UserSelection < RandomNumber)

 {

  cout << " Try guessing a little higher\n :";

 }

 // User input if the number is wrong they stay in the loop, if they are right they fail the condition for the loop and get dialogue according to how many tries they have left

 cin >> UserSelection;

 // Prevents the user from selecting a number out of bounds

 // If the number is greater than 100 or less than 1 they are prompted to select a new number

 while (UserSelection > 100 || UserSelection < 1)

 {

  cout << " I said between 1 and 100. select a new number BETWEEN 1 AND 100!\n :";

  cin >> UserSelection;

 }

}

// The amount of tries the user has left determines what dialogue they get

// First try win

if (Tries == 5)

{

 cout << " That's not luck, that's skill. you got the number right on the first try.\n ";

 system("pause");

}

// Second try win

else if (Tries == 4)

{

 cout << " That's pretty good luck, you guessed it right on the second try.\n ";

 system("pause");

}

// Third try win

else if (Tries == 3)

{

 cout << " Could be better, but it could also be way worse. You got it right on the third try.\n ";

 system("pause");

}

// Fourth try win

else if (Tries == 2)

{

 cout << " Could be worse, but it could also be a lot better. You got it right on your fourth try.\n ";

 system("pause");

}

// Fifth try win

else if (Tries == 1)

{

 cout << " I hope you don't gamble much. You got it right on your last guess.\n ";

 system("pause");

}

// Losing dialogue

else

{

 cout << " You guessed wrong all five times, the right number was " << RandomNumber << "\n ";

 system("pause");

}

return 0;

}

Explanation

C++, console based guessing game.

Int Tries at the top of main is linked to how many tries the user has

Read the comments in the code they roughly explain whats going on.

You might be interested in
Rainfall_mi is a string that contains the average number of inches of rainfall in Michigan for every month (in inches) with ever
const2013 [10]

Answer:

Hi!

The answer in Javascript is:

function monthsWithMoreOfThree() {

  var Rainfall_mi = '3,4,1,5,2,6,7,9'; <em>//Defined and declared</em>

  var num_rainy_months = 0;

  var values = Rainfall_mi.split(","); <em>//use split method for Strings. Array is returned</em>

   for (var i = 0; i < values.length; i++) { <em>//for each value, compares with 3</em>

     if(values[i] > 3) { <em>//In Javascript, you can compare an String value with an int. Can use parseInt(aString) to explicit parse but is not necessary</em>

       num_rainy_months++; <em>//If value is greater than 3, adds 1 to num_rainy_months</em>

     }

   }

}

5 0
3 years ago
Define a function calc_pyramid_volume with parameters base_length, base_width, and pyramid_height, that returns the volume of a
Sever21 [200]

The missing segment of the code illustrates the use of functions.

Functions are also referred to as <em>procedures or methods</em>; they are set of instructions that act as one.

The code segment that complete the code in the question is as follows:

<em>def calc_pyramid_volume(length, width, height): </em>

<em>    baseArea = length * width </em>

<em>    Volume = baseArea * height * 1/3 </em>

<em>    return Volume</em>

<em />

<em />

The first line of the code segment declares the function itself

<em>def calc_pyramid_volume(length, width, height):</em>

Then, the base area of the pyramid is calculated

<em>    baseArea = length * width</em>

Then, the volume of the pyramid is calculated

<em>    Volume = baseArea * height * 1/3</em>

Lastly, the volume is returned to the main method

<em>    return Volume</em>

So, the complete code (without comments) is:

<em>def calc_pyramid_volume(length, width, height): </em>

<em>    baseArea = length * width </em>

<em>    Volume = baseArea * height * 1/3 </em>

<em>    return Volume </em>

<em>     </em>

<em>length = float(input()) </em>

<em>width = float(input()) </em>

<em>height = float(input()) </em>

<em>print('Volume for', length, width, height, "is:", calc_pyramid_volume(length, width, height))</em>

<em />

See attachment for the sample run

Read more about functions at:

brainly.com/question/17225124

8 0
2 years ago
"Na2SO3” represents sodium sulfite.
STatiana [176]
There are four atoms
5 0
3 years ago
Read 2 more answers
Write a program that will predict the size of a population of organisms. The program should ask the user for the starting number
sineoko [7]

Answer:

// using c++ language

#include "stdafx.h";

#include <iostream>

#include<cmath>

using namespace std;

//start

int main()

{

 //Declaration of variables in the program

 double start_organisms;

 double daily_increase;

 int days;

 double updated_organisms;

 //The user enters the number of organisms as desired

 cout << "Enter the starting number of organisms: ";

 cin >> start_organisms;

 //Validating input data

 while (start_organisms < 2)

 {

     cout << "The starting number of organisms must be at least 2.\n";

     cout << "Enter the starting number of organisms: ";

     cin >> start_organisms;

 }

 //The user enters daily input, here's where we apply the 5.2% given in question

 cout << "Enter the daily population increase: ";

 cin>> daily_increase;

 //Validating the increase

 while (daily_increase < 0)

 {

     cout << "The average daily population increase must be a positive value.\n ";

     cout << "Enter the daily population increase: ";

     cin >> daily_increase;

 }

 //The user enters number of days

 cout << "Enter the number of days: ";

 cin >> days;

 //Validating the number of days

 while (days<1)

 {

     cout << "The number of days must be at least 1.\n";

     cout << "Enter the number of days: ";

     cin >> days;

 }

 

 //Final calculation and display of results based on formulas

 for (int i = 0; i < days; i++)

 {

     updated_organisms = start_organisms + (daily_increase*start_organisms);

     cout << "On day " << i + 1 << " the population size was " << round(updated_organisms)<<"."<<"\n";

     

     start_organisms = updated_organisms;

 }

 system("pause");

  return 0;

//end

}

Explanation:

6 0
3 years ago
Find the double word-length 2's complement representation of each of the following decimal numbers:a. 3874
miss Akunina [59]

Answer:

-3874₁₀ = 1111 1111 1111 1111 1111 1111 1101 1110₂

Explanation:

2's complement is a way for us to represent negative numbers in binary.

To get 2's complement:

1. Invert all the bits

2. Add 1 to the inverted bits

Summary: 2's complement = -N = ~N + 1

1. Inverting the number

3874₁₀ = 1111 0010 0010₂

~3874₁₀ = 0000 1101 1101₂

2. Add 1 to your inverted bits

~3874₁₀ + 1 = 0000 1101 1101₂ + 1

= 0000 1101 1110₂

You can pad the most signigicant bits with 1's if you're planning on using more bits.

so,

12 bits                          16 bits

0000 1101 1110₂  = 1111 0000 1101 1110₂

They asked for double word-length (a fancy term for 32-bits), so pad the left-most side with 1s' until you get a total of 32 bits.

           32 bits

= 1111 1111 1111 1111 1111 1111 1101 1110

7 0
3 years ago
Other questions:
  • WILL GIVE BRANLYIST
    6·1 answer
  • Application Software include programs like: MS Word, Google Docs, MS Exce ect...
    13·1 answer
  • Which word in brackets is most opposite to the word in capitals? PROSCRIBE (allow, stifle, promote, verify)​
    14·2 answers
  • What is meant by encapsulating semaphores? Bring out the need for it
    12·1 answer
  • By using the search functionality within a twitter stream, users can filter for:
    9·1 answer
  • Gary has to complete a form before enrolling into an online course. A few of the fields require Gary to respond with either a "Y
    7·2 answers
  • What would a digitizing application create?
    13·2 answers
  • How many clients has<br> Accenture engaged globally on blockchain?
    12·1 answer
  • How many voltage values can be represented with a 10-bit binary code?
    15·1 answer
  • Are you absent minded because you are thinking of an online activity?​
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!