×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

#### Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
To Your Posts
• Keyword Search
• One-Click Access To Your
Favorite Forums
• Automated Signatures
On Your Posts
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

# Help with Recursion problem

 Forum Search FAQs Links MVPs

## Help with Recursion problem

(OP)
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[i]= slot;
if(contweight[i]<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[i] = atoi(argv[i+3]);
}

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

free(contweight);

return 0;

}

### RE: Help with Recursion problem

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
http://www.tek-tips.com/viewthread.cfm?qid=1513513&amp;page=1

>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.

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

### Reply To This Thread

#### Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!

Already a member? Close this window and log in.