Each of these functions will process an Area calculation: (i.e. for a circle, yo
ID: 2318613 • Letter: E
Question
Explanation / Answer
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: "; // 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: ";
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: ";
cin >> length;
cout << " Enter the breadth of the Rectangle: ";
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: ";
cin >> base;
cout << " Enter the altitude of the triangle: ";
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 << " " << 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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.