Reading array for Bubble in Array problem (23)

Back to General discussions forum

lose311     2015-12-15 20:46:18
User avatar

(I'm using Java)

So I have encountered similar problems before, and I feel like my solution seems a bit clumsy. For the first step of scanning the input into an array, what's the best way to do this if you don't know how many numbers there are going to be? My first instinct is to just create an array of size 100 to scan into, then mark the index of the -1 so I can copy the scanned values to a new array of the correct size after everything is scanned. I also realize you can do it better with a linked list, but that's a little advanced for me at the moment. Is there a way to scan the input once to count how many values, then scan it again to read it into the array? Maybe scan it all as a string then parse later?

Any thoughts?

Goodwin Lu     2015-12-15 23:05:27

hmmm... for the scanning thing I did this function (I'm c++): int GetArray (int array[], sizet arraysize) { int i=0, num =0; do {cin >> array[i]; int num = array[i]; i++; } while (num!=-1); return i; }

I think there MUST be another way to tell the end of the line, because April Fool's problem expects us to know where the heck the end of the line is, with no indication (it seems to be 6 to 2 numbers, but the pattern of numbers don't seem to hint at just how much numbers are there except that the numbers were increasing, so it can be concluded that there MUST be a function allowing us to see until the end of a line. "getline" in c++, was it?

lose311     2015-12-17 01:52:46
User avatar

Yeah getline in C++ or or Scanner.nextLine() in Java would read the whole line into a string and then you can parse it into an array from there.

Please login and solve 5 problems to be able to post at forum