Thread: need help with program

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    need help with program

    I am working on a program that computes the duration of a projectiles's flight and its height above ground when it reacdhes the target. I can get it to compile and run but the math is wrong. Can anyone please help?


    #include <stdio.h> /*printf, scanf definitions*/
    #include <math.h> /*cos definition*/

    #define G 32.17 /*gravitational constant*/

    int
    main(void)
    {
    double Theta; /*input-angle(radians)of elevation*/
    double Distance; /*input-distance (ft) to target */
    double Velocity; /*input-projectile velocity (ft/sec)*/

    double Time; /* output-time(sec) of flight*/
    double Height; /*output-height at impact*/

    /*opening explanation to user*/
    printf("This program computes the duration of a projectile's flight and its height above the ground when it reaches the target. To use this program, enter the angle, the distance, and the velocity after each of the prompt.");


    printf("Enter the angle of elevation in radians>");
    scanf( "%f", &Theta );
    printf("Enter the distance to target in feet>");
    scanf( "%f", &Distance );
    printf( "Enter the projectile velocity in ft/sec>");
    scanf( "%f", &Velocity );

    Time = Distance / (Velocity * cos(Theta)) ;
    Height = Velocity * sin(Theta) * Time -G*Time*Time/2.0;


    printf("The time of flight is %.3f seconds.\n", Time);
    printf("The height at impact is %.3f feets.\n", Height);


    system ("pause");
    return (0);

  2. #2
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    1. Is the actual algorithm for this available?
    2. Do you have sample (known correct) inputs and outputs?

    Both would help with debugging....
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    2
    Yes the Relevant formula is

    time = distance
    ----------------------------------
    velocity x cos(theta)



    height= velocity x sin(theta) x time - g x time 2
    ------------------------
    2

    G 32.17

    sample input is 0.3 for angle
    11000 for distance
    800 for velocity

    sample output is 14.393seconds
    70.632feet


    I keep getting a negative number. (in the height equation that is time squared)
    Last edited by ddj1226; 02-09-2010 at 09:54 PM.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You might want to error check your input values (such as if the input fails).

    I would recommend fgets() and strtod() so you can check for overflows. At least for the time being print out each value as you read it. You could also comment out the scanf()'s and just declare the values as per your examples.

    And surely
    Code:
    #define GRAVITY ...
    Isn't so long that it had to be shortened to "G"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM