在C ++中计算浮力时出现未知问题

时间:2014-02-12 06:17:22

标签: c++ calculator

我将切入追逐:我用C ++编写了一个程序,用于计算球形物体是否对于类而言是不是很强烈。然而,在我(我想到的)成功制作Visual Studio 2013中的程序之后,当我将它提交到我需要的地方时(Pearon可怕的myProgrammingLab)我得到了与Pearon相比错误的输出。 (IE:我说它漂浮,他们说它下沉,但不自己显示计算。)

这是我的代码:

// Bouyancy formula:
// Fb = V * y
// Where:
// Fb is the bouyant force
// V is the volume of the submerged object
// y is the specific weight of the fluid
// If Fb is greater than or equal to the weight of the object, then it will float, otherwise it will sink.

// Sphere volume formula:
// (4/3)pi(radius cubed)

#include "stdafx.h"
#include <iostream>
#define _USE_MATH_DEFINES // Used with math.h to provide access to M_PI (used in calculation of volume)
#include <math.h> // M_PI is the value of pie (3.14..)

using namespace std;

int main()
{
    float sphere_radius, sphere_weight; // Stores the value of the Sphere's radius and weight in feet and pounds respectively.
    double water_weight = 62.4; // Value set to 62.4lb /cubic feet, this value is the "y" value in the above formula.
    double bouyant_force, volume; // Defines Fb and V in the Bouyancy formula listed above.

    cout << "Enter the radius of the sphere, in feet: ";
    cin >> sphere_radius;
    cout << "\nEnter the weight of the sphere, in pounds: ";
    cin >> sphere_weight;
    cout << endl;

    volume = ((4.0 / 3.0) * M_PI * (pow(sphere_radius, 3))); // Calculates the volume of the sphere
    bouyant_force = (volume * water_weight);
    if (bouyant_force >= sphere_weight)
    {
        cout << "The sphere will float." << endl;
    }
    else if (bouyant_force < sphere_weight)
    {
        cout << "The sphere will sink." << endl;
    }
    else { cout << "Something went terribly, terribly, wrong.. Oh dear.."; }

    char x; 
    cin >> x; //Waits for user to press a key before closing the program.
    return 0;
}

任何人都可以帮助我理解为什么这不正确或为什么它没有被注册为正确?提前谢谢!

1 个答案:

答案 0 :(得分:1)

根据您的代码判断,错误似乎是您直接将您接受的重量与浮力进行比较。您应该将您接受的质量(磅是质量单位)乘以您正在使用的单位系统中的g。这似乎是因为你得到了它,而另一方计算它下沉。