Thread: Sine Wave Program Help

  1. #1
    Unregistered
    Guest

    Angry Sine Wave Program Help

    I need to create a program that displays the graph of an increasing frequency sine wave. The program should ask the user for an initial step size in degrees and the number of lines of the graph to display. I thought I had the program done but when I run it and enter anything as the initial step size and 10 lines to print I see something like this: (no matter what number in degrees I print)
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *

    Here is my code: (can someone tell me what is wrong?)

    #include <stdio.h>
    #include <math.h> /* contains sin function */

    #define PI 3.14 /* used in conversion of degrees to radians */
    #define SCALE 35

    void main(void)
    {

    int counter, index, lines;
    double degree, radians, scale_sine;

    printf("Type in the initial step size in degrees:\n");
    scanf("%lf", &degree);
    printf("Type in the number of lines to be printed:\n");
    scanf("%d", &lines);

    radians = degree * PI/180; /* convert degrees to radians */
    scale_sine = SCALE * sin(radians);

    for(counter = 0; /* loop to print sine wave */
    counter < lines;
    ++counter)
    {
    for(index = -SCALE;
    index < scale_sine;
    ++index)
    {
    printf(" ");
    }

    printf("*\n");
    }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you're having problems, it's often handy to include your own debug lines:
    Code:
    #include <stdio.h> 
    #include <math.h> /* contains sin function */ 
    
    #define PI 3.14 /* used in conversion of degrees to radians */ 
    #define SCALE 35 
    
    void main(void) 
    { 
       int counter, index, lines; 
       double degree, radians, scale_sine; 
    
       printf("Type in the initial step size in degrees:\n"); 
       scanf("%lf", °ree);
       /**
       *** I will assume an err on your part here and assume you
       *** have this line correct in your actual code, right? If not,
       *** here is your problem.
       **/
       
       printf("Type in the number of lines to be printed:\n"); 
       scanf("%d", &lines); 
    
       radians = degree * PI/180; /* convert degrees to radians */ 
       scale_sine = SCALE * sin(radians); 
    
       /**
       *** Debug lines.
       **/
    
       printf("DEBUG: %f radians. %f scale_sine. %f degree.\n",
          radians, scale_sine, degree );
    
       /**
       *** See how simple that is, and how benificial it can be?
       **/
    
    
       for(counter = 0; counter < lines; ++counter) 
       {
          /**
          *** Debug Lines.
          **/
          printf("DEBUG: %d counter < %d lines\n",
             counter, lines );
    
          for(index = -SCALE; index < scale_sine; ++index)
          { 
             printf(" "); 
          } 
          printf("*\n"); 
       } 
    }
    This way you can be sure your numbers are correct.
    You could even make a macro to do it for you.

    Quzah.

  3. #3
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    The inner loop does not change with the variables in the outer loop , so the same thing will be printed in each line

    for(counter = 0; /* loop to print sine wave */
    counter < lines;
    ++counter)
    {

    /*this loop does not depend on counter , so the same thing will be printed each time*/
    for(index = -SCALE;
    index < scale_sine;
    ++index)
    {
    printf(" ");
    }

    printf("*\n");
    }

  4. #4
    Unregistered
    Guest

    Smile I Got It.

    I can't believe I didn't think of this before.
    I needed to add something to the main for loop to increment the degree and something else to re-calculate scale_sine.
    Thank all of you for your help!

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