0

The task was to find the Area and Volume of a sphere using functions and an additional task was provided to give a function to find the powers of the radius separately (using another function) and then call it in the functions of area and volume. I can't figure out the way to call the radius function for different powers. The formula for Surface area is 4*PI*r(power 2) The formula for the Volume is 4/3 *(PI)*r(power 3)

#include <stdio.h>
#include <math.h>
#include <conio.h>
#define PI 3.142

float surface(int x);
float volume(int y);
int radius();
int main()
{
    int r;
    float a, b;
    clrscr();
    printf("Enter the radius :");
    scanf("%d", &r);
    a=surface(r);
    b=volume(r);
    printf("Surface Area =%f", a);
    printf("\n");
    printf("Volume =%f", b);
    getch();
    return 0;
 }
int radius(int z)
{
    int f;
    f=z*z;
    return (f);
}
float surface(int x)
{
    float s;
    s = 4*PI*radius(x);
    return (s);
}
float volume(int y)
{
    float v;
    v = (4*PI*radius(y)/3);
    return (v);
}

I was also instructed to "Refrain from using Arrays or others methods of solving this question."

I cant figure out the way to call the radius function for different powers. An explanation or the correct way to solve this would be helpful!

Syed Hasan
  • 67
  • 2
  • 10
  • 1
    It's not really clear what the problem is here. Can you provide some examples of how the code isn't working as you would expect. – Sean Feb 20 '17 at 15:02
  • Your `radius` function should be called `square` (or `pow2`); you should invent another function called `cube` (or `pow3`). They should both take a single parameter, probably named `n`. The parameters to `surface` and `volume` should simply be called `r` (for "radius"). – Roger Lipscombe Feb 20 '17 at 15:02
  • Not an apparent issue , it just gives a value tha'ts not correct. So technically its wrong.@Sean – Syed Hasan Feb 20 '17 at 15:04
  • Yes that's what I am seeking @HighPerformanceMark. How do I get the function to figure out the power I seek because all I provide the function is the radius. – Syed Hasan Feb 20 '17 at 15:06
  • Sean meant to tell us the result for some example input, so for `5` input the expected area would be `25PI` and volume `500PI/3` but actual result was ... – niceman Feb 20 '17 at 15:06
  • 1
    Are you allowed to use build-ins? You could simply use `pow(radius,exponent)` [Look here](https://www.tutorialspoint.com/c_standard_library/c_function_pow.htm) – izlin Feb 20 '17 at 15:08
  • No, need to call a different function for the powers of the 'radius'. – Syed Hasan Feb 20 '17 at 15:11
  • @izlin: `pow` is not a built-in function. C does not have built-in functions. – too honest for this site Feb 20 '17 at 15:23
  • 1
    Possible duplicate of [The most efficient way to implement an integer based power function pow(int, int)](http://stackoverflow.com/questions/101439/the-most-efficient-way-to-implement-an-integer-based-power-function-powint-int) – James Snook Feb 20 '17 at 15:26
  • @izlin, `pow(3)` is not a good example of good coding style. It's implementation uses `log(3)` and `exp(3)` to compute the power and only squares and cubes are involved in the problem (which are better specified as `x*x` and `x*x*x`, not involving logarithm calculations, which involve series approximations) If the second parameter of `pow(3)`, some optimization is achieved not using it (of course, this optimization can be done inside the function, but even so, a cube involves only two multiplications, no tests, no calling overhead) – Luis Colorado Feb 21 '17 at 08:05

2 Answers2

3

radius is not really the best name for a function thet computes the square or power of 3. I would call the function power instead. And to make it do different powers you need the exponent as a parameter. You can then use a loop to calculate the power.

/* Calculate the power for exponents >= 0 */
int power(int radius, int exponent) {
    int i;
    int result = 1;

    for (i = 0; i < exponent; i++) {
        result = result * radius;
    }
    return result;
}

You use it like this:

int radius;
// After assigning a value to radius you can call power() like this:
int square = power(radius, 2);
int cube = power(radius, 3);
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • Thank you @KlasLindbäck. Can I call in multiple parameters like this in a function? – Syed Hasan Feb 20 '17 at 15:21
  • 1
    @SyedHasan Yes. You can add as many parameters as you need when you create your function, You need to match all parameters when you call the function. (For intermediate/advanced users there is something called variadic arguments, which is used for functions that take a variable number of arguments. See https://en.wikipedia.org/wiki/Variadic_function) – Klas Lindbäck Feb 20 '17 at 15:33
  • Thank you very much, solved my issue with another program! – Syed Hasan Feb 20 '17 at 15:41
  • @KlasLindbäck, have you ever heard about the quick power algorithm, applicable when the exponent is an integer? – Luis Colorado Feb 21 '17 at 08:08
  • @LuisColorado I have, and I am familiar with it. In this case I chose to tailor the complexity of the answer to the level of the OP (and to the current problem. The quick power algorithm isn't quicker for exponents as small as 2 or 3). – Klas Lindbäck Feb 21 '17 at 08:42
  • @KlasLindbäck, ok, you're right of course!!! :) – Luis Colorado Feb 22 '17 at 12:27
  • @niceman, no... as i goes from 0, the times it passes through the loop is exactly `exponent` times. – Luis Colorado Feb 22 '17 at 12:34
  • @KlasLindbäck, I don't understand why you claim about not calling `radius` the function, and then you call `radius` the formal parameter, that should be called `base`. Shouldn't it? – Luis Colorado Feb 22 '17 at 12:36
-2
int cube(int z){
    return z * z * z;
}

or

#include<math.h> // at the top
... 
int power(int base, int exponent){
    return pow(base,exponent);
}
user3853544
  • 581
  • 3
  • 9
  • this doesn't answer the question fully – niceman Feb 20 '17 at 15:04
  • @user3853544, Why not define a second function `power_bis` that calls `power` to finally call `pow`? or `power_tris` that calls `power_bis`, that calls `power` to finally call `pow` ??? – Luis Colorado Feb 22 '17 at 12:38
  • @Luis ColoradoThe way OP worded the question it sounded as if he was suposed to create his own function. Otherwise just call pow( ... ) on its own. – user3853544 Feb 22 '17 at 13:31