Monday 23 May 2016

f = cos(x) + cos(y) implemented in C

This program is compiled in Linux using the gcc command
gcc programname.c -lm
The -lm means that the maths funtions are included during compilation.

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

main()
{
const space = ' ', screenheight = 60, ycentre = 30, screenwidth = 70;
const xcentre = 35;
float xscale =7.0 , yscale = 7.0;
char screen[screenwidth][screenheight];
float xs, ys, interval, fvalue;
int period, row, col, ft, x, y, startx, starty, stopx, stopy;
char point;

/*insert spaces*/
for(col=0; col<=screenwidth; col++)
for(row=0; row<=screenheight; row++)
screen[row][col] = space;

startx = -xcentre;
stopx = xcentre;
starty =  -ycentre;
stopy = ycentre;
for(y = starty; y <= stopy; y++)
for(x = startx; x <= stopx; x++)
{
xs = x/xscale;
ys = y/yscale;
printf("xs =%f\n",xs);
printf("xy=%f\n",ys);
fvalue = cos(xs) + cos(ys);
ft=round((fvalue + 2) * 2.5);
printf("%i\n", ft);
switch(ft)
{
case 0: point = 'A';
break;
case 1: point = ' ';
break;
case 2: point = 'B';
break;
case 3: point = ' ';
break;
case 4: point = 'C';
break;
case 5: point = ' ';
break;
case 6: point = 'D';
break;
case 7: point = ' ';
break;
case 8: point = 'E';
break;
case 9: point = ' ';
break;
case 10: point = 'F';
}
screen[x+xcentre][y+ycentre] = point;
}
/*draw graph*/
for(row =0; row <= screenheight; row++)
{
for(col=0; col<=screenwidth; col++)
printf("%c",screen[row][col]);
printf("\n");
}

}

No comments:

Post a Comment