Hi, I'm a beginner with C, and as part of a piece of University Coursework (I'm on a physics course), and there's a part where I have to test if there are the right number of data entries and if the data entries are formatted correctly, and I'm unsure of how to test this. The coursework is to make a Pendulum and this particular exercise was to modify the pendulum program so that the time and theta values are saved to a file when the program has finished running. My program is included in an attachment. Thanks
Code:
#include <stdio.h>
int main(void)
{
int i; //this is the step counter
float theta[10000];
theta[0]=0; // this is the initial value for the angle
float omega=0.1; // this is the initial value for the angular speed.
float time[10000]; // this is the initial time.
float dt=0.01; //this is the time step. This is how much the time increases every step.
float length=2.39; //this is the length of the pendulum
float g=9.81; //this is the value for gravitational acceleration.
int steps=10000; //number of steps. The number of steps is 100.
time[0]=0;
//while(theta<6.28 && theta>-6.28)
// {
for(i=1 ;i<steps; i=i+1)
{
time[i] = time[i-1]+dt;
printf("Step Number is %i and Time equals %f\n",i,time[i]);
omega=omega-(g/length)*dt*sin(theta[i-1]);
theta[i]= theta[i-1]+omega*dt; // calculation of the angle, theta
printf("Time equals %f, ",time);
printf("angular speed is %f, ", omega);
printf("and theta equals %f \n",theta);
if (theta[i]>3.14 || theta[i]<-3.14)
{
i=steps;
}
}
if (theta[10000]=omega*time[10000])
printf("The value of theta is correct\n");
else
printf("The value of theta is incorrect");
// }
system("PAUSE");
{
FILE *time, *theta; //variables to hold the file name
int i,x;
time= fopen("testin.txt","r"); //opening file for reading
theta= fopen("testout.txt","w"); //opening file for writing
for (i=0;i<10;i++)
{
fscanf(time,"%f",&x); //reading values from file
fprintf(theta, "%f %f\n",x,x*x);
}
fclose(theta); //close file
fclose(time); //close file
}
}