public void autoFill()
{
int total = 50;
int contents = 0;
for (int p = 0; p < 10; p = p + 1)
{
if (nums[p] > 0)
{ if ( nums[p] <= total)
{ total = total - nums[p];
contents = contents + nums[p];
output("Taking # " + p + " which is " + nums[p] + " kg");
nums[p] = 0;
showNums();
}
}
}
output("Weight of this box = " + contents);
}
The code above - autoFill() - is a first attempt at programming the computer
to
automatically
pack 50 kg boxes (assumes the random weights are still under
50 kg).
(1) Start with an array containing {39 , 5 , 24 , 26 , 11
, 23 , 28 , 15 , 20 , 21 }
Trace the
algorithm. You probably don't want to make a "complete" trace-table,
but you do need to keep track of the nums[] array as it changes -
do
that on paper. Then fill in the items that go in each box:
First box = _____________________
Second box = _____________________
Third box = ______________________
Fourth box = _____________________
Fifth box = ______________________
Finished?
(2) Now you take the same set of numbers and see if you can do a better job,
using fewer boxes than the computer. Take
your time - this is a non-trivial problem.
(3) MAKE UP a set of data where the computer does a LOT WORSE than you can do by hand.
(4) Write PSEUDO-CODE to describe what your brain is doing when you look for a "best" solution.
(5) Describe why the computer algorithm is NOT OPTIMAL.
(6) Discuss your answers with the rest of the class.