I need to write a program in C that can take any function and graph it. I was able to do this , but my program isn't correctly graphing functions with sine and cosine. I believe it is graphing sine and cosine functions in degree mode instead of in radians. I have had no luck trying to fix this and would really appreciate some help. Thank you.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
# define x_semirange 3 /* this is to define the region to be displayed*/
# define y_semirange 2
double f(double);
int roundoff (double y);
main()
{
int a,b,x;
double func;
char graph[y_semirange*20+1][x_semirange*20+1];/*To store the graph in a 2D array*/
for(a=0;a<=y_semirange*20;a++)/* Drawing the axis of the graph*/
for(b=0;b<=x_semirange*20;b++)
{
graph[a][b]=' ';
if(b==x_semirange*10)
graph[a][b]='|';
if(a==y_semirange*10)
graph[a][b]='-';
}
for(b=0;b<=x_semirange*20;b++)
{
x=b-(x_semirange*10);
func= (f(x));
if(((y_semirange*10)-roundoff(func))>=0&&((y_semirange*10)-roundoff(func))<=y_semirange*20)
graph[((y_semirange*10)-roundoff(func))][b]='o';
}
for(a=0;a<=y_semirange*20;a++)
{
for(b=0;b<=x_semirange*20;b++)
printf("%c",graph[a][b]);
printf("\n");
}
}
double f(double x) /*holds the function to be graphed. Can be easily overwritten*/
{
return sin(3*x);
}
/* Function for converting the double values to int so it can be stored in the array*/
int roundoff ( double y )
{
int i;
if (y >= 0)
i = (int)(y + 0.5);
else
i = (int)(y - 0.5);
return i;
}