Thread: reading a sequence

  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
    Chappell Hill, Texas
    Posts
    2,332
    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
    Mainframe assembler programmer by trade. C coder when I can.

  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, 03: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, 10: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, 08:39 AM