March 7th, 2003, 05:11 AM

Graphs In C
How do you create a basic graph in C. I am using the borland compiler and i need the program to ask the user for the step size and the starting point of x.
Thanks
:confused:
March 7th, 2003, 06:37 AM

I think we need more information to answer your question. Such as: are you writing a console app, or do you have graphics unit, are you using DirectX/OpenGL, etc. What is your compiler (i.e. DOS, win32, etc)? And what type of graphs do you wish to draw? It would help us understand what you mean by 'starting point of x' and 'step size'.
March 7th, 2003, 04:45 PM

Basicly i wish to create an .exe file that will run in windows. The compiler is win32 (Youll have to bear with me im new to all ths)






________________

Above is how i wish my graph to look. The step size is the amount of units on the x axis between the current number and future number. i.e. 2, 4, 6 The setp size is 2 because there are two spaces in between each number.
The starting point of the X axis is the number it starts on. So if the starting point was 5, and the step number is 5 the graph would start: 5, 10, 15, 20
Thanks
March 7th, 2003, 07:23 PM

So, you are looking to plot an equation, like y = 2*x^2  3*x + 2 or y = cos(x) for example (i.e. where y is a function of x)?
March 8th, 2003, 04:54 AM

yes my formula is
Y=2X+5
:confused:
March 8th, 2003, 12:20 PM

Are you looking to create the graph in a high resolution bitmap mode (if so, what graphics API are you using?), or just a simple one in text mode?
March 8th, 2003, 04:10 PM

March 8th, 2003, 04:53 PM

An easy way to do this is to use a 2D array of char (an array of strings, essentially). You can initialize the array to spaces at first, then draw the x and yaxis with  and  characters. Then 'draw' the graph to the array.
You will need to know what x value each column of the graph represents. Use variables like xmin, xmax to store the x values for the extreme left and right columns, and xstep to mean the difference in x value from one column to another (if you ask the user for xstep, then you can compute xmax from this value if you know what xmin is).
Go through every column of the graph, and compute its y value. If it is within range (after rounding off to fit in a particular row), change that character in the graph to * or some other character. Obviously, you need ymin and ymax values set, as well. These will all need to be known when you draw the xaxis and yaxis to the array.
Now print it to the output one row at a time (one string at a time). Take care to print a \n character at the end of each row.