Okay, I should move a different forum then?
Also, I've just done what you've said and it is writing to the file. Just not the right values. Its just the same two values over and over again...
Edit:
I think what it is doing is just writing the end values of the for loop 100 times, instead of all of the values calculated in the for loop
Yeah, looking at your code you'll be writing the same values over and over.
You can do one of two things:
1) Move your write function to inside the loop where each value is calculated and automatically write to a file, rather than ask the user.
2) Store the results in an array(s), ask the user if they want to print, then iterate through the array(s), printing out each element as you go.
I would opt for the latter approach. Create a struct, containing a float for time and a float for thetaValue. Then, in main, you'd create an array of these structs (make sure the size of your array matches your 'steps' variable, which, by the way, may be better as a constant). Then, when you do your calculations, just populate the corresponding element of the array.
Later on, you can loop through the array and print them out.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
main()
{
float omegaValue;
float thetaChange;
float omegaGravValue;
float thetaValue;
float gravity = 9.81;
float length = 5.15;
int answer;
FILE *fp_out;
char Y;
printf("Enter a value for omega\n"); //finding value for omega
scanf("\n%f", &omegaValue);
printf("Omega=%f\n" ,omegaValue);
int i; //step counter
float theta = 0; //initial value for angle
float omega = omegaValue; //initial value for angular speed
float time[100]; //initial time
time[0]=0;
float dt = 0.01; //time step
int steps = 100; //number of steps
for(i=0; i<steps; i++) {
time[i] = time[i-1] + dt;
omegaGravValue = omegaValue-(gravity/length)*dt*sin(thetaValue);
thetaChange = theta + omegaGravValue*dt;
thetaValue = theta+thetaChange*i;
printf("Time equals %f. Theta equals %f. Omega equals %f\n",time[i],thetaValue,omegaGravValue);
}
printf("Do you want to write this data to a file? y/n\n");
scanf("\n%c", &answer);
if(answer == 'y') {
printf("Writing to file...\n");
fp_out=fopen("testout.txt","w");
for(i=0;i<100;i++){
fprintf(fp_out,"%f %f\n",time[i],thetaValue);
}
fclose(fp_out);
}
else {}
system("PAUSE");
}
I turned the time into an array and added the [i] and the [i-1] to the calculations. But when I try with thetaValue, it just comes up with #0NQAND# or something like that
Imagine the first iteration of your loop, when i = 0. In your code, you access the array element i - 1. In this iteration, i - 1 is -1 and is an illegal index of your array.
Also that's for your first for loop, when you're assigning the time to the array.
You'd write the file by using the line you have in your second loop.
Though you probably want something similar for thetaValue. Otherwise, when you write out to the file, thetaValue will always be the same.
If you do it this way, you have the problem of two concurrent arrays. They can be troublesome, especially if your project grows. Structs are the way forward with this one. :-)