-
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", °ree);
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");
}
}
-
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.
-
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");
}
-
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!