# Thread: i want to print a sin wave but my program just prints a line help!!!!!!!!

1. ## i want to print a sin wave but my program just prints a line help!!!!!!!!

Code:
```#include <iostream>
#include <math.h>
#include "myconsole.h"
using namespace std;
int main ()
{
int x,i;
float y;
ClearScreen ();

x=0;
y=30;
PlaceCursor (x,y);
for (i=0;i<=40;i++)
{
y = sin(x);
y=y-(10);
PlaceCursor(x,y);
cout <<".";
x=x+10;

}
}```

2. What is the prototype of PlaceCursor? I assume this is in "myconsole.h". I'd be willing to bet that it's:

Code:
`void PlaceCursor(int x, int y);`
Where x and y are some coordinates referring to some bit of screen somewhere. Of course I am only guessing, but if this is right, then note that the type of y you call PlaceCursor with is a float. It will be truncated to an integer. This might be ok in some situations, but sin oscillates between -1 and 1 (Sine wave - Wikipedia, the free encyclopedia), meaning your y coordinates only vary by 3!

I think you'll have to scale your y coordinates by some value that gives you a nice curve. I imagine you can just multiply y by something -- but what depends on how course the x,y values are. Actually I'm not convinced that'll give you a good curve -- but anyway, I'm pretty sure that's why you're just getting a line.

3. All trigonometric function in "math.h" take radians as input, not degrees. The sin of an angle is a value between -1 and 1.

4. It is very likely that PlaceCursor will only take the whole number part of the float y you give it (either because it actually takes an int as an argument, or because that's what it does). Most of the sin values you are getting are truncated to 0. Go to a finer scale when you draw your graph.

y = sin 10 = -.54402 might be very small, but at a finer scale where y can be tenths, you have to do some extra math y = sin (10) * 10 = -5.4402
In effect, make a point at about (10, -5). That is distinguishable from zero on a coordinate plane where y can be whole numbers.

There could also be a problem with using negative values in PlaceCursor, if the x and y coordinate are supposed to be a column and row on the screen. You will have to figure out what row and column means (0,0) and actually find the difference to plot negative coordinates in the right quadrant.

5. ## sine wave

ok thanks i do get what you are saying about y axis but can you help me by telling how to plot negative y-axis i thought the top of the dos grid was 0

6. Originally Posted by najia jaleed
ok thanks i do get what you are saying about y axis but can you help me by telling how to plot negative y-axis i thought the top of the dos grid was 0
You can't plot negative y coords if the dos box starts at 0. But that's ok - you can just move the entire plot downward (increasing y).

The smallest result that sin can output is -1 - so the smallest number you'll try to plot will be minus your scaling factor. So if you're multiplying y by 10, the lowest it can be is -10. So if you just add 10 on to the y value before plotting it, it'll be in a sensible place.

Originally Posted by GReaper
All trigonometric function in "math.h" take radians as input, not degrees. The sin of an angle is a value between -1 and 1.
That's a very good point! Currently the code appears to be expecting to input degrees, from 0-400. 700 degrees is about 7 radians, so you need to make the x increment much smaller! If you want the same number of points just increment by 7/40 = 0.175. Since you're now dealing with sub integer x values, you'll need to scale them too.

7. ## thanks i got it here is my code to move 5 stars on the curve neeeeeed help

as i have set the max range of x as 2PI i.e 6.28 so in order to move the stars from beggining again i had to set if (x>=6) but on the first cycle it prints 4 stars in the last half cycle then in the next cycle it prints two in the end then 5 ,then 3,then1 how to fix this???
Code:
```#include <iostream>
#include <math.h>
#include "myconsole.h"
#include <windows.h>
using namespace std;
int main ()
{
double x,y;
int ax,by;
int counter=0;
ClearScreen ();

for (x=0;x<=6.28;x=x+0.348)// increments x axis by 10 degrees
{
y = sin(x)*10;
ax=(x*10)/1; //stores an integer value
by=(y/1)+15;//magnifies output along y axis

PlaceCursor(ax,by);
cout <<"*";
counter=counter+1;
if (counter%5==0)
{Sleep(1000);
ClearScreen ();
}
if (x>=6)
{
x=0;
}
}```

8. How do you expect dividing by 1 to do anything?