Question

/4 Question An area of square, drea ofa rectangle and triangle can be calculated by using the given formula: sqareaa (renww value from the functioi area is reciabgle-(retrn valwe from the function is area int) ceouatre -D-(return value fromtheneion e s double t and s s formula must be use when the values for the sides of the triangle are whether you want to calculate the area of square or a. b and c are the sides of the triangle and s is called the semi perimerer atbte)This known* Your program should ask the user rectangle or triangle before giving the suitable input. Develop suitable overloaded functions area0 to calculate the area of a square and triangle using the different formula mentioned above and return it. The functions must receive the required arguments as pass-by-value. , rectangle Construct the overloaded functions using suitable prototype and use it in your main program. nter the ide of the agsiare t ress any key to cont inue nter the length and breadth of the rectang lo The area of the rectangle ia: 18 Press any key to cont inue . ClWindowslsystem321cmd.exe nter the three sides of t he triongle The cta-ea of the trishle is: 19.6678 ress any key to continue
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The C++ code is as follows:


#include<bits/stdc++.h>
using namespace std;
double area(double a); // function signatures with different parameter decalrations for each
double area(double a,double b);
double area(double a,double b, double c);
int main()
{
cout << "You want area of square(s) or rectangle(r) or triangle(t)?" << endl; // taking the input
char c;
cin >> c;
if(c == 's') // if the user inputted s
{
cout << "Enter the length of side of the square" << endl;
double l;
cin >> l;
double size = area(l); // calling the function
cout << "The area of the square is " << size << endl;
}
if(c == 'r') // if the user inputted r
{
cout << "Enter the two sides of the rectangle" << endl;
double l,b;
cin >> l >> b;
double size = area(l,b); // calling the function
cout << "The area of the rectangle is " << size << endl;
}
if(c == 't') // if the user inputted t
{
cout << "Enter the three sides of the triangle" << endl;
double l,b,r;
cin >> l >> b >> r;
double size = area(l,b,r); // calling the function
cout << "The area of the triangle is " << size << endl;
}
return 0;
}
double area(double a) // for the square
{
return a*a;
}
double area(double a, double b) // for the rectangle
{
return a*b;
}
double area(double a, double b, double c) // for the triangle
{
double s = 0.5*(a+b+c);
double size = sqrt(s*(s-a)*(s-b)*(s-c));
return size;
}

Output ;

You want area of square(s) or rectangle(r) or triangle(t)?
s
Enter the length of side of the square
3
The area of the square is 9

You want area of square(s) or rectangle(r) or triangle(t)?
r
Enter the two sides of the rectangle
2
3.2
The area of the rectangle is 6.4

You want area of square(s) or rectangle(r) or triangle(t)?
t
Enter the three sides of the triangle
2.1
3.2
4.3
The area of the triangle is 3.21994

Add a comment
Know the answer?
Add Answer to:
/4 Question An area of square, drea ofa rectangle and triangle can be calculated by using...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT