#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* Puzzle A30 -- print a wavey line down the page */
const int nRows=18 ;
const int nCols=21;
/* Number of complete cosine waves to fit into nRows rows*/
const int cycles=2;
int main()
{
int row, col ;
int centCol = nCols/2;
int amplitude;
double theta; /* angle in radians */
for ( row=0; row<nRows; row++ )
{
theta = (2*M_PI*cycles/nRows)*row;
amplitude = (int)((nRows/4)*cos( theta ));
for ( col=0; col<centCol+amplitude; col++ )
printf(" ");
printf("*\n");
}
printf("\n");
return 0;
}
Comments: You need to include math.h for cos() and
for M_PI. Remember that the argument to cos() is in
radians and must be a double precision value.
Another interesting effect is to gradually diminish the amplitude of the wave to zero as it goes down the page.