Recursive Call Program Issues

Discussion in 'Mac Programming' started by SkippyThorson, Apr 16, 2010.

  1. SkippyThorson macrumors 65816

    SkippyThorson

    Joined:
    Jul 22, 2007
    Location:
    Utica, NY
    #1
    Next in line, we are learning about recursive functions - the function that calls itself from within its function; backtracking. It is in standard C. I appreciate all who take a look in advance.

    The code we were given to use is as follows:
    Code:
    if x <= 0.....f(x) = 0
    else............f(x) = f(x - 1) + 2
    
    My program thus far is the following:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {   
    	//Variable local to main.
    	int x, y;
    	int fun(int);
    	
    	//Input
    	printf("Welcome. \n");
    	printf("Enter x to find f(x): \n");
    	scanf("%d", x);
    	
    	//Call to the recursive function to find x.
    	y = fun(x);
    	
    	//Output.
        printf("The result is is: %d \n", y);
    	return 0;
    }
    //Function that returns the recursive value.
    int fun(int x)
    {
    	//Base case.
        if(x <= 0)
    		return 0;
    	//Recursive function.
        else
    		return fun(x - 1) + 2;
    }
    
    When I compile the program, I get the following:

    Code:
    Welcome. 
    Enter x to find f(x): 
    8
    
    [Session started at 2010-04-16 14:55:50 -0400.]
    GNU gdb 6.3.50-20050815 (Apple version gdb-966) (Tue Mar 10 02:46:18 UTC 2009)
    Copyright 2004 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "powerpc-apple-darwin".sharedlibrary apply-load-rules all
    Attaching to process 66952.
    (gdb)
    
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Exactly what problem are you seeing? Or are we supposed to guess?


    This is wrong. Given that the type of x is int, and given what scanf() requires for %d, what is the one operator you need to add to fix it? You may have to review the docs for scanf().

    If you'd used a debugger, or had printf()'ed x before the call to fun(), the consequences of this error would probably have been plain to see. Simple design principle here: confirm inputs are sane before processing.
     
  3. mdatwood macrumors 6502a

    Joined:
    Mar 14, 2010
    Location:
    Denver, CO
    #3
    What do you think the problem is? Are you getting any compiler warnings?

    Hint, your recursion looks to be working fine. Have you checked to see if the number you typed in is getting passed to your recursion function?
     
  4. SkippyThorson thread starter macrumors 65816

    SkippyThorson

    Joined:
    Jul 22, 2007
    Location:
    Utica, NY
    #4
    Oh crap - Sorry folks.

    I thought my description looked a little short -- Major apologies. I added this to the top as well. When I compile the program, I get the following:

    Code:
    Welcome. 
    Enter x to find f(x): 
    8
    
    [Session started at 2010-04-16 14:55:50 -0400.]
    GNU gdb 6.3.50-20050815 (Apple version gdb-966) (Tue Mar 10 02:46:18 UTC 2009)
    Copyright 2004 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "powerpc-apple-darwin".sharedlibrary apply-load-rules all
    Attaching to process 66952.
    (gdb)
    
    I am going back to basics and reading the beginning of my book as we speak.

    -------------------------

    Holy crap. Honestly? I forgot the "&"? I've been troubleshooting the whole rest of the problem for 3 days.

    ~3~ days...

    Code:
    scanf("%d", &x);
    
    I feel like I shouldn't show up to class again. :eek: I did that for the whole first month with semicolons. "Tyranny of the semicolons" she told me. It stuck after that. Tyranny of the everything other than the english alphabet... Thanks to the both of you for subtly pointing it out. My level of pissed-at-myself should beat that one into my brain.
     
  5. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #5
    Ahaha... pointers strike again!

    Don't worry, this sort of "stupid" error plagues us all, from newbies to seasoned professionals.
     
  6. mdatwood macrumors 6502a

    Joined:
    Mar 14, 2010
    Location:
    Denver, CO
    #6
    3 days?! You need to turn on all compiler warnings and if you can turn on "all warnings treated as errors" do that also. I copy and pasted your code straight into XCode and it immediately pointed out the error. Embrace your tools! :)
     
  7. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #7
    No it doesn't! No professional programmer I know would ever admit to making a mistake. :p
     
  8. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    You can't deny it when another programmer finds your bug. Or if you do code reviews and have to own up the stupid mistake you or someone else found and fixed. I've rarely sent an email to my group when I find a bug I introduced, but it almost always comes out (and I always know if it was me from the logs).

    -Lee
     
  9. Detrius macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #9
    See Rubber Duck Debugging. Problem solved.
     
  10. mdatwood macrumors 6502a

    Joined:
    Mar 14, 2010
    Location:
    Denver, CO
    #10
    Haha, I've been in meetings where I've admitted to mistakes that weren't mine just to get past the finger pointing and move on. Software is a complex endeavor and mistakes/bugs happen. Fix them, put a test in place so it doesn't happen again and move forward!

    The only mistakes I ever get mad at my team for are careless/lack of detail errors. Stuff like skipping a test because it was just a small change or not testing it again once it hits production. Also, making the same error over and over...I fume on that one because it shows me a) you're not learning from your mistakes and b) back to careless/lack of detail.

    /rant off :)

    Yes, I just helped a friend over IM today do just that. He told me he had a problem he couldn't figure out. I asked him to explain, and about 1/2 way through he realized the error. Truth be told I didn't even read his whole explanation because I was busy working lol, but it helped him figure out his issue.
     
  11. SkippyThorson thread starter macrumors 65816

    SkippyThorson

    Joined:
    Jul 22, 2007
    Location:
    Utica, NY
    #11
    I will be trying the rubber duck idea. It's so stupid and out there that it's brilliant, and I wish I came up with it. :)

    You know, I've never poked through the XCode settings. Every app, I've customized and fixed just the way I wanted, but haven't touched XCode - I just use it to get the job done, and that's about it. I've been afraid to mess with its power. I'll get on that one now. :)
     
  12. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #12
    More times than I care to admit, I will start to compose an email to the other members of my team saying "Hey guys, I'm trying to figure out what's wrong with ___ and I'm just not getting anywhere. I've tested X to see if ..."

    Usually after a few paragraphs, I'll figure it out, discard the email and keep plugging away... !
     
  13. Detrius macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #13
    I even bought a set of rubber duckies for my team. :D
     
  14. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #14
    At my company we've started developing with developers and QA in a barn (not literally, but that's what i call it). A lot of times you'll be huffing/swearing over something and generally because you are annoying them someone nearby will ask what's going on. This leads to a discussion, and anyone that has any ideas tends to overhear and chime in. You get the benefit of the rubber duck with the added benefit of feedback if you don't come across the solution just talking it out.

    -Lee
     

Share This Page