Ugly recursion

This is a discussion on Ugly recursion within the C Programming forums, part of the General Programming Boards category; I think i have a bad function Code: void fill_me(int A[ ], int N) { if (N != 0) /* ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    27

    Question Ugly recursion

    I think i have a bad function
    Code:
    void fill_me(int A[ ], int N)
    {
        if (N != 0)   /* if N is not equal to zero do*/
            {
                --N;  /*decrease N*/
                A[N] = N;/*  this attempts to put N in A[N] which is wrong.. am I correct*/
                fill_me(A,N);
             }
    }
    does this result in an infinite loop or only with some values of N

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by matthughes View Post
    I think i have a bad function
    Code:
    void fill_me(int A[ ], int N)
    {
        if (N != 0)   /* if N is not equal to zero do*/
            {
                --N;  /*decrease N*/
                A[N] = N;/*  this attempts to put N in A[N] which is wrong.. am I correct*/
                fill_me(A,N);
             }
    }


    does this result in an infinite loop or only with some values of N
    Your function could be used to make an index array, where a[n] = n, throughout the array.

    There's no reason why you can't assign a[N] to the value of N - perfectly OK.

    Let me boot up here and check this out. It's not clear why it would loop indefinitely at first glance.

    Works just fine. No, I didn't test it for negative numbers!

    N > 0 would be an improvement, and very standard usage, imo.
    Last edited by Adak; 05-17-2007 at 07:54 PM.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    It only results in an infinite loop for some values of N: when N is negative. Well, it wouldn't be an infinite loop, but it would certainly execute for a very long time, trashing your memory in the process.

    I would either change your expression from N!=0 to N>0 or pass N as an unsigned int to the function -- which could be awkward unless you make A[] unsigned as well. Best just to change your expression.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    27
    I get it I am so stupid if negative number is not equal to 0 and the--N just keeps decreasing it keepin it from ever equaling zero wow!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 08:45 PM
  2. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 09:56 AM
  3. stack and recursion help needed!
    By LouB in forum C++ Programming
    Replies: 3
    Last Post: 07-01-2002, 02:19 PM
  4. To Recur(sion) or to Iterate?That is the question
    By jasrajva in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 08:24 AM
  5. selection sorting using going-down and going-up recursion
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 01:29 PM

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