Hey guys, I started on this simple plotting program, and everything seems to be fine with the program, no errors came up, but its not plotting the graph. Here's the code, and any suggestions would be more than appreciated.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define X_SEMIRANGE 3
#define Y_SEMIRANGE 2
int rtoi(double);
double f(double);
char graph[Y_SEMIRANGE*20+1][X_SEMIRANGE*20+1];/*store x and y points in a
character array*/
int creategraph();
int plot();
int dispplot();
int main()
{
creategraph();
plot();
dispplot();
getchar();
return 0;
}
int rtoi(double x) /*rounds the double values of f() to ints before storing
points in the array*/
{
int i;
if(x >= 0)
i = (int)(x + 0.5);
else /* x < 0 */
i = (int)(x - 0.5);
return i;
}
double f(double x) /*holds the function to be graphed. Can be easily
overwritten*/
{
return cos(x*x);
}
int creategraph()
{
int i,j;
for (i=0; i<=X_SEMIRANGE*20+1; i++)/*for loop defines where axes should be
placed*/
{
for (j=0; j<=Y_SEMIRANGE*20; j++)
{
if(i==(X_SEMIRANGE*20/2))
graph[j][i] = '|';
else if(j==(Y_SEMIRANGE*20/2))
graph[j][i]='-';
else
graph[j][i]=' ';/*inserts empty spaces in the array, this will
be replaced in the the graph*/
}
}
}
int plot()
{
int x_coor,y_coor;
double range;
double func;
for(range=-1*((X_SEMIRANGE*20)/2); range <=((X_SEMIRANGE*20)/2); range++)
/*for loop will plot the points onto graph, it starts from negative
X_SEMIRANGE and goes to the end of X_SEMIRANGE*/
{
func = (f(range));/*units in x direction will increment by 1/10*/
y_coor=(Y_SEMIRANGE)*20/2-rtoi(func);/*values are rounded and
plotted where the graph is*/
x_coor=(X_SEMIRANGE)*20+range;
if(y_coor >= 0 && y_coor <=20*Y_SEMIRANGE)
graph[y_coor][x_coor]='o'; /*inserts 'o' where I previousle had
empty spaces in the previous for loop*/
}
}
int dispplot()
{
int i,j;
for(j=0; j <= Y_SEMIRANGE*20; j++)/*checks if any graph gets cut off
by semirange*/
{
for(i=0; i <= (X_SEMIRANGE*20); i++)
{
printf("%c",graph[j][i]);
}
printf("\n");
}
}