My code is supposed to return the following: "The angle and velocity to launch the snowball farthest is 45
degrees and 28 m/s which requires 98 joules and will launch the
snowball 80 meters."
My code returns the angle, but it returns 98 m/s, 0 joules, and 245 meters. My error lies somewhere in my velocity, but I don't know where it is.
#include <iostream>
#include <cmath> //allows you to use functions such as sin, cos, tan, etc.
using namespace std;
int main()
{
int bestAngle;
double bestDistance;
double bestVelocity;
double bestEnergyLimitation; // Declare variables (such as to track the best angle and distance)
// Calculate the distance for angles from 0-90
for (int newAngle = 0; newAngle < 90; ++newAngle )
{ double newVelocity;
float mass = 0.25;
double newDistance;
double kineticEnergy;
float pi = 3.14;
float g = 9.8;
float bestEnergyLimitation;
// Calculate the distance for velocities from 0-50
for (newVelocity = 0; newVelocity < 50; ++newVelocity)
{
// Calculate distance given angle and velocity
newDistance = (pow(newVelocity, 2) * sin(2 * ((newAngle*pi)/180))) / g;
// Calculate energy needed to launch
kineticEnergy = (1/2) * ((pow(newVelocity, 2) * mass));
// Determine if new (angle, velocity) gives a best distance
if ((newDistance > bestDistance) && (kineticEnergy < 100) )
{ bestAngle = newAngle;
bestDistance = newDistance;
bestVelocity = newVelocity;
bestEnergyLimitation = kineticEnergy;
}
}
}
// Display the best angle and velocity choice for the catapult
cout << "The angle and velocity to launch the snowball farthest is " << bestAngle <<
" degrees and " << bestVelocity << " m/s which requires " << bestEnergyLimitation << " joules and will launch the snowball " << bestDistance << " meters" << endl;
Try 1.0/2.0 instead of 1/2 - the latter leads to integer division and a value of 0.
double bestEnergyLimitation; // at top of main float bestEnergyLimitation; // within for loop
bestEnergyLimitation is declared twice - the one in the for loop gets updated, but its scope is limited to the loop. The value that's output at the end is the bestEnergyLimitation declared at the top of main, which hasn't changed. I'd try removing the second declaration within the for loop.
#include <iostream>
#include <cmath> //allows you to use functions such as sin, cos, tan, etc.
usingnamespace std;
int main()
{
int bestAngle;
double bestDistance;
double bestVelocity;
double bestEnergyLimitation; // Declare variables (such as to track the best angle and distance)
// Calculate the distance for angles from 0-90
for (int newAngle = 0; newAngle < 90; ++newAngle )
{
double newVelocity;
float mass = 0.25;
double newDistance;
double kineticEnergy;
float pi = 3.14;
float g = 9.8;
//float bestEnergyLimitation; <--removed this declaration
// Calculate the distance for velocities from 0-50
for (newVelocity = 0; newVelocity < 50; ++newVelocity)
{
// Calculate distance given angle and velocity
newDistance = (pow(newVelocity, 2) * sin(2 * ((newAngle*pi)/180))) / g;
// Calculate energy needed to launch
kineticEnergy = (1.0/2.0) * ((pow(newVelocity, 2) * mass));
// Determine if new (angle, velocity) gives a best distance
if ((newDistance > bestDistance) && (kineticEnergy < 100) )
{
bestAngle = newAngle;
bestDistance = newDistance;
bestVelocity = newVelocity;
bestEnergyLimitation = kineticEnergy;
}// end if
}// end for
}// end for
// Display the best angle and velocity choice for the catapult
cout << "The angle and velocity to launch the snowball farthest is " << bestAngle <<
" degrees and " << bestVelocity << " m/s which requires " << bestEnergyLimitation
<< " joules and will launch the snowball " << bestDistance << " meters" << endl;
return 0;
}
The angle and velocity to launch the snowball farthest is 45 degrees and 28 m/s which requires 98 joules and will launch the snowball 80 meters