gpt4 book ai didi

C++:浮力程序没有按计划输出

转载 作者:行者123 更新时间:2023-11-28 00:23:11 24 4
gpt4 key购买 nike

我试图制作一个程序,要求输入球体的半径和球体的重量。它使用这些输入来计算球体的浮力,然后程序确定它是否可以漂浮在水中。但是,无论我的输入是什么,我都会不断收到“6.95207e-308”

这些是编程作业的说明:

"Buoyancy is the ability of an object to float. Archimede's Principle states that the buoyant force is equal to the weight of the fluid that is displaced by the submerged object. The buoyant force can be computed by:

buoyant force = (object volume) times (specific gravity of the fluid)

If the buoyant force is greater than or equal to the weight of the object then it will float, otherwise it will sink.

Write a program that inputs the weight (in pounds) and radius (in feet) of a sphere and outputs whether the sphere will sink or float in water. Use 62.4 lb/cubic foot as the specific weight of water. The volume of a sphere is computed by (4/3)π times the radius cubed."

这是我的代码:

//Written by: Edward Santiago
//Assignment: HW_03_No14.cpp
//Class: CO SCI 243
//Date: October 17, 2014
//Description: Prime Numbers
#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;
double sphere_volume (double);
double buoy_force (double,double);
int main ()
{

double rad,volume,force,buoyancy;
double weight;
double water = 62.4;
cout << "~~Buoyancy calculator~~" << endl;
cout << "Please enter the radius of your sphere" << endl;
cin >> rad;
cout << "Please enter the weight of your sphere" << endl;
cin >> weight;
volume = sphere_volume(rad);
buoyancy = buoy_force(volume,weight);
cout << "The force of your sphere is "<<force<<endl;
if (force <= water)
cout << "Your sphere will float in the water"<<endl;
else
cout <<"Your sphere will sink :( "<<endl;
return 0;
}
double sphere_volume (double radius)
{
double vol;
vol = ((4/3) * (M_PI) * (pow(radius,3)));
return vol;
}
double buoy_force (double vol,double weight)
{
double force;
force = vol * weight;
return force;
}

最佳答案

你永远不会初始化力。

buoyancy = buoy_force(volume,weight);
cout << "The force of your sphere is "<<force<<endl;

将赋值更改为 force = buoy_force(volume, weight)

关于C++:浮力程序没有按计划输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26446968/

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