Thread: C graph

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    30

    C graph

    Hi,

    I was wondering if anyone could tell me how I could write a program where there is a number scale and a certain number when read from the input will represent it in accordingly to the scale. any help will be appreciated. thanks

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    @Nasser - welcome to the forum!

    Say I want the input to fit into a graph with 0-100 for it's range. My data numbers are ranging from 0 to 10,000.

    Take 10,000, and divide it down, until it is 100, or just barely less than that.

    Since 10000 / 100 = 100, all my data numbers need to be divided by 100, to scale properly.

    This worked out "perfectly" with all integers, but in real life, you may want to use a float or double, and "walk" it down in a loop:

    Code:
    float factor = 1.00, highest_data, graph_scale = 100;
    while((highest_data /factor) > graph_scale) {
       factor += .01;
    }
    The above is not meant to be a runnable bit of code. Just an idea, expressed in code form.
    Last edited by Adak; 03-26-2011 at 03:50 AM.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    30
    Sorry, I don't think you understood me. what I mean to say is, say:

    Input = 3.

    And then output would be:

    1---2---3----4----5----6
    (blank )xxxxx

    (Everything before "x" is blank)

    I'm assuming the "1---2---3----4----5----6" would be printed using printf() but I'm not sure how to locate the "3" and print "x" on the graph.

    Thanks

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When I'm making a graph, I like to first, put the whole thing into an array.

    Then just print up the array. That helps because I can jump around in the rows and columns of the array, much easier than I can when printing it on the screen.

    The answer to your question is to multiply 2 * the width of each "column" of the graph, +1. If the column has space for a 2 digit number, and then 4 spaces after it, that's 6 spaces total, so three's column begins at the 13th space over from the left margin. (the left margin of the graph will not be the left margin of the screen most likely, so add that value in, as well.)

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    30
    I'm sorry I'm have trouble understanding what you explained. What are the "columns"? Could you explain it in a different way?

    Thanks

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'll try with a visual:

    Code:
    1----2----3----4----5----6
    xxxxxxxxxxx
    ...............x
    The first row shows a bar graph style, with value equal to 3.0. The second row shows a point graph, where the value equals 4.0

    From the left side, until it reaches the 2, is the first column, from the 2 until it reaches the 3, is column two, etc.

    To put a mark at 6, I'd go to (width of column) * 5 +1.
    Last edited by Adak; 03-26-2011 at 06:20 AM.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nasser View Post
    Sorry, I don't think you understood me. what I mean to say is, say:

    Input = 3.

    And then output would be:

    1---2---3----4----5----6
    (blank )xxxxx

    (Everything before "x" is blank)

    I'm assuming the "1---2---3----4----5----6" would be printed using printf() but I'm not sure how to locate the "3" and print "x" on the graph.

    Thanks
    That's actually quite easy... If you use a known number of dashes between your numbers... Say, the number and 3 dashes... then you have a number every 4th space... So to put an X under the 3, you need to move over 8 spaces (2 sets of 4)... Just print the required number of spaces followed by an X...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Graph implementation
    By tcoffee in forum C Programming
    Replies: 10
    Last Post: 11-16-2010, 09:52 AM
  2. C++ graph ploting
    By ivpavic in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2010, 12:06 PM
  3. error help making no sense
    By tunerfreak in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2007, 07:55 PM
  4. determining a path through the graph
    By Mist in forum C Programming
    Replies: 2
    Last Post: 02-27-2005, 12:21 PM
  5. Minimize crossing edges in undirected graph
    By Shiro in forum C Programming
    Replies: 0
    Last Post: 12-26-2001, 04:48 AM