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
#include <stdio.h>
#include <stdlib.h>
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\n");
printf("This program will give all the possible combinations the \n robot can walk \n");
do{
printf("\n How far would you like the robot to walk? ");
scanf("%d",&d);
if (d<1)
printf(" \n A robot can not walk a negitive distance \n");
else{
count = walk(d);
printf("The robot can walk %d meters in %d ways. \n",d,count);
combination(1,d);
getchar();
getchar();
printf("\nWould 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);
}
}
But i am having a bunch of trouble with the recursive function that shows the different combinations