Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Recursive Function help

zbmayzbmay Posts: 2Member
So I need help with my recursive function.
I have to do the following. Allow the user to input a number. After that the program is suppose to run in order to show how many different combinations a machine could walk a certain distance. The machine is allowed to walk 1,2,3 meters in order to get to the distance inputted by the user. So the machine 4 meters.
Output the following:

The robot can walk 4 meters in 7 ways
1 1 1 1
2 1 1
1 2 1
1 1 2
2 2
1 3
3 1

My code so far is
[code]
#include
#include
int walk(int distance);
int combination (int a,int count);
main()
{
int d,count,answer;
printf("A robot can walk 1,2 or 3 meter at a time
");
printf("This program will give all the possible combinations the
robot can walk
");
do{
printf("
How far would you like the robot to walk? ");
scanf("%d",&d);
if (d<1)
printf("
A robot can not walk a negitive distance
");
else{
count = walk(d);
printf("The robot can walk %d meters in %d ways.
",d,count);
combination(1,d);
getchar();
getchar();
printf("
Would you like to run program again? (0=No 1=Yes)");
scanf("%d",&answer);
}
}while(answer);
}
int walk(int distance)
{
switch (distance){
case 1:
return 1;
case 2:
return 2;
case 3:
return 4;
}
return walk(distance-1)+walk(distance-2)+walk(distance-3);
}
int combination(int a,int count)
{
if (count <=1){
printf("%d",a);
return a;
combination(a+1,count-1);
printf("%d",a);
}
}
[/code]
But i am having a bunch of trouble with the recursive function that shows the different combinations
Sign In or Register to comment.