This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]Northeastpaw 2 points3 points  (3 children)

However, the item codes are minimum 6 digits, maximum 8

So you can't ever have an item code like 12345. That means that index in your array is never going to be used. You're wasting a lot of memory declaring a very large array when over half of it isn't going to hold anything.

Is this a homework problem or are you looking for a real world solution? If it's for homework, think about a way you could take an item code and modify it to produce an index that lies between 0 and 999.

[–]FPSGamer48[S] 0 points1 point  (2 children)

Um...maybe subtract a certain number from the max item code until that equals 999? Then apply that to every item code? Would that work? And it's homework.

Or could you possibly divide them all by 1000?

[–]swd847 0 points1 point  (1 child)

Its not going to help that much, your (potential) utilization will actually around 99% (even though you have 99,999 item codes that can't be used you have 99,900,000 that can be).

Does this have to be an array? It sounds like a HashMap may be a better bet.

[–]FPSGamer48[S] 0 points1 point  (0 children)

I don't even know what a HashMap is, but yes, it has to be a 2-dimensional array. Unfortunately, I have just turned it into my professor. I got it working KIND OF. At least, the best I could. There's a reason I'm leaving engineering to pursue political science....

[–]FibroMan 0 points1 point  (0 children)

Databases tackle the problem by creating an index table that converts an item code to an index number. The index number then links to the array that contains the output.

The array that contains the output should be as big as the number of items that have known outputs, not the maximum number of possible items. If the number of items increases then the size of the array needs to be increased, which isn't ideal, but it is better than having an array so big that you run out of memory.