# Using recursive functions (I am beginner at C programming)

• 03-02-2013
Zachary May
Using recursive functions (I am beginner at C programming)
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); }```
• 03-02-2013
Salem
What happens if you write it out backwards, to see the pattern you would get if called recursively

3 1
2 2
2 1 1
1 3
1 2 1
1 1 2
1 1 1 1

On each recursive call, you're decrementing the number in column n, and incrementing the number in the column to the right.
When you end up with a 1 in the right most column, you can go no further, the recursion ends and backtracks up to a previous column that can be incremented.
• 03-02-2013
Zachary May
Okay, I am understand how to go about the solution, it is clearer to see with the answers being backwards. I am going to write the program part of this later on and i will respond back if I have trouble (brain is fried)

Thank you
• 03-02-2013
jwroblewski44
*Noting this exercise for me to practice recursive functions.
• 03-03-2013
Zachary May
I am having a terrible time figuring out how to start this.
Code:

```int combination(int a,int count) {         if (count <=1){                 printf("%d",a);                 return a;         combination(a+1,count-1);         printf("%d",a);         } }```
• 03-03-2013
Salem
Code:

``` combination(int a[],int pos) {   ....   combination(a,pos+1);   ...   // use a for loop to print from a[0] to a[pos] } int a[4] = { 4, 0, 0, 0 }; combination(a,0);```
What you decide is when to recurse, and when to print.
• 03-04-2013
Zachary May
I don't have any experience in arrays yet for this project so I can't use this.

Code:

```int combination(int a,int pos,int count) {         int d;         if (count>0)         {                         for (d=pos;pos!=0;--pos){                         printf("%d",a);             }             printf("\n");             pos=d;             a++;             count--;             combination(a,pos,count);         } }```
Okay, so I got my code to run
1111
2222
3333
4444
where count is the number of combinations in main and pos is the number entered by the user. Now how do I change what i have for
1111
2222
3333
4444
5555
6666
7777
to the actual combinations
1 1 1 1
2 1 1 0
1 2 1 0
1 1 2 0
2 2 0 0
1 3 0 0
3 1 0 0
I don't mind if my program has the zeros in it.
Or am I going the completely wrong direction when it comes to this.
• 03-04-2013
Salem
Well the problem comes when you get to something like this
1 1 2
1 1 1 1

Somehow, you need to output the common prefix to multiple permutations.

If you can't store these in an array (how about a string - they're arrays, can you use them?), it's going to be harder to do.
• 03-04-2013
Zachary May
Nope. We are learning Strings and Arrays right now, but for the purpose of this exercise, we are not allowed to use them. And that is what im having the main problem with. If I could use arrays I kinda (not a strong understanding of array and strings yet) see how the program would work. But I can't use them