Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5430
Number of posts: 16951

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Recursive Function help Posted by zbmay on 2 Mar 2013 at 10:54 PM
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



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.