# reading a sequence

• 03-17-2008
drag0n69
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
• 03-17-2008
Dino
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
• 03-17-2008
broncoslb
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
• 03-17-2008
drag0n69
Quote:

Originally Posted by Todd Burch
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?
• 03-17-2008
JDGATX
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.