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;
}
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;
}