1. ## File Data Saving

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;
theta=0;   // this is the initial value for the angle
float omega=0.1; // this is the initial value for the angular speed.
float time;    // 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;
//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=omega*time)
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++)
{
fprintf(theta, "%f %f\n",x,x*x);
}
fclose(theta); //close file
fclose(time); //close file
}
}``` 2. > if (theta=omega*time)
1. These array elements don't exist.
The last subscript in an array of 10000 elements is 9999 (it's always N-1 for an array[N])

- You're using the same variable names for file handles as you used for pendulum calculations.
- You're just making a table of x2

Perhaps
Code:
```FILE *out = fopen("pendulum.txt","w");
for ( i = 0 ; i < size ; i++ ) {
fprintf("%d: %f %f\n", i, time[i], theta[i]);
}``` 3. ## Checking if the data is saved correctly.

Thank you very much. But do you know how to check if the data is saved correctly? Like how to check if there are the right number of data entries and to check if these are formatted correctly? 4. Like how to check if the data is saved to a file when the program is finished running? 5. 6. > Like how to check if the data is saved to a file when the program is finished running?
Well you could check the return result of every single fprintf call.

On some level, you have to assume that some things just work.

Otherwise, you end up with an infinite recursion of
- how do I check the data was saved
- how do I check the check was correct
- how do I check the check of the check was correct
.... Popular pages Recent additions coursework, data, entries, file, program 