-4

I'm working on below program and I want the program to do the same thing, but with not one main() function, but instead one main() function PLUS one user defined function called computeConeVolume that contains the calculation. In other words I want to remove the one line calculation and replace it with a function call, then write and add the function below main with the calculation, surrounded any other syntax that I need to complete it.

The function should contain local variables and a constant declared and must have the calculation, it may not do anything else such as input or output. Should be able to declare "global" variables anywhere but no variables above or outside of main() and the function are allowed. A value-returning function should be used because it's a little simpler to understand, but you can employ a void function. Need to have a function prototype at the top of the code, then main, then your function.

Need some help with this since I'm new to C++ and trying to learn.

//Cone Volume Calculator Program

#include <iostream>
using namespace std;

int main( )
{
    //Declare variables and constants
    double coneRadius = 0.0;
    double coneHeight = 0.0;
    const double PI = 3.1415;
    double coneVolume = 0.0;

    //Prompt the user for inputs
    cout << "Enter the radius of the cone: ";
    cin >> coneRadius;
    cout << "Enter the height of the cone: ";
    cin >> coneHeight;

    //Do the calculation
    coneVolume = 0.3333 * PI * coneRadius * coneRadius * coneHeight;

    //Display the result
    cout << "The volume of your cone is: " << coneVolume << endl;

    system("pause");
    return 0;
} //end of main
michael m
  • 81
  • 2
  • 7
  • 3
    You have the exact requirements for the function, so you should be able to make a first attempt and show us here to improve on. If you don't know where to start, you can search for function tutorials online. – Neil Kirk Nov 07 '14 at 02:00

2 Answers2

1

I'm trying to recycle some of Amadeus' answer and use some of your code.

First of all, you should define the function you wish to calculate the cone volume with. Something like:

double coneVolume(double, double);

You should pay attention to always leave the main function at the end of your .c document.

What you also need is a declaration of your function. This is where you actually write down what the function does:

double coneVolume(double coneRadius = 0.0, double coneHeight = 0.0) {
   double coneVolume = coneVolume = 0.3333 * PI * coneRadius * coneRadius * coneHeight;
   return coneVolume;
}

The value setting in the method head is just a thing for default values, this isn't really needed here, just to show you.

Where is the const double PI = 3.1415; going? Somewhere above your functions, then it's visible everywhere in your document. You could also think about using math.h by include, then you can use M_PI, which is about the same thing as your PI constant. (To be more precise it is a definition which replaces any time you write M_PI by the actual Pi value)

If you really want the function to just calculate without input, you can just define them locally, just like in your main.

Note: Global scope is always out of the main method.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Cadoiz
  • 1,446
  • 21
  • 31
  • Extension: Could've easily done return (math operation using 2 vars) inside your coneVolume() method, instead of actual declaration and return. All preference. – user2277872 Jul 19 '16 at 04:20
  • Of course, that's right. I refer to his task: "The function should contain local variables [...] A value-returning function should be used because it's a little simpler to understand, but you can employ a void function." – Cadoiz Jul 20 '16 at 01:46
  • Ah, I see. Great answer, just extending it to provide clarity. – user2277872 Jul 20 '16 at 02:42
-2

How about this program

#include <cmath>
#include <iostream>
using namespace std;
double coneVolume(double, double);
int main( )
{
    //Declare variables and constants
    double coneRadius = 0.0;
    double coneHeight = 0.0;

    //Prompt the user for inputs
    cout << "Enter the radius of the cone: ";
    cin >> coneRadius;
    cout << "Enter the height of the cone: ";
    cin >> coneHeight;

    //Do the calculation

    //Display the result
    cout << "The volume of your cone is: " << coneVolume(coneRadius, coneHeight) << endl;

    system("pause");
    return 0;
} //end of main

double coneVolume(double coneRadius, double coneHeight)
{
   double PI = acos(-1.0);
   double volume = coneRadius * coneRadius * coneHeight * PI / 3.0;
   return volume;
}

Please note that I added the cmath library in order to use the acos function.

double PI = acos(-1.0);

I read this trick in a piece of code that a guy used to do trigonometry using C++. I did not use any kind of parenthesis when I calculated the volume because both * and / are in the same order of precedence and are evaluated from left to right. I divided by 3.0 because I am using doubles. For the prototype of the function that is right above the main function

double coneVolume(double, double);

I just wrote the type of arguments of the function as only the type of the arguments of the function is needed for propotypes.

Amadeus Sanchez
  • 2,375
  • 2
  • 25
  • 31
  • 3
    This answer would be useful if you could explain the original problems, and explain why you did it the way you did it. Nobody learns anything when you do their work for them. – Jason C Nov 07 '14 at 04:07
  • [`M_PI`](http://stackoverflow.com/questions/1727881/how-to-use-the-pi-constant-in-c) – Jason C Nov 07 '14 at 04:46