robot step sizes

This is a discussion on robot step sizes within the C Programming forums, part of the General Programming Boards category; hi again. i have a question on how to get my program to work. my program is to see how ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    19

    robot step sizes

    hi again. i have a question on how to get my program to work. my program is to see how many steps a robot can take to get to a certain distance. the user inputs 3 step sizes and the distance. then, my program states how many possible ways the robot can travel the distance. for example, distance is 7. step sizes are 2,3,5. there are 5 possible ways to cover the distance (2+2+3,2+3+2,3+2+2,2+5,5+2).
    now my question is how can i fix my recursive functions to work. how can i get the values of the step sizes into the recursive function. here is the code...

    Code:
    #include <stdio.h>
    
    int steps(int dist);
    
    main()
    {
      int a,b,c,d,e;
    
      do
        {
          printf("Input small step: ");
          scanf("%d", &a);
          printf("Input medium step: ");
          scanf("%d", &b);
          printf("Input large step: ");
          scanf("%d", &c);
    
          if(a<=0||b<=0||c<=0||a>=b||a>=c||b>=c)
            printf("\nIllegal input! Please enter value again!\n\n");
        }
      while (a>=b||a>=c||b>=c||a<=0||b<=0||c<=0);
    
    
      do
        {
          printf("Input total distance to travel: ");
          scanf("%d", &d);
        }
      while (d<=0);
    
      printf("\nNumber of ways the robot can make the trip: %d\n", steps(d));
    }
    
    int steps(int dist)
    {
      if(dist <= 2)
        return dist;
      return steps(dist-a) + steps(dist-b) + steps(dist-c);
    }

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    9
    that's a huge if statement. Try using switch()

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    nothing wrong with that if statement.
    Code:
      do
        {
          printf("Input total distance to travel: ");
          scanf("%d", &d);
        }
      while (d<=0);
    you rewrite over d until the last time that is what will mess you up. Once you fix that you could go through the array of d's until '\0'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! Robot Programming
    By Gizzle in forum C Programming
    Replies: 23
    Last Post: 09-28-2006, 07:32 AM
  2. Help with moving robot in a maze
    By Killahkode in forum C Programming
    Replies: 2
    Last Post: 09-25-2006, 10:51 AM
  3. recursion
    By paulmedic555 in forum C Programming
    Replies: 26
    Last Post: 01-27-2005, 11:43 PM
  4. this sites Compiler Resources Specs.
    By Powerfull Army in forum C++ Programming
    Replies: 9
    Last Post: 07-08-2002, 06:12 PM
  5. Replies: 5
    Last Post: 09-08-2001, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21