Thread: what this function does..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    what this function does..

    Code:
    int what2(int *arr1,int *arr2,int i)
    {
       if(*arr1==-1) return 0;
       if (i)
           return *arr1-what2(arr+1,arr2+1,1-i);
       else
           return *arr2-what2(arr+1,arr2+1,1-i);
    }
    i was told that both arrays end by -1
    i tried arr1: 1,2,-1
    arr2: 3,4,-1
    i=2
    so i get:
    1-what2(2,4,-1)==>1-(4-what(-1,-1,2))=
    it never ends??

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    If i == 0, then 1-i == 1. If i == 1, then 1-i == 0. I suspect that the only valid values for i are 0 and 1, and this is acting as a toggle to switch between two options at each level. The whole things ends when the end of the first array is reached.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so its if it i=1
    then we sum on the diagonal from arr1
    arr1: 1 2 6 -1
    arr2: 3 4 5 -1
    if starts from i=1
    1-4+6
    if starts from i=0
    3-2+5
    correct?

    what is the word definition for this diagonal thing??

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    129
    For your original question, I suggest that you loop through the source arrays to see if -1 ever occurs. If it does, print something and stop looping. If you get a segfault while doing that, you've run out of the array and into some other memory you're not supposed to be looking at - i.e., -1 isn't in the array.

    But your logic looks right to me, and the what2 function is recursive.
    Last edited by Jesdisciple; 03-23-2009 at 09:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM