Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Recursion problem

Status
Not open for further replies.

joeblack437

Technical User
Nov 9, 2008
1
Hello, I am experiencing a lot of difficulty in starting this program as we just learned recursion today. I am not asking for the actual solution but just a few points on how to get started, so any help would be appreciated.
The program must give all the variations on how containers can be packed into trucks.
So the user enters
% pack 3 10 5 3 8 2
meaning that there are 3 trucks, each can hold 10 weights and then 5 3 8 2 are weights of different containers. our program must give all the solutions to the problem.
for ex: 2311 - 10 5 3
so container 2 is packed into tuck one, container 3 is packed into truck 2, and container 3 and 4 packed into container 1, giving us a total of 10 in truck one, 5 in 2 and 3 in 1.

this is what i came up with so far but its obviously wrong. can anyone please give me some hints or pointers?

- Thanks in advance





#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void pack(int truck, int weight, int slot, int *list, int *contweight,int numcont)
{
int i, j ;
for(i=0; i<numcont;i++){
list= slot;
if(contweight<weight)
pack(numcont,weight,slot+1,list);
else{
for(j=0;j<numcont;j++)
printf("%d", list[j]);
printf("\n");
}
}
}






int main(int argc, char *argv[])
{
int n, i;
int truck= atoi(argv[1]);

int weight=atoi(argv[2]);
int numcont = argc-3;
int *contweight = (int*)malloc(numcont*sizeof(int)) ;

for(i=0;i<numcont;i++){
contweight = atoi(argv[i+3]);
}

pack(truck, weight, 0, list, contweight,numcont);


free(contweight);

return 0;


}
 
For example, we have a list of packages/containers
packages = [5, 3, 8, 2, 1, 2, 3, 5, 2]
i.e.
1. package with weight 5
2. package with weight 3
..etc

and 3 trucks with the same max capacity=10

For Loading one truck in recursive procedure:
1. Try to load first package from the list
2. and then try to load the rest of packages.

For loading all packages on the 3 trucks:
1. load first truck
2. then load second truck
3. at end load last truck

For describing the solution I rather used pseudocode i.e. Python :), therefore I posted the procedure in Python-thread


>The program must give all the variations on how containers can be packed into trucks.
To do this I would generate all permutations of given containers in a loop and apply the procedure on every permutation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top