# Thread: Creating a pendulum with gravity

1. ## 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;                     // initial value for angle
float omega;                            // initial value for angular speed
float time;                       // 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;                             //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);
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! 2. 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. Popular pages Recent additions 