Creating a pendulum with gravity

This is a discussion on Creating a pendulum with gravity within the C Programming forums, part of the General Programming Boards category; Hey, First thing to note is I am new, if I am in the wrong thread or I am clashing ...

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    9

    Creating a pendulum with gravity

    Hey,
    First thing to note is I am new, if I am in the wrong thread or I am clashing with any other forum regulations I would ask to be kindly redirected.

    My query is probably simple, I have recently begun programming c within my university course. I have been given a task to add arrays to a program I had previously made to make it record the values as I expect I will then quote them on an exported document.


    Code:
    #include <stdio.h>
    #include <math.h>
    main()
    {
        int i;                                  // step counter
        float theta[10000];                     // initial value for angle
        float omega;                            // initial value for angular speed
        float time[10000];                       // array of possible time values
        float dt = 0.01;                          // time step
        int steps = 100;                         // number of steps
        float l = 4.04;                          //length of string
        float g = 9.81;                             //gravity
        time[0] = 0;                             //time starts at 0
    
                                                    //values theta cant go past
    
    
    
    
    
    
        printf("What would you like the initial speed to be?\n");
        scanf("%f",&omega);
        printf("What angle will you drop it from?\n");
        scanf("%f",theta[0]);
        for(i=0;  theta[i]=0; i++){
    
    
    
    
            time[i] = time[i-1]+dt;
            omega = omega-(g/l)*dt*sin(theta[i]);
            theta[i] = theta[i-1]+omega*dt;
            printf("Step number %i, Time equals %f, Theta = %f omega = %f\n",i,time[i],theta[i],omega);
    
    
        }
    
    
        if(theta[i] == 180 || theta[i]== -180){
            printf("\a");
        }
    }

    This is what I have written so far, you can probably see what I am expecting. The pendulum is meant to swing and take gravity into account, then it is meant to loop and record values for the step count, time, theta and omega (mainly theta and omega required). The boundaries are set so once the angle of theta reaches 180 degrees it then stops. I believe the problem lies in the way I have created my theta array, however, I don't properly know how to implement this. Upon launch it asks for omega, as required, after inputting the value it crashes.


    It is probably an extremely simple matter but I have tried this in the space of two days of learning code so please bear with me, a brief explanation would be greatly appreciated so I understand why the changes have been made!


    Thank you!
    Last edited by RyRy; 01-11-2014 at 11:15 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Well a decent compiler with the warning level turned up will cut out a lot of the dumb mistakes.
    Code:
    $ gcc -c -Wall bar.c
    bar.c:3:1: warning: return type defaults to ‘int’ [-Wreturn-type]
    bar.c: In function ‘main’:
    bar.c:25:5: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat]
    bar.c:26:5: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    bar.c:10:9: warning: unused variable ‘steps’ [-Wunused-variable]
    bar.c:43:1: warning: control reaches end of non-void function [-Wreturn-type]
    Look at your scanf and for loop in particular.

    Secondly, ALWAYS read the manual for functions the first time (or nearly every time) you use them.
    For example, you would learn that sin() etc take radians, not degrees, as parameters.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gravity allegro
    By nukecake in forum Game Programming
    Replies: 4
    Last Post: 09-02-2011, 12:14 PM
  2. SDL gravity.
    By bijan311 in forum Tech Board
    Replies: 4
    Last Post: 06-02-2010, 08:08 PM
  3. Have I Destroyed Gravity?
    By bumfluff in forum Game Programming
    Replies: 25
    Last Post: 12-28-2006, 03:58 PM
  4. Need help with gravity engine
    By madmech in forum Game Programming
    Replies: 11
    Last Post: 07-03-2005, 02:41 PM
  5. Gravity? We don't need no steenking gravity!
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 03-28-2002, 06:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21