Thanks for responses again. I'm ALMOST there! Let me post my code, bold the part that is causing me my (hopefully) final problem, and knock it out!
Code:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define X_SEMIRANGE 3
#define Y_SEMIRANGE 3
double f(double); //prototype of the mathematical function to be graphed
char plot(); //prototype for the function that will plot the graph of f
char makeGraph(); //prototype for the function that intializes the graph to spaces and the x/y coordinates
int intCast(double); //prototype for the function that type casts a double to int correctly
double f(double x)
{
return (2*x);
}
char makeGraph()
{
int x, y, i, j;
double xx, yy;
int Y_AXIS = (Y_SEMIRANGE * 20) + 1;
int X_AXIS = (X_SEMIRANGE * 20) + 1;
int MID_Y = Y_AXIS / 2;
int MID_X = X_AXIS / 2;
int Y_INT, X_INT, Y_POS, X_POS;
char graph[Y_AXIS][X_AXIS];
for(y = 0; y < Y_AXIS; y++) //This nest loop should create the x and y axis
{
for(x = 0; x < X_AXIS; x++)
{
graph[y][x] = ' ';
if(y == MID_Y)
graph[y][x] = '-';
if(x == MID_X)
graph[y][x] = '|';
//putchar(graph[y][x]);
}
printf("\n");
}
//end creating x and y axis
for(xx = -(X_SEMIRANGE); xx < X_AXIS; xx++) //This loop should insert o's for all points on the function
{
yy = f(xx);
Y_INT = intCast(yy);
X_INT = intCast(xx);
Y_POS = MID_Y + Y_INT;
X_POS = MID_X + X_INT;
graph[Y_POS][X_POS] = 'o';
}
for(i = 0; i < Y_AXIS; i++) //This should loop through index positions and output the graph
{
for(j = 0; j < X_AXIS; j++)
{
putchar(graph[i][j]);
}
}
}
int intCast(double x)
{
int i = x;
if (x >= 0)
i = (int)(x + 0.5);
else /* x < 0 */
i = (int)(x - 0.5);
return i;
}
int main()
{
makeGraph();
system("PAUSE");
return 0;
}
The bolded code causes the program to crash. I think I have the right idea on how I am loading the o's on the graph (which are the plotted points on the function line). Am I doing this correctly, and am I outputting the graph correctly? Obviously I am wrong somewhere, because the program is crashing.
Thanks for the guidance!