GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 31-Jul-2006, 08:42
jonas12rena jonas12rena is offline
New Member
 
Join Date: Jul 2006
Posts: 6
jonas12rena is on a distinguished road

Need Help With Recursive Functions


Here is my code:

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

I am having trouble figuring out what this code does. With my limited knowledge of reading code(correct me if I am wrong) If the integer size is larger than zero the first element of A will be set to zero, then recursion calls the Clueless function again and sets the pointer to first element to -1(isnt this the final number in the array?) So will the recursive function set just the first element to zero, or will it set the last element to zero, or will all elements be set to zero, or finally wil it create a infinite loop?
Last edited by LuciWiz : 31-Jul-2006 at 10:17. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 31-Jul-2006, 11:12
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Need Help With Recursive Functions


Quote:
Originally Posted by jonas12rena
Here is my code: (reformatted for readability -- wp)
CPP / C++ / C Code:
void Clueless (int A[], int size)
{
    if (size > 0)
    {
        A[0] = 0; 
        Clueless (A+1, size -1);
    }
}

I am having trouble figuring out what this code does. With my limited knowledge of reading code(correct me if I am wrong) If the integer size is larger than zero the first element of A will be set to zero, then recursion calls the Clueless function again and sets the pointer to first element to -1(isnt this the final number in the array?) So will the recursive function set just the first element to zero, or will it set the last element to zero, or will all elements be set to zero, or finally wil it create a infinite loop?
Close.

This function zeros an array of size elements.

In effect:
Code:
As long as size is not zero Zero the first element of the array Call the function again with address of next element size - 1
The address of the next element becomes the start of the array in the next function call. And size reflects the fact that there is one element gone (from the front of the array) -- which the next call knows nothing about.

Before the if you can put print/cout statements to see exactly what each call of the function sees.
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogMeeting the populace by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance comparison between recursive and iterative functions earachefl C++ Forum 1 21-Mar-2006 15:17
help in c++ recursive functions vinch C++ Forum 7 04-Jan-2006 12:22
Recursive functions using pointers kai85 C++ Forum 12 03-Aug-2005 13:52
Understanding Recursive Functions Nexa C++ Forum 5 19-Nov-2004 11:51
conflict between printf and stdarg.h va functions mirizar C Programming Language 3 12-Jul-2004 08:11

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:17.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.