gpt4 book ai didi

c++ - 球 jar 容积的最终答案无效

转载 作者:行者123 更新时间:2023-11-30 21:27:59 26 4
gpt4 key购买 nike

我创建的这个程序应该基本上使用公式 V=Pi*h^2(3r-h)/3 但我的最终答案并没有相加。

例如:如果我用 1 代替半径,用 2 代替高度,我应该得到 4.18,但通过程序我得到 -1。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double calculatevolume(double);

double main() {
double radius, height;
double sum; //for storing the radius of the reservoir given by the user
//for storing the calculated volume of the water.

printf("Please enter the Radius in meters first and then the Height in meters right after:\n"); //Displays the message on the screen

scanf("%lf",&radius);
printf("Value of r: %f\n", radius);

scanf("%lf",&height);
printf("Value of h: %f\n", height);

sum = calculatevolume(sum);

printf("For a reservoir of radius: %.2f m\nAnd a water depth: %.2f m\nThe water volume is: %.2f m^3\n",radius,height,sum);

system("PAUSE");
}

double calculatevolume(double sum) {
double radius;
double height;

sum = ((((3 * radius) - height)/3) * 3.14 * height * height);

return sum;
}

最佳答案

尝试这段代码,您应该将高度和半径传递给函数:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double calculatevolume(double,double,double);
double main() {
double radius, height;
double sum; //for storing the radius of the reservoir given by the user
//for storing the calculated volume of the water.

printf("Please enter the Radius in meters first and then the Height in meters right after:\n"); //Displays the message on the screen

scanf("%lf", &radius);
printf("Value of r: %f\n", radius);


scanf("%lf", &height);
printf("Value of h: %f\n", , height);

sum = calculatevolume(sum,radius,height);

printf("For a reservoir of radius: %.2f m\nAnd a water depth: %.2f m\nThe water volume is: %.2f m^3\n",radius,height,sum);

system("PAUSE");
}

double calculatevolume(double sum, double radius, double height) {
sum = ((((3 * radius) - height)/3) * 3.14 * height * height);
return sum;
}

你也可以从函数头中删除sum,没有必要传递它

关于c++ - 球 jar 容积的最终答案无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46395440/

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