Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 361

# Graphs in C

How do you create a basic graph in C.
I havent got a clue and just need the basics.

It must have an X & Y axis.

Thanks
0
IanWillo
• 2
1 Solution

Commented:
Are you saying a visual graph?  If so, the display functions are OS specific, so we'll need to know what you are using.  Do you have a graph subsytem available to you?

If you are simply looking for console output, you can use pipes as the vertical axis "|", and hyphen as the horizontal axis "-".

An easy way, if it is not too large, is to store the "data" in a 2-dimensional array.  Then simply traverse the array, and if there is "data" there, then print an "X", or a "*" or however you want to represent it...
0

Commented:
Here's a quick dumb was to draw the axis - I am sure you can see how you can add data...

#define GRAPHSIZE 11
int i,j;
char graph[GRAPHSIZE][GRAPHSIZE]; /*10x10 graph, w/axes*/

/* "clear" graph
memset(graph,0,GRAPHSIZE*GRAPHSIZE*sizeof(char));

for ( i=0;i<GRAPHSIZE;++i)
{
graph[GRAPHSIZE-1][i] = '-'; /* set X axis character */
graph[i][0] = '|';  /* set y axis character */
}

/* set data here, for example, if you have a point at 5,3, you would set  graph[5][3]='*', or whatever character you want to display */

/* print graph */
for ( i=0;i<GRAPHSIZE;++i)
{
for ( j=0;j<GRAPHSIZE;++j)
{
printf("%c",graph[i][j]);
}
printf("\n");
}

Obviously, if you aren't using a "square" font, you won't have an even distribution on the X and Y axes.  But that would be much more than 50 pts... <grin>

0

Commented:
be more specific .. if u are talking about something visual which can display some y=f(x)
does it have to be console based .. or r u planning to have some fancy colored stuff,. the OS u r working on .. the programming language u r comfortable with ( i assume u know C thats why u came here).. and any specific GUI library u have on mind ( GUI is more neat than console based display)

one option i left out is.. r u talking about graph-the data structures.. ( that is possible just in case u mis-heard ur assignment)
0

OwnerCommented:
Nothing has happened on this question in more than 10 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by gj62 [grade B] (is this homework? code example needs fixes).