I attempted to make a program that asks for the input for the radius of a sphere, and the weight of the sphere. it uses these inputs to calculate the buoyancy of the sphere and the program determines whether or not it can float in water. However, I keep getting "6.95207e-308", no matter what my inputs are
These are the instructions for the programming assignment:
"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."
This is my code:
//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;
}
You never initialize force.
buoyancy = buoy_force(volume,weight);
cout << "The force of your sphere is "<<force<<endl;
Change the assignment to force = buoy_force(volume, weight).
You assigned the big answer to "buoyancy" and then printed out "force".
Add
force=buoyancy;
just before printing force. This is because force is uninitialized and you are trying to print it.
Related
Heres the given:
Write a program to compute for the surface area and volume of a sphere if the unit of the radius
is in centimeters (cm).
Filename: exer10.cpp
Formulas: area = 4*pi*radius2
Volume = (4/3)*pi*radius3
I was skeptical because of the "if" on the given as you read it, now I don't know if what I did is right. But I have some ideas on how to do it
I Have a 2 ideas in mind 1st is where if I input a value there will be a formula to distinguish if that value is in centimeter, the thing is I don't know how.
2nd idea is I will use the if else method where after I input a value, it will ask if its in centimeter or not, if I type "Y" it will do its thing and continue its computation but if I type "N" it`ll will not compute and end the program.
Any suggestion guys?
By the way here's my code (My given ideas are not written here)
#include <iostream>
using namespace std;
int main()
{
float radius, area, volume;
cout << "This program computes for the surface area and volume of a sphere (centimeters)"
"\n\n";
cout << "Input the radius of a sphere : ";
cin >> radius;
area = (4 * 3.1416 * radius * radius);
volume = (4 / 3) * (3.1416 * radius * radius * radius);
cout << "The surface area of a sphere is : " << area << "\n";
cout << "The volume of a sphere is : " << volume;
return 0;
}
You can ask the user to input the unit. Consider this example, if it's in centimeters then perform the operation/task or else exit the program.
#include <iostream>
#include <string>
#include <cstdio>
int main() {
int radius;
std::string unit;
std::cout << "Enter radius of sphere in centimeters (e.g. 5 cm) : ";
std::cin >> radius >> unit;
if (unit != "cm") {
std::cout << "\nPlease enter in centimeters (e.g. 5 cm)";
exit(1);
}
// perform operation
return 0;
}
I', trying to make a simple BMI calculator using C++. When I input my personal height and weight, I get the correct result but I can't round the number to the nearest whole number. I looked up some videos and some articles and many of them suggested using the "round()" function. I tried that and the result I got was 0!
All feedback helps. Thanks!
#include <iostream>
#include <cmath>
using namespace std;
float Calculate(float kilo, float centimeter)
{
float meter = centimeter * 100;
return kilo / (meter * meter);
}
int main()
{
float kilo, centimeter;
float bmi;
cout << "BMI calculator." << endl;
cout << "Please enter your weight in kilograms. ";
cin >> kilo;
cout <<"Please enter your height in centimeters. ";
cin >> centimeter;
bmi = Calculate(kilo, centimeter);
cout << round(bmi) << endl;
return 0;
}
Your formula for calculating BMI is wrong just change the line float meter = centimeter/100;.
because according to your formula any number multiplied by 100 and then squared becomes so big that you get very small floating point number after the division with weight that is eventually rounded that's why you always get 0 in output.
c++ and I'm trying to figure out why my code returns 0's from a few statements after the user inputs some float numbers. I'm not sure why. Maybe someone can help:
This is what I get after running my method and answering the questions before it:
The number of gallons of paint required is: 0 gallons
Hours of labor that is required: 0 hours
.
Also ignore the () around my # in the beginning. I will put periods between lines to make it look neater on this website.
/**
* A painting company has determined that for every 160 square feet of wall
space, one gallon of paint and 3 hours of labor are required.
* The company charges the $28.00 per hour for labor.
* Design a modular program that allows the user to enter the number of rooms
that are to be painted,
* the approximate square feet of wall space in each room (may differ from room
to room), and the price per gallon of paint.
* It should then create a report that includes a fancy company header and
displays the following information:
* The number of gallons of paint required: (Rounded up to the next full
gallon)
* The hours of labor required:
* The cost of the paint:
* The labor charges:
* Total cost of the paint job:
* Requirements:
* Input validation: The program should not accept a value less than 1 or
more than 12 for the number of rooms
* Should not accept a value less than 100 for the square
footage of a room.
* Should not accept a value less than $10.00 or more
than $25.00 for the price of a gallon of paint
*
* Lets do this...
*/
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
float priceOfGallon(float);
float numberOfGallons(float, float);
float totalWallArea(float, float, float);
float laborHours(float, float);
void fancyCompanyHeader();
int main() {
float area;
float totalArea;
float min_labor = 3;
float number_of_rooms;
float number_of_gallons;
float price_of_gallon;
totalWallArea(area, totalArea, number_of_rooms);
priceOfGallon(price_of_gallon);
numberOfGallons(number_of_gallons, totalArea);
laborHours(number_of_gallons, min_labor);
fancyCompanyHeader();
return 0;
}
// function that gets the number of gallons needed for the total area
float numberOfGallons(float number_of_gallons, float totalArea) {
number_of_gallons = (totalArea / 160.0);
std::cout << "The number of gallons of paint required is: " <<
number_of_gallons << " gallons" << std::endl;
}
float priceOfGallon(float price_of_gallon){
std::cout << "Please enter the price per gallon: " << std::endl;
cin >> price_of_gallon;
while(price_of_gallon < 10.00 || price_of_gallon > 25.00) {
std::cout << "The price should be between $10.00 and $25.00. Please try again: " << std::endl;
cin >> price_of_gallon;
}
}
float totalWallArea(float area, float totalArea, float number_of_rooms) {
std::cout << "Please enter the number of rooms that needs to be painted:" <<
std::endl;
std::cin >> number_of_rooms;
while(number_of_rooms < 1)
{
cout << "Number of rooms must be at least one. Please try again: " <<
std::endl;
cin >> number_of_rooms;
}
for(float i = 1; i <= number_of_rooms; i++)
{
cout << "Please enter the square feet of wall space needed for Room " <<
i << std::endl;
cin >> area;
while(area < 100)
{
std::cout << "The area should be 100 or greater. Please try again: "
<< std::endl;
cin >> area;
}
totalArea += area;
}
}
// I will finish this method later
float laborHours(float number_of_gallons, float min_labor) {
min_labor = number_of_gallons * 28.00;
std::cout << "Hours of labor that is required: " << min_labor << " hours "
<< std::endl;
return min_labor;
}
You need to make all of those variables you are modifying global (Declared outside of int main()). In C++, when you give a function a variable, it will just copy the contents of the variable into the function's variables: the original variable passed in remains constant. Thus, your uninitialized floats default to 0 and are not changed by any of the functions, so when they are given to the laborHours function or numberOfHours function, 0s are passed into each.
Example with much better practices than in your code (it's ok, everyone starts by writing atrocious code) :
#include <iostream>
int walls,rooms,total_walls; //It only makes sense if all of these are integers.
//These are global declarations, they can be modified by any function in the program
void input_walls() {
/* Functions are notated as so in C++:
{return type} {function_name}({input},{input},...)
It's pointless to annotate functions that don't return anything
with a declaration of a float return type (they'll just return 0 */
std::cin >> walls;
while(walls < 0) {
std::cout << "Please input a positive amount of walls per room.";
std::cin >> walls;
}
}
void input_rooms() {
std::cin >> rooms;
while(rooms < 0) {
std::cout << "Please input a positive amount of rooms";
std::cin >> rooms;
}
}
void calculate_result() {
total_walls = walls*rooms;
}
void output_result() {
std::cout << "I'll need to paint " << total_walls << " walls!" << std::endl;
}
int main() {
input_walls();
input_rooms();
calculate_result();
output_result();
}
This still isn't the best way to write this, but it's still the exact same thing you were trying to do. Now try rewriting what you were trying to do in this style!
TLDR/Quick fix: Make the variable definitions global, cut out the arguments from the functions.
I'm extremely new with programming, and I mostly pieced this together straight out of textbooks, but I cannot get the damn thing to work. For some reason, my doubles (F1 for example) say that they haven't been declared. Here's the code.
// Vector Resultant Calculator by Jeffrey Weissman
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
double F1 = 0, double F2 = 0, double F3 = 0, double F4 = 0; // The initial forces
double A1 = 0, double A2 = 0, double A3 = 0, double A4 = 0; // The initial angles
double F1x = 0, double F2x = 0, double F3x = 0, double F4x = 0; // The calculated horizontal components
double F1y = 0, double F2y = 0, double F3y = 0, double F4y = 0; // The calculated vertical components
double Rx = 0, double Ry = 0, double R = 0, double RA = 0; // The calculated attributes of the Resultant Vector R
string unit; // The unit of the force magnitudes
std::cout << "Welcome to Jeffrey Weissman's Vector Resultant Calculator./n To begin, please enter the magnitude of the first vector. For now, please ignore all units.";
std::cin >> F1;
std::cout << "Thank you. Please enter the angle, in degrees, of the vector. Please count the angle counterclockwise from the x axis.";
std::cin >> A1;
std::cout << "Thank you. Please enter the magnitude of the second vector.";
std::cin >> F2;
std::cout << "Thank you. Please enter the angle, in degrees, of the vector. Please count the angle counterclockwise from the x axis.";
std::cin >> A2;
std::cout << "Thank you. Please enter the magnitude of the third vector. If there is not a third vector, please enter 0";
std::cin >> F3;
std::cout << "Thank you. Please enter the angle, in degrees, of the vector. Please count the angle counterclockwise from the x axis. If there is not a third vector, please enter 0.";
std::cin >> A3;
std::cout << "Thank you. Please enter the magnitude of the fourth vector. If there is not a fourth vector, please enter 0";
std::cin >> F4;
std::cout << "Thank you. Please enter the angle, in degrees, of the vector. Please count the angle counterclockwise from the x axis. If there is not a fourth angle, please enter 0.";
std::cin >> A4;
std::cout << "Thank you. Now, please enter the unit of the force magnitudes.";
std::cin >> unit;
F1 * cos(A1) = F1x, F2 * cos(A2) = F2x, F3 * cos(A3) = F3x, F4 * cos(A4) = F4x;
F1 * sin(A1) = F1y, F2 * sin(A2) = F2y, F3 * sin(A3) = F3y, F4 * sin(A4) = F4y;
F1x + F2x + F3x + F4x = Rx;
F1y + F2y + F3y + F4y = Ry;
tan(Ry / Rx) = RA;
Rx * Rx + Ry * Ry = R ^ 2;
std::cout << "The Vector Resultant has magnitude " << R << " at an angle " << RA << " degrees from the x axis.";
}
Here's a working solution. I am writing this in sympathy for students with an unreasonable professor, but this is not what we do on this site.
#include <iostream>
#include <math.h>
using std::cout;
using std::endl;
using std::cin;
int main()
{
cout << "Welcome to Jeffrey Weissman's Vector Resultant Calculator" << endl;
cout << "Please enter the magnitude (without units) and then the angle (in degrees, measured counterclockwise from the x axis) of each vector. Omit all units. When you have entered all vectors, enter 0." << endl;
double mag=1.0, angle;
double Sx=0.0, Sy = 0.0;
cin >> mag;
while(mag > 0.001)
{
cin >> angle;
angle *= 3.14159/180.0;
double x = cos(angle);
double y = sin(angle);
Sx += x;
Sy += y;
cin >> mag;
}
mag = sqrt(Sx*Sx + Sy*Sy);
angle = atan2(Sy, Sx) * 180.0/3.14159;
cout << "The Vector Resultant has magnitude " << mag << " at an angle " << angle << " degrees from the x axis." << endl;
return 0;
}
Try something simpler (another approach).
If your existing program has error's, a self taught programming co-worker once taught me to
Divide and conquer.
Simply disable the compile of those parts of the program with problems using #if(0) ... #endif.
He often simply split the code in half ... just to determine if his coding problem was in the 1st or second half.
Expand the disabled code until you get a clean compile.
Now you have isolated problems to work on. (And perhaps generate a MCVE for creating a question for SO.)
Narrow the search
Your first efforts are to find where the errors begin ... not just in the sense of line numbers, but also in the sense of program logic.
Then get to work on the earliest error 1st.
I'll cut to the chase: I made a program in C++ that calculates if a spherical object is bouyant or not for a class. However, after I (from what I thought) successfully made the program in Visual Studio 2013 when I submitted it where I need to (Pearon's terrible myProgrammingLab) I get the wrong output compared to Pearon's. (IE: Mine says it floats, they say it sinks, but don't show the calculations themselves.)
Here is my code:
// 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;
}
Can anyone please help me understand why this isn't correct or why it isn't being registered as correct? Thanks in advance!
Judging by your code, the error seems to be you directly comparing the weight you accept against the buoyant force. You should be multiplying the mass you accept(the pound is a unit of mass) by g in the unit system you are using. That seems to account for you getting that it flaots while the other side calculates that it sinks.