Each of these functions will process an Area calculation ie


Each of these functions will process an Area calculation: (i.e. for a circle, your function would be the followingn4 steps prompt the User for to enter a radius. Read in the radius, calculate the area by using the formula a -pi *Radius squared. The print out the area of the circle.) Lastly modify the if statements switch statement whichever one you used) in your main function. Your main function will no longer do all the work to process the area calculations. Your main() function will simple call the 4 new functions that you just built. If the uses enters a \'C\', call the function \"calcAreaCircle[]\" to process the calculation of the area of a circle. Do the same for the other 3 figures. Project B - Build a simple Game Program The Game is called Twenty One. The user rolls in an attempt to hit exactly 21. If the user goes over 21, they low. If they hit 21 exactly, they win. Then they can play the game again if they want. At first the user should 2 dice. Once their total hits 15 or above, they should only roll are disc. (Use a random number generator to simulate the rolls.)

Solution

Answer for 1.

#include<iostream>
#define PI 3.14159265358979323846
//PI is a constant that is used in calculation of area of circle
using namespace std;

//declaration of functions
void calcAreaCircle();
void calcAreaSquare();
void calAreaRectangle();
void calAreaTriangle();

int main()
{
char c; // c is a input character prompt to get the type of operation
cout << \"Enter C for circle, S for square, R for rectangle, T for triangle:\ \ \";
cin >> c;
switch(c){
case \'C\': // if user press \'C\' then calcAreaCircle() is called
calcAreaCircle();
break;
case \'S\': // if user press \'S\' then calcAreaSquare() is called
calcAreaSquare();
break;
case \'R\': // if user press \'R\' then calAreaRectangle() is called
calAreaRectangle();
break;
case \'T\': // if user press \'C\' then calAreaTriangle() is called
calAreaTriangle();
break;
default: // if user presses any other character, then it exits displaying the error message
cout << \"\ Error and Exiting...Correct value not entered\ \";
}
return 0;
}

void calcAreaCircle(){
float radius,area=0;
cout << \"\ Enter the radius of the circle:\\t\"; // input prompt
cin >> radius; // read input
area = PI * radius * radius; // calculate area, PI is defined before
cout << \"\ Area of the circle : \" << area; // display area
cout << \"\ \ \";
return;
}

void calcAreaSquare(){
float side_length,area=0;
cout << \"\ Enter the side length of the square:\\t\";
cin >> side_length;
area = side_length * side_length;
cout << \"\ Area of the square : \" << area;
cout << \"\ \ \";
return;
}

void calAreaRectangle(){
float length,breadth,area=0;
cout << \"\ Enter the length of the Rectangle:\\t\";
cin >> length;
cout << \"\ Enter the breadth of the Rectangle:\\t\";
cin >> breadth;
area = length * breadth;
cout << \"\ Area of the rectangle : \" << area;
cout << \"\ \ \";
return;
}

void calAreaTriangle(){
float base,altitude,area=0;
cout << \"\ Enter the base of the triangle:\\t\";
cin >> base;
cout << \"\ Enter the altitude of the triangle:\\t\";
cin >> altitude;
area = (1.0/2.0) * base * altitude; // 1.0/2.0 is used to for floating point calculation
cout << \"\ Area of the triangle : \" << area;
cout << \"\ \ \";
return;
}

Answer for 2:

#include <iostream>
#include <cstdlib>
#include <time.h>
//stdlib and time required for srand and time
using namespace std;

int main(){
start:
int score=0, die1, die2; // score count and dice 1 and 2
srand((unsigned)time(NULL));
while(score < 15){ // if score is less than 15, keep rolling two dices
cout << \"\ \ Press enter to roll two dices...\";
cin.get();
die1 = (rand() % 6) + 1; // roll die 1
die2 = (rand() % 6) + 1; // roll die 2

score = score + (die1 + die2); // total score for each rolls
cout << \"\ Your dice rolls : \" << die1 << \"\\t\" << die2;
cout << \"\ Total Score: \" << score ;
}
while(score<21){ // if score > 15 and <21 keep rolling 1 die at a time
cout << \"\ \ Press enter to roll one dice...\";
cin.get();
die1 = (rand() % 6) + 1; // roll 1 die
score = score + die1; // add score
cout << \"\ Your dice roll : \" << die1 ;
cout << \"\ Total Score: \" << score ;
}
cout << \"\ \ Final Score : \" << score; // final score
if (score == 21){
cout << \"\ \ Woww....You won\ \"; // if ==21 then you win, and play again
char c; // press Y to play again
cout << \"Press \'Y\' to play again, else type any key to end\ \";
cin >> c;
if (c == \'Y\')
goto start;
else
exit;
}
else{
cout << \"\ \ You lost\"; // if >21 you lose, and exit
}
return 0;
}

 Each of these functions will process an Area calculation: (i.e. for a circle, your function would be the followingn4 steps prompt the User for to enter a radiu
 Each of these functions will process an Area calculation: (i.e. for a circle, your function would be the followingn4 steps prompt the User for to enter a radiu
 Each of these functions will process an Area calculation: (i.e. for a circle, your function would be the followingn4 steps prompt the User for to enter a radiu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site