# Recursion

• 11-09-2008
jordan_230
Recursion
Using recursion to pack trucks..
The input will be
pack T W W1 W2 W3
T being the max trucks
W being the max weight
W1..Wn are the containers and their respective weights.

For example if my input was
pack 3 10 5 3 8 2

I should output
2311 - 10 5 3

The columns of the first number 2311 (before the -) indicate the truck assignment of the
4 containers in the solution.
since column 2 is 3, container #2 is packed into truck #3. n so on..

I have to use Recursion and no Multi-Dimensional Arrays
Also use command line arguements like argc and argv

//This should take care of the main variables.
Code:

```int truck_num=atoi(argv[1]); int max_load=atoi(argv[2]); int _contain[argc-2], max_weight = 0; int i = 0, count = 0; for (i = 3; i < argc; i++) {         _contain[count] = atoi(argv[i]);         max_weight += _contain[count];                count++; }```
I have sort of thought of the algorithm, but cant put it into code..

base case: This will stop the recursive function. When you have no containers left to pack, you know that this arrangement of containers in the trucks will work.

The recursive solution: You must try to pack one container that you haven't packed into any available truck. If you can't do it, then you must stop and return false. If you can, then you just recursively call your function, except remove the container that you just packed, and have it try to pack the remaining containers into the trucks.