Thread: Ideas Welcome!

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    25

    Ideas Welcome!

    Hey guys, first time poster!

    I had a quick question for the masses about a program I am writing: I need to take the input from the user at the keyboard that contains all integers, and then store each integer into an array index.

    Example:

    User inputs: "10 5 6 7 8 111 2 3"

    And I would like to have that turned into an array of
    Code:
    ({ 10, 5, 6, 7, 8, 111, 2, 3 })
    What is the best way of going about doing this? The number of numbers input by the user is unknown, so I can't do a scanf with a predetermined number of %d's.

    Thanks in advance!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you don't want a predetermined number of entries - but how will you know how many entries there are? Is it always a single line of numbers? If it's multiple lines, then you DO need some sort of indication that the sequence is finished.

    Otherwise, I don't see any problem with it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Use fgets() to read a whole line.
    Use strtol() to convert a single int, and also tell you where to start looking for the next int.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    25
    Yes, it is always a single line of numbers input by the user.

    I just need the line to be separated into an array of ints with each index at one. I will try what Salem mentioned, thanks!

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by TitoMB345 View Post
    Yes, it is always a single line of numbers input by the user.

    I just need the line to be separated into an array of ints with each index at one. I will try what Salem mentioned, thanks!
    That sounds like the sort of solution I would have suggested, but I wanted to clarify the problem first.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    25
    Quote Originally Posted by Salem View Post
    Use fgets() to read a whole line.
    Use strtol() to convert a single int, and also tell you where to start looking for the next int.
    Hmm, okay, so I have strtol() convert the first appearance of an integer into a long... how do I go on from there? How do I tell the program to start looking again passed the integer? The pointer I set in the second argument probably has something to do with it..

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    25
    Okay, so my latest thought would be somehow using recursion to keep looking for ints until the input is over... hmm.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by TitoMB345 View Post
    Okay, so my latest thought would be somehow using recursion to keep looking for ints until the input is over... hmm.
    Recursion isn't needed to solve the problem, and as far as I can see, it's not really going to simplify the problem particularly over a while-loop of some sort.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    25
    Actually, I ended up using Recursion to fix it!

    Code:
    void sepArrays(char input[], int array[], int *pos) {
       long num = 0;
       char *p;
    
       num = strtol(input, &p, 10);
       if (num != -2) {
          if (
          array[*pos] = num;
          *pos = *pos + 1;
          sepArrays(p, array, pos);
       }
    }
    with the initial call to the function being
    Code:
       sepArrays(input, array, &total);
    That solved the problem, and gave me exactly what I needed.


    Now the question is though.. is there a more efficient way to do it?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    How about this:
    Code:
    int sepArrays(char input[], int array[]) {
       long num = 0;
       char *p = input;
       int pos = 0;
    
       do {
         num = strtol(p, &p, 10);
         if (num != -2) {
            array[pos] = num;
            pos++;
       } while(num > 0);
       return pos;
    }
    ---
    total = sepArrays(input, array);
    It should be faster than recursion, it's a line or so shorter in the main code (there's more lines overall, but that's because of the extra variable declaration for pos as a local variable instead of parameter), and I don't see any problem with it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Um, and exactly how does it solve the problem that you don't know how large array needs to be?

    And another question: why do you think pos needs to be a pointer?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Anon, that's a good point. I would propose a "guestimate" solution, e.g. select a "should be large enough" array-size, such as 100 or 1000. Just to make it safe, one may want to pass this into sepArray and bail out if the array is filled to the end, rather than overwriting some random other data with more numbers.

    There are of course other potential solutions, such as counting the number of integers in the line first. [I have another variation on this that is "really clever", but I'll let that be for a few minutes to see what everyone else can come up with]

    [By the way, if we use fgets, how do we know how large to make the input-buffer in the first place? If we assume that this is a hard limit, then the number of integers can not be more than half the length of this string, since each number is at least one digit with one separator character].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Sep 2007
    Posts
    25
    Hey guys, thanks again for the help.

    You are right, the assignment says to assume there would be no more than 1000 ints entered. So if they enter 1000 ints, the character array would overflow because each character would be 1 space.. I'm not sure how to work around that.

    And just to clear the air, there is way more to this assignment than just this part, which I have already flowcharted out, so please don't think I am trying to cheat. I just got stuck at how to come up with the ints into an array.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by TitoMB345 View Post
    Hey guys, thanks again for the help.

    You are right, the assignment says to assume there would be no more than 1000 ints entered. So if they enter 1000 ints, the character array would overflow because each character would be 1 space.. I'm not sure how to work around that.
    So you need to make sure that your input character array is, 4-16000 characters - this depends on what range you reccon the ints to be in - 4 chars for each int covers numers averaging around 50-99, 16 covers "anything". And in a modern machine, a buffer of 16K isn't really that big.

    And just to clear the air, there is way more to this assignment than just this part, which I have already flowcharted out, so please don't think I am trying to cheat. I just got stuck at how to come up with the ints into an array.
    I think this is fine - we don't do other peoples homework, but giving hints and improving on some existing code is a different thing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ideas, how do you get them? xD
    By Akkernight in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-22-2009, 03:53 AM
  2. cool ideas for a game
    By Shadow12345 in forum Game Programming
    Replies: 7
    Last Post: 05-18-2004, 08:37 PM
  3. idea's idea's idea's
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-29-2002, 12:30 AM
  4. Small app ideas
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 02-15-2002, 08:57 PM