如何设置函数来计算圆的面积 - C.

时间:2017-06-11 15:45:36

标签: c function area

我有这个C程序,需要计算用户输入的圆的面积。 我必须在主函数之前使用它:

void area_circum(double radius, double *area, double *circum);

我无法让程序运行起来。我正在使用main函数和另一个名为area_circum();

的函数

这就是我现在所拥有的:

#include <stdio.h>


void area_circum(double radius, double *area, double *circum);

int main(void) {

    double radius, area, circum;

    printf("Please enter the radius of the circle: ");
    scanf("%f", &radius);

    area_circum(radius, area, circum);

    printf("Area of circle : %0.4f\n", area);

    return 0;
}


void area_circum(double radius, double *area, double *circum) {
    double PIE = 3.141;

    double areaC = 0;

    areaC = PIE * radius * radius;
}

当我构建并运行它时,它可以工作,我输入一个数字,但它返回0.00

3 个答案:

答案 0 :(得分:1)

从该函数的名称和签名中可以明显看出,它应该计算面积和周长。另外很明显,你应该传递两个int变量的地址(或两个指向int的指针),然后给出计算结果:

#include <stdio.h>

void area_circum(double radius, double *area, double *circum);

int main(void)
{
    double radius, area, circum;

    printf("Please enter the radius of the circle: ");
    scanf("%lf", &radius); // Note: You should add a check to see if
                           //       scanf() failed.
    area_circum(radius, &area, &circum);  // &area is the address of area
                                          // &circum is the address of circum
    printf("Area of circle : %0.4f\n", area);
    printf("Circumference of circle : %0.4f\n", circum);
    return 0;
}

void area_circum(double radius, double *area, double *circum)
{
    double PIE = 3.1416;
    *area = PIE * radius * radius;
    *circum = PIE * radius * 2;
}

答案 1 :(得分:0)

你快到了。您需要做的就是返回计算的值:

void area_circum(double radius, double *area) {
    double PIE = 3.141;
    *area = PIE * radius * radius;
}

并打电话:

    area_circum(radius, &area);

答案 2 :(得分:-4)

#include <stdio.h>

void area_circum(double radius);

int main() {

   double radius;

   printf("Please enter the radius of the circle: ");

   scanf("%lf", &radius);

   area_circum(radius);

   return 0;

}


void area_circum(double radius) {

   double PIE = 3.141;

   double areaC = 0;

   areaC = PIE * radius * radius;

   printf("Area of circle : %0.4f\n", areaC);

}