We have got a text file which contains 10 million integers in a sorted order. Need to write a program in C to reverse these numbers and write it to a separate file. Which data structure to use and how to copy such a huge data in memory? And for reversing shall i use fseek or store in some data structure and then reverse?
Please help.
We have got a text file which contains 10 million integers in a sorted
Share
If you want to run this on a basic desktop system, no special data structure other than a (dynamic) array is needed.
Just allocate space for e.g. 1,000 numbers initially, startl loading, and double the allocation size when it runs out. This will grow the array like so:
64,000 -> 128,000 -> 256,000 -> 512,000 -> 1,024,000
so it will do 10 calls to
realloc()which really should be fine.Once you have the numbers in an array, just loop over it backwards and print each number to the output file.
Of course, you can use the fact that you know that there’s a million numbers to your advantage, and set the initial size appropriately.