Thread: Sine Wave Program

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    12

    Sine Wave Program

    Before high resolution displays became common, computer terminals were often used to display graphs of equations using only text characters. A typical technique was to create a verticalo graph by spacing over on the screen, then displaying an *. Write a program that displays the graph of an increasing frequency sine wave this way. The program should ask the used for the initial step-size in degrees and the number of lines of the graph to display.


    Im a little confused about how to go about writing this program. Im looking at my book and theres different kinds of loops, and I cant figure out which one to use. If anyone could push me in the write direction I would really appreciate it. I dont need a written program just what I more or less need to do.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Well you need to get an API to handle the grpahics - something where you can set specific pixels. Then you just need to use a "for" loop to find a series of values for the sin function, calcaulate what pixels would represent those values on the screen, and plot those pixels. Just take the problem one step at a time and the individual parts are easier to understand.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Well you need to get an API to handle the grpahics - something where you can set specific pixels.
    The OP is supposed to print the sine wave using asterisks. There's no need for a graphics API.

    To the OP: Try to give it at least somewhat of a shot before asking questions. Do you know how to ask the user for the required input? When you get something done, let us know how far you've gotten, what you're stuck on and then I'm sure you'll get some direction.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    The OP is supposed to print the sine wave using asterisks. There's no need for a graphics API.
    Ah yes - well in that case just calculate the locations on the screen where asterisks need to be (you'll need to do some significant rounding). I would suggest having a 2d array of chars that represents locations on the screen - when you're done calculating the locations and setting the correpsonding location in the array, just print the array to the screen.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    12
    Having the user ask for what angle is what I dont know how to do. Now that I know what loop I should use I can probably get that done. Ill try and post my code when I have done a little bit more on it. Also, Im using pico a compiler in unix so how do I get it from unix to here?

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Pico is just a text editor. Try doing cat myprog.c at the command line, then copy and paste that ouput.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    Elmer Grubbs ECE 175? i'm in the same class and stuck on the same program.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    12
    Yeah actually I am in that class. Good to know Im not the only one having issues with the program.

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    chekc out this thread i found. it was posted earlier and looks to be the same thing.

    http://cboard.cprogramming.com/showt...ight=sine+wave

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    12
    Well heres my code Its not working properly and I know im missing somethings but I dont know what:

    Code:
    #include <math.h>
    #define w 0.25
    
    int
    main(void)
    {
    int number_line; /*input - number of lines to be displayed.*/
    int x; /* frequency of sine wave graph. */
    double z; /* spaces. */
    int y; /* number of spaces. */
    
    /* number of lines*/
    printf("number of lines to be displayed> ");
    scanf("%d", &number_line);
    
    /* loop used to display sine wave graph.*/
    for (x = 0; x < number_line; x++) {
    z = 40 * sin(w * x) + 40;
    for (n = 0; n < y; n++)
    printf(" ");
    printf("*\n");
    w += .1;
    }
    return(0);
    }
    Last edited by Stiks; 10-14-2005 at 11:33 PM.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > z = 40 * sin(w * x) + 40;
    > for (n = 0; n < y; n++)
    Try better variable names, then you would see that you completely ignore the z calculation result, and go on to use something uninitialised.

    > #define w 0.25
    You can't do w += .1 with this declaration of w
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. sine C program with Power Series
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-08-2001, 10:46 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM