?
Solved

Graphs in C

Posted on 2003-03-05
5
Medium Priority
?
354 Views
Last Modified: 2010-04-15
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
Comment
Question by:IanWillo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 6

Expert Comment

by:gj62
ID: 8074175
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?

Please provide more information...

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
 
LVL 6

Accepted Solution

by:
gj62 earned 150 total points
ID: 8074288
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 8076248
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
 
LVL 20

Expert Comment

by:jmcg
ID: 10089283
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).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question