Solution for Exercise 11-16

ex1116

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

#ifndef M_PI
#define M_PI 3.14159
#endif

int main()
{
    const float amplitude=35;
    const float    wavelength=0.1;
    float graph,s,x;

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

Output

**********************************************************************
**********************************************************************
**********************************************************************
*********************************************************************
********************************************************************
******************************************************************
****************************************************************
**************************************************************
************************************************************
*********************************************************
******************************************************
***************************************************
************************************************
*********************************************
*****************************************
**************************************
**********************************
*******************************
****************************
************************
*********************
******************
***************
************
**********
*******
******
****
***
**
*
*
*
*
**
***
****
******
********
**********
*************
***************
******************
*********************
*************************
****************************
********************************
***********************************
***************************************
******************************************
*********************************************
*************************************************
****************************************************
*******************************************************
**********************************************************
************************************************************
***************************************************************
*****************************************************************
******************************************************************
********************************************************************
*********************************************************************
**********************************************************************
**********************************************************************

Notes

* Here are the changes I made from Exercise 11-15:

* Line 17 is a bit of a cheat. By adding 1.o to the value of s the code compensates for negative values that don't graph properly. The 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 removed if you weren't able to properly concoct the graph. It took me a while to get it right.