# Thread: Sine Wave Program

1. ## 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. 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. 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.

4. 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. 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. Pico is just a text editor. Try doing cat myprog.c at the command line, then copy and paste that ouput.

7. Elmer Grubbs ECE 175? i'm in the same class and stuck on the same program.

8. Yeah actually I am in that class. Good to know Im not the only one having issues with the program.

9. 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. 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);
}```

11. > 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

Popular pages Recent additions