Need help with sine waves

Here's the thing... I wanted to do a sine wave and i did it, however i want to put a * at each turning point.. i can't seem to find an equation for it with using if. Also, i want to use / if the waveform is falling and \ if it is rising.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 #include<stdio.h>
#include<math.h>
#include<stdlib.h>

int main()
{
    double angle, sinVal, numSpaces;
    int numSteps;
    double maxAngle=M_PI*2;
    int i,j;
        
   
    do {printf("Input the number of steps: ", numSteps);
    scanf("%d", &numSteps); }
     while(numSteps<=2);
        
    for(i=0; i<=numSteps; i++)
    {      
         angle=(double)i/(double)numSteps*maxAngle;
         sinVal=sin(angle);
         numSpaces=30+sinVal*30;
         printf("%3d:%5.2f",i,angle);  
        for(j=0; j<=numSpaces; j++)
        {
         printf(" ");                                                                                                                                     
        
// if();
//{ printf("*"); }                       }
                                   
          	}
    	printf("\\\n"); 
	}
	system("PAUSE");
return 0;
}
closed account (48T7M4Gy)
Unless you want to determine the changes numerically, the answer lies in the derivative function cos(x).

sin(x) is rising when cos(x) >0, falling when cos(x) < 0 and a 'turning point when cos(x) = 0..
I get that but the problem is that the * keeps popping up everywhere instead of just one place! I tried so many equations but the * just wont go where it belongs. and if i run the program with the if included then its not gonna work like i how i want it to be.
I really can't guess what is wrong.
1
2
3
4
5
6
7
8
#include <cmath>
...
int ch;
deriv = cos(angle);
if (fabs(sinVal) < 0.001) ch = '0';   // pick an appropriate tolerance
else if (cosVal < 0) ch = '/;
else ch = '\\';
putchar(ch);   // faster than printf 


To really get the spot where it crosses zero you need to pick that tolerance carefully. It must be big enough to catch the change but small enough that that you don't get two positions showing it.
Last edited on
1
2
3
4
5
      const double cosVal = cos( angle );
      const double epsilon = 0.001;  // magic is here
      const char x = ( cosVal < -epsilon ) ? '/' : ( epsilon < cosVal ) ? '\\' : '*';

      std::cout << std::setw( numSpaces ) << x; // or loop of printf 

hmmmm...... so your saying that i have to put in cos?
is there any other way where i don't have to involve cos in this and make it work?
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<stdio.h>
#include<math.h>
#include<stdlib.h>

int main()
{
    const double PI = 3.14159265359;
    double angle, sinVal, cosVal, numSpaces;
    int numSteps;
    double maxAngle = PI*2;
    int i,j;

    char slash ='\\';

    do
    {
        printf("Input the number of steps: ", numSteps);
        scanf("%d", &numSteps);
    }
    while(numSteps<=2);

    for(i=0; i<=numSteps; i++)
    {
        angle=maxAngle/numSteps*i;
        sinVal=sin(angle);
        cosVal=cos(angle);
        if(cosVal > 0)
            slash ='\\';
        if(cosVal < 0)
            slash ='/';

        numSpaces=30+sinVal*30;
        printf("%3d:%5.2f",i,angle);
        for(j=0; j<=numSpaces; j++)
        {
            printf(" ");
        }

        printf("%c\n", slash);
    }
    system("PAUSE");
    return 0;
}


It seems your casting in OP line 19 was causing problems. The next challenge is to follow up the tolerance idea for the peaks.
Thanks! I somehow almost get this.... there is just a tiny problem.... the / and \ are not falling or rising as it should have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
	double angle;
	int numSteps;
	double Compair=0;
	double maxAngle=M_PI*2;
	int i,j,k,m;
	double sinVal;
	double numSpaces;
	m=(sinVal<0)-1;
	printf("Input the number of steps ");
	scanf("%d",&numSteps);
	for(k=0;numSteps<=2;k++)
	{
		if(numSteps<=2)
		{	
		printf("Input the number of steps ");
		scanf("%d",&numSteps);
		}
	}
	
		for(i=0;i<=numSteps;i++)
		{
			angle=(double)i/(double)numSteps*maxAngle;
			sinVal=sin(angle);
			numSpaces=30+sinVal*30;
			printf("%3d:%5.2f",i,angle);
			for (j=0; j<numSpaces; j++)
		 	{             
     			printf(" ");
     		}
     		
     		if (numSpaces==0 || numSpaces==60)
     		{printf("*\n");}
			else if (sinVal>0)
     		{printf("\\\n");}
     		else if (30<sinVal<=60)
			{printf("/\n");}
			
     	}
		system("PAUSE");	    			
return 0;
}
ok...I did as 'kemort' suggested and it looked REALLY good right now!
except that...at the turning point there is still a \ and / next to my *
how do i get rid of it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
	double angle;
	int numSteps;
	double Compair=0;
	double maxAngle=M_PI*2;
	int i,j,k,m;
	double sinVal, cosVal;
	double numSpaces;
	m=(sinVal<0)-1;
	printf("Input the number of steps ");
	scanf("%d",&numSteps);
	for(k=0;numSteps<=2;k++)
	{
		if(numSteps<=2)
		{	
		printf("Input the number of steps ");
		scanf("%d",&numSteps);
		}
	}
	
		for(i=0;i<=numSteps;i++)
		{
			angle=(double)i/(double)numSteps*maxAngle;
			sinVal=sin(angle);
			cosVal=cos(angle);
			numSpaces=30+sinVal*30;
			printf("%3d:%5.2f",i,angle);
			for (j=0; j<numSpaces; j++)
		 	{             
     			printf(" ");
     		}
     		
     		if (numSpaces==0 || numSpaces==60)
     		{printf("*");}
			  if(cosVal >= 0)
            {printf("\\\n");}
              if(cosVal <= 0)
            {printf("/\n");}
			
     	}
		system("PAUSE");	    			
return 0;
}
If cosVal would be exactly 0 (and numSpaces 0 or 60), then you would print all three ( *, /, \ ).

You did use else for something before. Not all of that was bad.



Note. You could do without cos:
If current is larger than previous, then current is either rising or at peak.
If current is smaller than previous, then current is either falling or at bottom.
If current and previous are equal, then you have flat.
In order to check these cases, you have to know previous, current, and next value.
Last edited on
Here's another approach. If numSpaces == 30 then print '*'. The idea is that if you're printing on the axis then print *. Otherwise print \ or /
closed account (48T7M4Gy)
@ dhayden
Excellent move, actually should be 60 as keskiverto says. numSpaces == 0 would need to be included :-)
Topic archived. No new replies allowed.