Thread: Help printing values in the correct order

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    1

    Help printing values in the correct order

    Hi! I have the following data:

    Code:
    one    1  0
    two    2  0
    three  3  1
    four   4  1
    five   5  1
    six    6  4
    seven  7  1
    eight  8  4
    nine   9  1
    ten   10  8
    and I need to print these values out like:

    Code:
    one
     - three
     - four
       - six
       - eight
         - ten
     - five
     - seven
     - nine
    two
    The names are ordered by their second integer based on the value of the first. Names with a 1 as the second integer will go under the name with 1 as its first integer, and so on.

    What is the best way of doing this in C? I was thinking a hash, but I don't know if it's possible to create a hash in C (I'm used to Python where hashes are very easy to use).

    Any help would be appreciated.

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Since the keyspace seems to be purely numerical (and low numbered) you can use either switch() or lookup map (an array of size [MAX_KEYNUM]). Each element of the array should be a string you can modify/append to.

    Then as you read numbers in, each second digit becomes your lookup value into the array above (just like you were probably thinking you'd use the hash). They clearly intended for you to do this because the numbers are sequential and start at 0, perfect for array indexing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Correct/best way to return values from a function?
    By jim_0 in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2014, 11:42 AM
  2. Replies: 2
    Last Post: 07-21-2013, 10:35 PM
  3. Replies: 1
    Last Post: 11-17-2012, 05:34 PM
  4. seed rand not correct values
    By bloodshot772 in forum C Programming
    Replies: 24
    Last Post: 09-29-2010, 02:13 PM
  5. correct order to insert new node?
    By campermama in forum C++ Programming
    Replies: 1
    Last Post: 06-16-2004, 07:51 PM