# Algorithm in C++. with extra credit.

**Question description**

Algorithm and tips for Assignment 4.

Tip: Use the following code inside the main function to repeat your program without compiling it every time:

char response = 'y';

while (response == 'y')

{

//most of your code goes here

cout << "Would you like to run the program again? \n" << "Enter y for yes or n for no: ";

cin >> response;

}

You can modify the code as you wish, just make sure the loop is intact

Question 1 Algorithm

1) Declare your dimensions (i.e. length, width, height, radius, volume)

2) Get constant, for π, store in pi

3) Inform user (via output) about selection of choices (You can use numbers or letters or symbols)

4) Get choices from user, store in choice

5) In switch statement:

a. Create cases to match 6 choices and don’t forget the default!

b. For each case:

i. Get dimensions for the choice (example: radius, and height for the cylinder)

ii. Calculate volume using given formula (see assignment for formula. At this point, you know how to express the formula in C++)

iii. Don’t forget to break after each case!

6) Apply while loop for repetition of program, using code from tip.

Sample output for HW 4 Question:

This program calculates the volume of several 3D objects.

Here are your choices:

Enter 1 for cylinder

Enter 2 for sphere

Enter 3 for pyramid

Enter 4 for cone

Enter 5 for cube

Enter 6 for rectangular prism

Please enter your choice now:

8 You have entered an invalid choice.

Please run the program again.

Would you like to run this program again? Enter 'y' for yes and 'n' for no: y

This program calculates the volume of several 3D objects.

Here are your choices:

Enter 1 for cylinder

Enter 2 for sphere

Enter 3 for pyramid

Enter 4 for cone

Enter 5 for cube

Enter 6 for rectangular prism

Please enter your choice now:

3 You wish to find the volume of a pyramid.

Enter the length, width and height: 5 6 7 The volume of the pyramid is 70 cubic units.

Would you like to run this program again? Enter 'y' for yes and 'n' for no: n

Press any key to continue . . .

Extra Credit Algorithm: 1) Get computer’s randomly generated number, store in num a. Seed the random number generator b. Set the range of random numbers using random number formula

2) Create a variable for counting the number of guesses, store in tries

3) Get guesses from user, store in guess

4) Set up loop to compare guesses (see pseudocode) while (they haven’t guessed the number && they have tried to guess 5 or less times) { if (guess is less than number) output “guess is too low” else if (guess is greater than number) output “guess is too high” get another guess increment tries }

5) Output the value to be guessed if (they guessed the number) output congrats, and confirm the value they guessed by outputting num else (if they haven’t guessed the number and they used up all five tries) tell the user that they lost and reveal the value in num

6) Apply while loop for repetition of program, using code from tip.Sample output for Extra Credit: I'm thinking of a number between 1 and 100. Can you guess what it is? You have five tries! What's your guess? 76 Nope, that's too high. Guess again: 64 Nope, that's too high. Guess again: 53 Nope, that's too low. Guess again: 57 Nope, that's too high. Guess again: 55 You used up all 5 tries! The number I was thinking of was 56 Would you like to run the program again? Enter 'y' for yes or 'n' for no: y I'm thinking of a number between 1 and 100. Can you guess what it is? You have five tries! What's your guess? 32 Nope, that's too low. Guess again: 64 Nope, that's too high. Guess again: 48 Congratulations! You guessed it! I was thinking of the number 48. You took 3 tries! Would you like to run the program again? Enter 'y' for yes or 'n' for no: n

Press any key to continue . . .

## Tutor Answer

## Review from our student for this Answer

Brown University

1271 Tutors

California Institute of Technology

2131 Tutors

Carnegie Mellon University

982 Tutors

Columbia University

1256 Tutors

Dartmouth University

2113 Tutors

Emory University

2279 Tutors

Harvard University

599 Tutors

Massachusetts Institute of Technology

2319 Tutors

New York University

1645 Tutors

Notre Dam University

1911 Tutors

Oklahoma University

2122 Tutors

Pennsylvania State University

932 Tutors

Princeton University

1211 Tutors

Stanford University

983 Tutors

University of California

1282 Tutors

Oxford University

123 Tutors

Yale University

2325 Tutors