Catapult Problem

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;

return 0;
}
Last edited on
kineticEnergy = (1/2) * ((pow(newVelocity, 2) * mass));

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.
I fixed the first error, thanks.
For the second error, if I remove the second declaration, it returns all values as zero.
I commented out the float bestEnergyLimitation variable declaration in the loop and got the expected results. Is this what you have?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    #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; <--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
Somehow I missed commenting it out. It works now - thank you!
Topic archived. No new replies allowed.