I'm in an intro C programming class, and I need to write a program that uses the output of a previous program I wrote as input to compute the period of a sine wave. We have an interface that reads the input for us, but here is the code I have written so far:
Code:
//estimates the period of given sine data
#include <stdio.h>
#include <math.h>
int main(void)
{
//defining variables
int s;
double y1=0,y2=0,d,t1=0, t2=0;
while(1) // until end-of-file or error
{
s = scanf("%lf",&d); // try to read one double
t2=t1, t1++;
if( s != 1) // could not read one double, must be end-of-file or error
break;
y1=y2,y2=d;
if ((y1>0&&y2<=0)||(y1<=0&&y2>0)) //if the two data values have opp. signs
printf("%5.2f\n",(t2+t1)/2);
}
return 0;
}
Now it's not like I expected this to print me the period, because I obviously haven't written any code to do anything of the sort. That being said, this prints me all of the values where zero-crossings occur. How can I change the program/write something that will take two of these consecutive values, and subtract one from the other?
Right now my output is something like:
9.50
22.50
36.50
52.50
etc.
And I need to compute, for example, 2*(22.50-9.50)