1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Rep Power

    Help With C - Trying to create a pendulum with Gravity

    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.

    #include <stdio.h>
    #include <math.h>
        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
        int 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");
        printf("What angle will you drop it from?\n");
        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){
    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!
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Rep Power
    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
  4. #3
  5. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power

    2 of the problems

    a) scanf("%f",theta[0]);
    b) for(i=0; theta[i]=0; i++){

    a) scanf needs a location to store the result. You passed a double instead of an address. (double because of promotion) Instead, choose one of
    scanf("%f",&theta[0]);//which is the same as
    scanf("%f",theta+0); // which is the same as

    b) == is an equality operator. = is an assignment operator. Write equality tests "backward". Since 0=theta[i] is invalid the compiler will trap "typos".

    Even so, gcc warns of this sort of problem.
    $ gcc -Wall c.c
    c.c: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
       for(i=0; theta[i]=0; i++) {

    Do you have a reason for storing all the results in an array? You use only one prior value and display the result.

    Your program looks like it depends on exact arithmetic.
    if(theta[i] == 180...
    In-range integer computations are exact. Floating point calculations are inexact. For example in python:
    >>> sum(0.01 for i in range(180*100))
    or in j (www.jsoftware.com)
    The j "customize" conjunction sets a comparison tolerance for equality tests. If you need an equality test then you should test that the absolute value of a difference is sufficiently small.
    However, you probably don't want an equality test. Use less than and greater than.
    Last edited by b49P23TIvg; January 12th, 2014 at 03:38 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo