Page 1 of 1

## 1 Replies - 4962 Views - Last Post: 27 March 2007 - 11:19 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=25671&amp;s=e6f6b268264cbaae1ff7f1840826910f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ProfessorWC

Reputation: 0
• Posts: 21
• Joined: 04-March 07

Posted 27 March 2007 - 11:05 PM

I am reading an undisclosed number of integers from stdin, and placing them into an array. Then I have to copy the array twice and sort it using different sorting algorithms. Pretty much all I need is to figure out how to get the integers out of stdin into the array.

the program is executed using the line:

./a.out < input.txt

any ideas?
Is This A Good Question/Topic? 0

## Replies To: Reading integers from stdin

### #2 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: Reading integers from stdin

Posted 27 March 2007 - 11:19 PM

Well the problem is the "undisplosed number" isn't it. Since I don't know what language you are in I will sort of set up two answers
[C]
I would set up a structure that holds a pointer of type (int *) and an integer to hold the size. Then I would use malloc() to create an array of ruffly what I thought might be needed... say 1000. Should the array get grater than 1000, I would recall malloc() adding in another big chunk, copy the current array into the new one (using memcpy() not a cell for cell copy). Then release the old array ussing free().

--Option 2, use a linked list rather than an array

[C++]Much like the above example except I would create a class, two private members, a pointer and an integer, and I would us the new statment to create the inital array, and then I would use memcpy() to again copy the data to a new array if I needed it. delete() the old array.

-- or use a liked list.