球罐的体积无效的最终答案

时间:2017-09-24 22:24:15

标签: c++ c

我创建了这个程序,基本上应该使用公式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;
}

1 个答案:

答案 0 :(得分:0)

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

#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,没有必要传递它