Thread: recursive ?

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

    Question recursive ?

    Given this snippet i know it sets the first element of the array A to zero but what about the rest of the arrays dont they increase while the size decrements untilz size is 0?

    Code:
    void Mystery (int A[ ], int size)
    {
          if (size > 0){A[0] = 0; Mystery (A+1, size -1);}
    }

  2. #2
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    a[0] will indeed only set the first element. You need either a variable in the brackets or some other numbers as well (and sometimes even some math inside like "ImageData[Row*TotalColumns+Column] = 5;").
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    It's just setting each element in the array to 0. Trace it all on paper.

    Assuming the call is something like this:

    Code:
    int array[10];
    
    ...
    
    Mystery(array,10);
    First few iterations you'll have this:

    Code:
    Mystery is called: A points at array[0], size = 10
    (size > 0) is true.
    A[0] = 0;  This means array[0] = 0 in main()
    Mystery is then called with A + 1 and size - 1.
    Code:
    Mystery is called: A points to array[1], size = 9
    (size > 0) is true
    A[0] = 0;  array[1] = 0 in main()
    Mystery is then called with A + 1 and size - 1
    Code:
    Mystery is called: A points to array[2], size = 8
    (size > 0) is true
    A[0] = 0; array[2] = 0 in main()
    Mystery is then called with A + 1 and size - 1
    A is just really a pointer. It's not really an array, although it can behave just like one in many ways. It's important to realize that just because A[0] is set to 0, doesn't mean that array[0] is what will be changed.

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    27
    Thanks you guys shood be c professors your make me understand better than my instructors

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Maybe they are C professors?

    Also if your going to post 3 topics simultaneously, consider one posting them in one topic?

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

    Question reply

    Quote Originally Posted by zacs7 View Post
    Maybe they are C professors?

    Also if your going to post 3 topics simultaneously, consider one posting them in one topic?
    easy does it I didnt mean to make u upset

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    http://cboard.cprogramming.com/showthread.php?t=89984
    http://cboard.cprogramming.com/showthread.php?t=89976
    http://cboard.cprogramming.com/showthread.php?t=89980
    Considering that they were all basically the same question, once you actually understood recursion from one thread, figuring out the answers to all the others would have been a lot easier.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursive function
    By technosavvy in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 05:42 AM
  2. difference between recursive and iterative
    By Micko in forum C Programming
    Replies: 33
    Last Post: 07-06-2004, 09:34 PM
  3. Algorithm help (Changing from Recursive to Non Recursive)
    By Thantos in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2004, 07:27 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM