Results 1 to 16 of 16

Thread: Is this possible using recursion?

  1. #1
    Sc0rp
    Guest

    Red face Is this possible using recursion?

    URGENT!
    I must hand this in tomorrow.

    We got this exercise a few days ago:
    Write a function that receives 3 parameters:
    A 4-digit integer, and two digits.
    The function checks if the two digits appear next to each other on the left part of the number or on the right part of the number, but not in the middle. The order doesn't matter.
    If the digits appear next to each other the function will print the number with the order of the digits reversed, else it will print the number without any changes.
    examples:
    function(1234, 3, 4) ---> 1243
    function(7997, 9, 7) ---> 9779
    function(1234, 2, 3) ---> 1234

    The function MUST be recursive.
    I've thought about it all day and I can't figure out how to do it using recursion.

    Help!

  2. #2
    Member
    Join Date
    Feb 2001
    Posts
    57

    Just an idea...

    Okay, this may not be what you're looking for but it's all I could think of. You could make the function that you described that accepts the three arguments and then immediately check to see if the two digits appear next to each other. If not (here's the recursion), swap the numbers of the four-digit number and call the function again. This time they should match up if there ever going to. I know it's weak, but it might work.

    Psuedocode:

    Function Header

    Are the digits next to each other?
    If no then call function again
    If yes then reverse the digits appropiately and return number


    End the Function

    Just watch out to make sure the function will not call itself forever.

  3. #3
    Sc0rp
    Guest
    That's the problem, it doesn't make any sense to use recursion in this situation.
    I thought about your idea already, but I don't think that recursion with only 2 calls is considered recursion.

    I have no idea what to do.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I'm sorry, I tried, but it just doesn't make any sense at all. Maybe you could make a recursive function to reverse the integer...

    There is absolutely no way this thing can be made recursive. If you want my opinion, your teacher must be very dumb (like the one that insists on writing void main(), or maybe a little bit more).

    You might want to post the solution here when you get it tomorrow.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    hmm, well it's not a very usefull algoritm I see, but make the function call itself, and you have made a useless one, but you have still met all the criterias on the excercise.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Something like this:
    PHP Code:
    void func(int iint jint k)
    {
      if(
    == 0)
      {
        if(
    != 0)
          
    func(00k-1);
        return;
      }
      
    func(008);  // trick recursion
      // real code here

    This will recur 9 levels deep
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I wouldn't spend much extra time on that exercise
    PHP Code:
    void func(int iint jint k)
    {
      
    // real code here

      
    func(ijk);  // blows up the stack


    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  8. #8
    Sc0rp
    Guest
    I can do something like:

    PHP Code:
    int Function(int numint dig1int dig2)
    {
        if (
    == 3)
            Function(
    num+1dig1-1dig2-1);

        
    // real code here...


  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    or maybe you could put something like
    PHP Code:
    cout << "do it again?"
    cin >> i;
    if (
    i)func(ijk); 
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10
    Sc0rp
    Guest
    I'll post the answer as soon as I get it.

    BTW, he is the kind of teacher that insists on void main().

  11. #11
    Zaei
    Guest
    Tell him hes an idiot =).

    Try this one:
    Code:
    int func(int x, int y, int z)
    {
        //code here
        return <answer>
        func(x, y, z);
    }
    =).

    Z.

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You know, I slowly get the feeling that programming teachers are those programmers that are too bad to get a real programming job...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13
    Zaei
    Guest
    Yeah. Its pretty sad. I got lucky to get a good teacher.

    Z.

  14. #14
    Sc0rp
    Guest

    Wink

    Turns out that was the solution, although I think it would have been smarter not to use recursion here:
    PHP Code:
    #include <iostream.h>

    int SwitchDigits(intintint);

    void main()
    {
        
    int numd1d2;
        
        
    cout << "Enter number and digits: ";
        
    cin >> num >> d1 >> d2;

        
    cout << "Switched: " << SwitchDigits(numd1d2) << endl;
    }

    int SwitchDigits(int numint dig1int dig2)
    {
        
    int leftright;

        if (
    num 100)
        {
            
    left SwitchDigits(num 100dig1dig2);
            
    right SwitchDigits(num 100dig1dig2);

            return (
    100 left right);
        }

        if ((
    num == 10 dig1 dig2) ||
            (
    num == 10 dig2 dig1))    
            return (
    10 * (num 10) + (num 10));
        
        else
            return 
    num;


  15. #15
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    No, definitely not. This isn't really recursion, this is two functions in one (if number > 100 it calls the other function twice, else it acts like the other function)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  16. #16
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    And, it says explicitely in the exercise that the function takes in a 4-digit integer
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width