# algorithm in c with extra credit

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

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.

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

8 You have entered an invalid choice.

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.

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

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 . . .