-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcw1.c
54 lines (51 loc) · 1.39 KB
/
cw1.c
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <math.h>
const double PI = 3.141592653; // declare PI and initialize it with 3.1415926...
/*
plot sin(ax+b)
input: a, b
output: nothing, just prints the graph
*/
void plot(double a, double b)
{
// printf("Please expand your commandline window if the graph is not displayed properly.\n\n");
printf("Function graph for sin(%.2fx+%.2f):\n\n", a, b);
for (double y = 1; y >= -1; y = y - .2) // loop over each y value
{
for (double x = 0; x <= PI * 2; x = x + .1) // loop over each x value
{
if (fabs(y) < 1e-5)//print x axis labels
{
if (fmod(x, PI / 2) < 1e-1) // print an x mark for multiples of pi/2
{
printf("X");
}
else
{
printf("-");
}
}
else if (fabs(x) < 1e-5)//print y axis label
{
printf("|");
}
else
{
if (fabs(y - sin(a * x + b)) < .1) // print function sin(ax+b)
{
printf("*");
}
else
{
printf(" ");
}
}
}
printf("\n");
}
}
void main()
{
// test my function
plot(2, PI / 2);
}