2017-09-24 39 views
-4

我創建了這個程序,應該基本上使用公式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; 
} 
+0

「ISN」加起來「不是一個有意義的問題描述。 –

+0

什麼不加起來,你的主要回報是什麼? – doctorlove

+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; 
} 

您也可以從函數頭刪除總和,它沒有必要將它傳遞

+2

這可行,但你改變了什麼? – Habibi

+0

檢查函數** calculatevolume **現在在代碼中獲得3個參數而不是1個 –

+0

Ohhh,我沒有意識到你應該在參數中包含所有變量。我是編程新手,但非常感謝 – Habibi