Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++ help needed I have the structure of my program done so I thought but my outp

ID: 643579 • Letter: C

Question

c++ help needed

I have the structure of my program done so I thought but my output is not doing what I am wanting. Any help is appreciated.

Write a program which finds the 2 angles at which you can throw the ball to hit a target at a given distance. Prompt the user for the velocity and the distance to the target. Yjust need to get within 10 cm. (One angle will be below 45 degrees, the other above it.)

If the ball cannot reach the target before it hits the ground, print a message to that effect and prompt for a new velocity. Keep going until the user gives a velocity sufficient to throw it past the target.

Use a binary search to find the two angles. Find the distance at 45 degrees, which should be beyond the target. Then try at half that angle. If still beyond the target, you know the answer lies between 0 & 22.5 degrees, so divide that range by 2. (Obviously, if short of the target, try halfway between 22.5 and 45 degrees.) Keep subdividing until you hit the target. While testing/debugging, for each throw, report the angle, the distance thrown, and how much you missed the target by; when you get it all working, comment the debugging code out, but leave the commented code in your program with an explanatory comment tagging it as debugging code.

Do the same to find the angle between 45 and 90 degrees that will also hit the target

At the end, Report the two angles and the number of steps needed to hone in on the target.  

Explanation / Answer

#include <iostream>
#include <cmath>

using namespace std;

float absolute(float n)
{
    if(n<0)
        return -n;
    else
        return n;
}

int main()
{
    float d,v,th1,g=9.81,d1=0,l=0,u=45;
    float deg_to_rad = 3.1416/180;
    int steps=0;

    cout<<" Enter the distance : ";
    cin>>d;

    cout<<" Enter the velocity : ";
    cin>>v;

    if(v*v/g < d)
        cout<<" The ball cannot reach that distance !";
    else
    {
        th1 = (l+u)/2;
        d1 = v*v*sin(2*th1*deg_to_rad)/g;
        while(absolute(d1-d)>0.1)
        {
            if(d1>d)
                u = th1;
            else
                l = th1;

            th1 = (l+u)/2;
            d1 = v*v*sin(2*th1*deg_to_rad)/g;
            steps++;
        }

        cout<<" The two angles are "<<th1<<" and "<<90-th1<<" The steps required :"<<steps;
    }


}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote