用C计算球体的面积和体积

时间:2017-02-20 14:59:16

标签: c function

任务是使用函数找到球体的面积和体积,并提供一个额外的任务来提供一个函数来分别找到半径的力量(使用另一个函数),然后在区域和函数中调用它。体积。 我无法弄清楚为不同的力量调用半径函数的方法。 表面积公式为4 * PI * r(功率2) 卷的公式为4/3 *(PI)* r(幂3)

{{1}}

我还被指示“不要使用数组或其他解决这个问题的方法。”

我无法弄清楚为不同的力量调用半径函数的方法。解释或解决这个问题的正确方法会有所帮助!

2 个答案:

答案 0 :(得分:3)

radius实际上并不是函数的最佳名称,它计算3的平方或幂。我会调用函数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;
}

你这样使用它:

int radius;
// After assigning a value to radius you can call power() like this:
int square = power(radius, 2);
int cube = power(radius, 3);

答案 1 :(得分:-2)

int cube(int z){
    return z * z * z;
}

#include<math.h> // at the top
... 
int power(int base, int exponent){
    return pow(base,exponent);
}