I need to find the period of wave using zero crossings given a set of values that represents a sine wave...

I'm a little new to C so there is a chance I made this over complicated, but...

oh also, the time of the zero-crossing can be estimated as tz=(t1+t2)/2 and after finding the first two zero-crossings, twice the time difference between those points is the estimated period.

#include <stdio.h>

int main( void)
{
  int s; // scanf return status

  double d, y1, y2, tz, tz2, t1=0, t2=1;

  while( 1) // until end-of-file or error
  {
    s = scanf("%lf",&d); // scans for given values
    if( s != 1) // could not read one double, must be end-of-file or error
      break;
    y2=y1;
    y1=d;

    if (y1>0)// I have two possibilities y1>0 and y1<0
    {
      while (y2>0) //there would not be a zero crossing, so code would break out of loop
      {
      break;
      }
      while(y2<0){
        tz=(t1+t2)/2;
        printf("%g/n",tz);
        break;} // gets the first tz value
    else if (y2 != 0){
     break;
      }}}

    if (y1<0)
    {
     while (y2<0)
     {
     break;
     while(y2>0){
     tz2=(t1+t2)/2;
     printf("%g/n",tz2);
     break;}
     else if (y2 =! 0)
    break;
    }}}
    t1++;
    t2++;
    printf("%g/n",(tz2-tz)/2);
    return 0;
}

Sorry about not putting the code in right .....

#include <stdio.h>

int main( void)
{
int s; // scanf return status

double d, y1, y2, tz, tz2, t1=0, t2=1;

while( 1) // until end-of-file or error
{
s = scanf("%lf",&d); // scans for given values
if( s != 1) // could not read one double, must be end-of-file or error
break;
y2=y1;
y1=d;

if (y1>0)// I have two possibilities y1>0 and y1<0
{
while (y2>0) //there would not be a zero crossing, so code would break out of loop
{
break;
}
while(y2<0){
tz=(t1+t2)/2;
printf("%g/n",tz);
break;} // gets the first tz value
else if (y2 != 0){
break;
}}}

if (y1<0)
{
while (y2<0)
{
break;
while(y2>0){
tz2=(t1+t2)/2;
printf("%g/n",tz2);
break;}
else if (y2 =! 0)
break;
}}}
t1++;
t2++;
printf("%g/n",(tz2-tz)/2);
return 0;
}

Next time, try some indentations. It makes things a LOT easier to read!

#include <stdio.h>

int main( void)
{
    int s; // scanf return status

    double d, y1, y2, tz, tz2, t1=0, t2=1;

    while( 1) // until end-of-file or error
    {
        s = scanf("%lf",&d); // scans for given values
        if( s != 1) // could not read one double, must be end-of-file or error
            break;
        y2=y1;
        y1=d;

        if (y1>0)// I have two possibilities y1>0 and y1<0
        {
            while (y2>0) //there would not be a zero crossing, so code would break out of loop
            {
                break;
            }
            while(y2<0)
            {
                tz=(t1+t2)/2;
                printf("%g/n",tz);
                break
            } // gets the first tz value
            else if (y2 != 0)
            {
                break;
            }
        }
    }

    if (y1<0)
    {
        while (y2<0)
        {
            break;
            while(y2>0)
            {
                tz2=(t1+t2)/2;
                printf("%g/n",tz2);
                break;
            }
            else if (y2 =! 0)
                break;
        }
    }
}
t1++;
t2++;
printf("%g/n",(tz2-tz)/2);
return 0;
}

Doing that, and matching up the braces (my editor does that nicely), you can see that there are SERIOUS structural problems with your code. Clean them up first before you continue with this thread.

@line 14 y1 is not yet initialized so y2 won't have a value

@ the while loops
the value of y1 or y2 doesn't change so there won't be a chance to exit the loops

Doing that, and matching up the braces (my editor does that nicely)

@rubberman
you mean when you copied the code to your editor then it indented it properly.
is it automatic, push of a button, or did you do it manually?

what editor are you using, I'm curious :)

I use an X-Windows editor for Linux or Cygwin on Windows called nedit. On Windows I use notepad++. The nedit editor will match braces very nicely, and will, if you want, auto-indent everything for you, as well as auto-indent as you type in code. It knows about many programming languages, and will deal with each appropriately - everything from Ada to Yacc, and most stuff in between such as Java, Python, Perl, Make, Matlab, Verilog... It was originally written by a staff member of the Fermi National Accelerator Lab computing division, but has been released into the open source community and is now hosted on SourceForge.net.

commented: Thank you +9
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.