1 Replies - 4980 Views - Last Post: 27 March 2007 - 11:19 PM Rate Topic: -----

#1 ProfessorWC   User is offline

  • New D.I.C Head

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

Reading integers from stdin

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   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • 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
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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1