The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Graphs In C
Discuss Graphs In C in the C Programming forum on Dev Shed. Graphs In C C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 7th, 2003, 05:11 AM
|
|
Junior Member
|
|
Join Date: Feb 2003
Location: Liverpool
Posts: 6
Time spent in forums: < 1 sec
Reputation 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

|

March 7th, 2003, 06:37 AM
|
 |
jasondoucette.com
|
|
Join Date: Feb 2003
Location: Canada
Posts: 378

Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
|
|
|
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
|
|
Junior Member
|
|
Join Date: Feb 2003
Location: Liverpool
Posts: 6
Time spent in forums: < 1 sec
Reputation 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
|

March 7th, 2003, 07:23 PM
|
 |
jasondoucette.com
|
|
Join Date: Feb 2003
Location: Canada
Posts: 378

Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
|
|
|
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
|
|
Junior Member
|
|
Join Date: Feb 2003
Location: Liverpool
Posts: 6
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
yes my formula is
Y=2X+5

|

March 8th, 2003, 12:20 PM
|
 |
jasondoucette.com
|
|
Join Date: Feb 2003
Location: Canada
Posts: 378

Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
|
|
|
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
|
|
Junior Member
|
|
Join Date: Feb 2003
Location: Liverpool
Posts: 6
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
simple one in text
|

March 8th, 2003, 04:53 PM
|
 |
jasondoucette.com
|
|
Join Date: Feb 2003
Location: Canada
Posts: 378

Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|