Question #48784

Given the lengths a,b,c of the sides of triangle, write a function based program to compute the area of the triangle., The formula for computing the area is given by:
Area = √ s(s-a)(s-b)(s-c)
where s is the semiparameter of the triangle
s=(a+b+c)/2
2. Convert the program to calculate the area and circumference of the circle into function by reference code.

Expert's answer

#include <stdlib.h>

#include <iostream>

#include <stdio.h>

#include <cstdlib>

#include <cmath>

using namespace std;

float CalculateAreaTriangle(float a, float b, float c)

{

float s;

float area;

s = (a+b+c)/ 2.0;

area = sqrt( s * (s-a) *(s-b) *(s-c) );

return area;

}

void CalculateAreaCircumference(float radius, float &area, float &circumReference)

{

circumReference = 2 * 3.14 * radius;

area = 3.14 * radius * radius;

}

int main()

{

int menuState;

do

{

do

{

system("cls");

cout<<"What you want to compute : "<<endl;

cout<<"1. Compute the area of the triangle"<<endl;

cout<<"2. Calculate the area and circumference of the circle"<<endl;

cout<<"3. Exit"<<endl;

cin>> menuState;

} while (menuState < 0 && menuState > 3);

switch(menuState)

{

case 1:

{

float a, b, c;

system("cls");

cout<<"Enter the lengths a,b,c of the sides of triangle ";

cin>>a>>b>>c;

cout<<"Area of the triangle : "<<CalculateAreaTriangle(a,b,c)<<endl;

system("pause");

} break;

case 2:

{

float radius, area, circumReference;

system("cls");

cout<<"Enter the radius of the circle ";

cin>>radius;

CalculateAreaCircumference(radius, area, circumReference);

cout<<"Area of the circle : "<<area<<endl;

cout<<"Circumference of the circle : "<<circumReference<<endl;

system("pause");

} break;

case 3: break;

}

} while (menuState != 3);

return 0;

}

#include <iostream>

#include <stdio.h>

#include <cstdlib>

#include <cmath>

using namespace std;

float CalculateAreaTriangle(float a, float b, float c)

{

float s;

float area;

s = (a+b+c)/ 2.0;

area = sqrt( s * (s-a) *(s-b) *(s-c) );

return area;

}

void CalculateAreaCircumference(float radius, float &area, float &circumReference)

{

circumReference = 2 * 3.14 * radius;

area = 3.14 * radius * radius;

}

int main()

{

int menuState;

do

{

do

{

system("cls");

cout<<"What you want to compute : "<<endl;

cout<<"1. Compute the area of the triangle"<<endl;

cout<<"2. Calculate the area and circumference of the circle"<<endl;

cout<<"3. Exit"<<endl;

cin>> menuState;

} while (menuState < 0 && menuState > 3);

switch(menuState)

{

case 1:

{

float a, b, c;

system("cls");

cout<<"Enter the lengths a,b,c of the sides of triangle ";

cin>>a>>b>>c;

cout<<"Area of the triangle : "<<CalculateAreaTriangle(a,b,c)<<endl;

system("pause");

} break;

case 2:

{

float radius, area, circumReference;

system("cls");

cout<<"Enter the radius of the circle ";

cin>>radius;

CalculateAreaCircumference(radius, area, circumReference);

cout<<"Area of the circle : "<<area<<endl;

cout<<"Circumference of the circle : "<<circumReference<<endl;

system("pause");

} break;

case 3: break;

}

} while (menuState != 3);

return 0;

}

## Comments

## Leave a comment