### Thread: Graphs In C

1. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2003
Location
Liverpool
Posts
6
Rep Power
0

#### 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:
2. 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'.
3. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2003
Location
Liverpool
Posts
6
Rep Power
0
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
4. 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)?
5. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2003
Location
Liverpool
Posts
6
Rep Power
0
yes my formula is

Y=2X+5

:confused:
6. 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?
7. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2003
Location
Liverpool
Posts
6
Rep Power
0
simple one in text
8. 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 y-axis 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 x-axis and y-axis 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.