Solution for Exercise 11-16

ex1116

#include <stdio.h>
#include <math.h>

#define PI 3.14159
#define WAVELENGTH 35
#define PERIOD .1

int main()
{
    float graph,s,x;

    for(graph=0;graph<2*PI;graph+=PERIOD)
    {
        s = cos(graph);
        s+=1.0;         /* compensate for negative values */
        for(x=0;x<s*WAVELENGTH;x++)
            putchar('*');
        putchar('\n');
    }
    return(0);
}

Notes

* As I wrote in the solution for Exercise 11-15, don't sweat it if this math is too heavy duty for you.

* Line 14 is a bit of a cheat. By adding 1 to the value of s the code compensates for negative values that wouldn't graph properly. The net effect is that the entire output is shifted so that the graph is displayed. Definitely don't pull that trick if you're calculating waves or angles in your code.

* You get no points off if you weren't able to properly concoct the graph. It took me a while to get it right.