Okay so the job of this program is to have a robot take steps of 1,2,3 meter in a amount of steps asked by the user. After the user inputs a value, the program will tell them that the robot can walk "user input" meter in a certain amount of ways.
Example:
If the user enters "4"
The program enters 4 meters and uses a recursive function to print how many steps it will take.
To Travel 4 meters, a robot has 7 ways to do it
This is the part I have down right now
The other part is to display all of the possibilities. So after that is printed
1 1 1 1
2 1 1
1 2 1
1 1 2
2 2
1 3
3 1
would print.
How do I go about printing this part of the code?
Code:#include <stdio.h> #include <stdlib.h> int walk(int distance); main() { int d,count; 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"); 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); getchar(); getchar(); } } 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); }



LinkBack URL
About LinkBacks


