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

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    3

    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. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    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. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    All trigonometric function in "math.h" take radians as input, not degrees. The sin of an angle is a value between -1 and 1.
    Devoted my life to programming...

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    3

    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. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by najia jaleed View Post
    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.

    Quote Originally Posted by GReaper View Post
    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. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    3

    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. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    How do you expect dividing by 1 to do anything?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-06-2011, 01:08 AM
  2. how to open files and print line by line in shell
    By omega666 in forum Linux Programming
    Replies: 4
    Last Post: 04-15-2011, 04:54 PM
  3. Why the program prints line twice?
    By red463 in forum C Programming
    Replies: 15
    Last Post: 04-30-2010, 05:20 AM
  4. Replies: 13
    Last Post: 05-08-2006, 11:14 AM
  5. print driver that prints to a PDF file
    By planet_abhi in forum C# Programming
    Replies: 2
    Last Post: 11-11-2005, 12:53 AM

Tags for this Thread