Overloading functions
Jan 24, 2017 at 7:25am UTC
Write a program that will calculate the circumference of a figure. The program must use two overloaded functions, each named calcCircumference. The first function must calculate the circumference of a circle, and will have one parameter of type double that represents the radius of the circle. The second function will have two parameters of type double representing the length and width of a rectangle. Both functions must calculate the circumference and return the value as type double.
The main function should request the user to specify whether the circumference of a rectangle or a circle must be calculated, and depending on the answer, the user must either be prompted to enter the radius of the circle, or the length and the width of the rectangle. The main function should then call the correct overloaded function and display the circumference. Define a double constant variable PI with the value of 3.14285 to use for the calculation of the circumference of the circle..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include <iostream>
#include <cmath>
using namespace std;
const double PI = 3.14285;
double calcCircumference(double radius);
//Returns the area of a circle with specified radius .
double calcCircumference(double length,double width);
//Returns the area of a rectangle with specified length and width .
int main()
{
//formula_choice lets user decide which circle calculation he wants to perform.
double length = 0.0;
double width = 0.0;
double radius = 0.0;
double radius_of_circle,area_of_circle,area_of_rectangle;
cout << "Enter the radius of the circle" << endl << endl;
cin >> radius_of_circle ;
area_of_circle = calcCircumference( PI * pow(radius, 2));
area_of_rectangle = calcCircumference (length * width);
cout << "Radius = " << radius_of_circle << endl << endl;
cout << "Area_of_circle = " << area_of_circle << endl << endl;
cout << " Area_of_rectangle = " << area_of_rectangle << endl << endl;
return 0;
}
double calcCircumference(double radius)
{
return (PI* pow(radius, 2));
}
double calcCircumference(double length,double width)
{
return (length*2) +(width*2);
}
Put the code you need help with here.
Jan 24, 2017 at 7:37am UTC
You have both task description and some code. What is the problem?
* The code does not compile?
* The program crashes?
* Unexpected results?
Jan 24, 2017 at 7:57am UTC
Where on earth did you get that value of PI from?
Also, why are you calculating the AREA of a circle in a routine called calcCircumference?
Last edited on Jan 24, 2017 at 8:00am UTC
Jan 24, 2017 at 8:23am UTC
Just yet another troll topic :+(
Last edited on Jan 24, 2017 at 8:23am UTC
Jan 24, 2017 at 2:07pm UTC
Code is not working properly .Where did I go wrong .Please help .This my assignment
Jan 24, 2017 at 2:42pm UTC
There are numerous inconsistencies: the question asks for circumference, but the program tries to calculate area.
There is no user input for rectangle length and width. Two different variables are used for a single radius value.
These two lines use the wrong parameter in function call:
1 2
area_of_circle = calcCircumference( PI * pow(radius, 2));
area_of_rectangle = calcCircumference (length * width);
could be more like this (but take care - which radius variable is used):
1 2
double circumference_of_circle = calcCircumference( radius );
double perimeter_of_rectangle = calcCircumference(length , width);
Jan 24, 2017 at 2:50pm UTC
Because radius_of_circle is never actually passed to the program. You can do the rectangle yourself after this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <iostream>
#include <cmath>
using namespace std;
const double PI = 3.1415;
double area_of_circle(const double & radius) {return PI * pow(radius, 2);};
int main()
{
double radius_of_circle;
cout << "Enter the radius of the circle" << endl << endl;
cin >> radius_of_circle ;
cout << "Radius = " << radius_of_circle << endl << endl;
cout << "Area_of_circle = " << area_of_circle(radius_of_circle) << endl << endl;
return 0;
}
Last edited on Jan 24, 2017 at 2:51pm UTC
Topic archived. No new replies allowed.