-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsine_print.c
More file actions
36 lines (29 loc) · 1.23 KB
/
sine_print.c
File metadata and controls
36 lines (29 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* sine_print.c
Print a sideways sine wave pattern using ASCII characters
Outputs an 80-byte array of ASCII characters (otherwise
known as a string) 20 times, each time replacing elements
in the array with an asterisk ('*') character to create a
sideways plot of a sine function.
From: https://www.oreilly.com/library/view/real-world-instrumentation/9780596809591/ch04.html
*/
#include <stdio.h> /* for I/O functions */
#include <math.h> /* for the sine function */
#include <string.h> /* for the memset function */
int main()
{
/* local variable declarations */
int i; /* loop index counter */
int offset; /* offset into output string */
char sinstr[80]; /* data array */
/* preload entire data array with spaces */
memset(sinstr,0x20, 80);
sinstr[79] = '\0'; /* append string terminator */
/* print 20 lines to cover one cycle */
for(i = 0; i < 20; i++) {
offset = 39 + (int)(39 * sin(M_PI * (float) i/10));
sinstr[offset] = '*';
printf("%s\n", sinstr);
/* print done, clear the character */
sinstr[offset] = ' ';
}
}