January 11th, 2014, 11:46 AM

Help With C  Trying to create 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
int g = 9.81; //gravity
time[0] = 0; //time starts at 0
theta[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[i1]+dt;
omega = omega(g/l)*dt*sin(theta[i]);
theta[i] = theta[i1]+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!
January 11th, 2014, 01:51 PM

January 12th, 2014, 03:13 AM

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
scanf("%f",theta);
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.
Code:
$ 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...
Inrange integer computations are exact. Floating point calculations are inexact. For example in python:
>>> sum(0.01 for i in range(180*100))
179.99999999998127
or in j (www.jsoftware.com)
0j14":+/(180*100)#0.01
179.99999999998127
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!