gpt4 book ai didi

c - 我将如何设置函数来计算圆的面积 - C

转载 作者:行者123 更新时间:2023-11-30 21:45:16 24 4
gpt4 key购买 nike

我有一个 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

最佳答案

从该函数的名称和签名可以明显看出,它应该计算面积和周长。而且很明显,您应该将两个 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;
}

关于c - 我将如何设置函数来计算圆的面积 - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44485706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com