Thread: c programming - issue with projectile motion ( calculating hieght and range )

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    1

    c programming - issue with projectile motion ( calculating hieght and range )

    well I am not familliar with c programming so it will be so great if anyone could help me
    actually I've been asked to write a program which include :

    2- In kinetics, we often study projetiles. You are asked to write a program that:
    1- prompts the user to enter the magnitude of the initial velocity and the angle it makes with respect to the horizontal axis in DEGREEs.
    2- Calculates and displays the range of the projectile, the maximum height it reaches. 3- displays the function of the path y(x).
    4- calculates the height at any given point 0<x<range;

    We assume that:
    -There is no acceleration along the horizontal axis.
    -The acceleration along the y axis is the gravity ( g=9.81m/sē)
    -The projectile is considered to be a point object and at t=0 x=0 y=0.





    and then I've written this and the pb was when I put v0 and theta the range and the hight is 00
    Code:
     
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    float v0,x,y,os,h;
    float theta;
    float g = 9.81;
    int main()
    {
        printf("Enter the magnitude of v0:\n");
        scanf("%f",&v0);
        printf("Enter theta in degree:\n");
        scanf("%d",&theta);
        os = (v0*v0)*sin(2*(theta))/9.81;
        printf("%f\n",os);
        h = sqrt(v0*sin(theta))/(2*9.81);
        printf("%f\n",h);
        printf("y=(tan(theta)*(x))-((0.5*g*x)/(v0*cos(theta)))\n");
        getch();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Global variables are bad. You should move all of those variable declarations inside main. Also, scanf("%d",&theta); is wrong. What is the type of theta? What is the correct format specifier for a float?

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The trigonometric functions in C take radians as input. Your degree value therefore needs to be first converted to radians (multiply by PI and divide by 180 I believe) before being used as input to those functions.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    remove
    Code:
    <conio.h>
    as it's a non standard header. As well remove your call to
    Code:
    getche()
    and simply call
    Code:
    getchar();
    you may need a few more calls to getchar() - i haven't ran your code - or since you're on windows some IDE will set it so once your code is finished, it will wait for input before terminating the program.
    Last edited by caroundw5h; 12-26-2012 at 06:42 PM.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Runge Kutta Projectile Problem
    By meadmead in forum C Programming
    Replies: 2
    Last Post: 11-25-2011, 02:25 PM
  2. Projectile C++ and OpenGL
    By coryj525 in forum Game Programming
    Replies: 6
    Last Post: 02-11-2011, 03:58 PM
  3. Projectile launching code
    By thintheherd in forum C Programming
    Replies: 27
    Last Post: 04-23-2010, 01:01 AM
  4. Calculating the Range
    By volk in forum C Programming
    Replies: 4
    Last Post: 03-19-2003, 08:32 AM
  5. projectile trajectories
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 02-07-2002, 09:36 PM