reading a sequence

This is a discussion on reading a sequence within the C Programming forums, part of the General Programming Boards category; Hi: What would be the best way to read a sequence of numbers that are separated by white spaces? EX: ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    35

    reading a sequence

    Hi:
    What would be the best way to read a sequence of numbers that are separated by white spaces?
    EX: "10 20 30 -1"
    The -1 will be used to mark the end of the sequence.
    I will use the numbers in the sequence to search through a 2D array full of numbers for where the sequence entered starts and ends (must be in a straight line). Or whether the sequence exists at all...
    What would be the best way to do something like this?
    Take all the values at once and put it into an array? (How would I go through the input and get the number of elements for the array?)
    Or do I work with one value at a time somehow?


    Thanks

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Probably the easiest way to "read" them would be by using an I/O function that works with blank delimited input, in a loop.

    Or, you could read a "line" at a time and parse it yourself.

    You can convert the numeric strings to integers, or not, depending on what you need to do with them.

    Vague question... vague answer.

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    Do you know the maximum numbers that are in the sequence. If you do use an array and a loop.
    Also, is this being read from keyboard input or file input?

    -Dustin
    C tutorials

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Quote Originally Posted by Todd Burch View Post
    Probably the easiest way to "read" them would be by using an I/O function that works with blank delimited input, in a loop.
    How would I do that? Could you maybe show me an example?
    We are currently learning about pointers; does something like this require the use of pointers possibly?
    I am basically taking the sequence of numbers and seeing if they exist and where they start and end in a map of numbers
    ex:
    10 20 30 40 50
    99 32 41 42 53
    34 51 62 75 85
    24 4 42 24 3

    If the user enters (10,20,30) I would have to output
    Starts: 0,0 Ends: 0,2

    Any ideas for the best way to achieve this?

  5. #5
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Look at the tutorial on File I/O here on the website. I just had to use it for one my projects, and it was extremely easy to understand and implement for my purposes. I bet you'll have the same luck if you read it carefully.

    Put those numbers into a text file, read in a character at a time. Is it a space? Is it a number? Is it a return character? Process accordingly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bus error when reading input from file
    By gkoenig in forum C Programming
    Replies: 6
    Last Post: 03-29-2008, 04:05 PM
  2. Replies: 2
    Last Post: 01-28-2008, 03:07 AM
  3. help with reading from stream
    By movl0x1 in forum C Programming
    Replies: 7
    Last Post: 05-31-2007, 11:36 PM
  4. Fun with reading hex
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 02-17-2006, 06:41 PM
  5. problems reading data into an array and printing output
    By serino78 in forum C Programming
    Replies: 4
    Last Post: 04-28-2003, 09:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21