0

I have a recursive function that has a parameter that is a reference to a 2D matrix of object pointers. My simple question is in what format do I pass that within the function so that it would work?

I wrote this code purely for example to pass my point along and in no way represents my intended use of recursion.

This code checks for a 0 object ID value in a diagonal line through the matrix to 255,255. Then prints 0 or -1.

typedef object* m256x256[256][256];

int x = 0;
int y = 0;

int isThereZero(m256x256 & m){
  if(m[x][y]->getID() == 0){ // Each object has an ID value
    return 0;
  }
  x++;
  y++;
  if(x==256){
    return -1;
  }
return isThereZero(/*I need to pass M byref again... what do I put here?*/);
}
int main{
  object* M[256][256];
  /* Initialization Code */
  cout << isThereZero(M);
  return EXIT_SUCCESS;
}

So that by the 256th recursion m is still the same reference to M

Specific question: How do I format this to get it to compile:

int isThereZero(m256x256 & m){
   return isThereZero(/*I need to pass M byref again... what do I put here?*/);
}
2
  • if you have to send byref then just send the pointer of m256x256 Commented Feb 4, 2014 at 5:18
  • What's wrong with passing in m itself. It compiles fine for me. It also looks like that's how you intended your algorithm to work. Commented Feb 4, 2014 at 15:38

3 Answers 3

1

Rather than using global x and y, try something like so:

bool is_there_zero(const m256x256& m, int x = 0, int y = 0)
{
    if (m[x][y]->getID() == 0)
        return true;

    if (++y == 256) {
        y = 0;
        if (++x == 256) {
            return false;
        }
    }

    return is_there_zero(m, x, y);
}

So we bump the y value, and if that reaches the end of the row we reset it to zero and bump the x coordinate, and finally terminate the recursion when both are equal to 256. The original matrix is untouched.

In other words, we've turned it into a poor man's double for loop, only with added function call overhead and the risk of running out of stack...

As such, I'm not at all sure why you'd want to do it this way rather than just iterating. The only advantage I can see is that, with some "creative" use of the ternary operator you could turn this into a one-line constexpr function, which could potentially be evaluated at compile-time for a fixed m, but that hardly seems worth the bother.

EDIT: Re-reading the question, I see you only wanted to test the leading diagonal, so it's a bit simpler:

bool is_there_zero(const m256x256& m, int x = 0)
{
    if (x == 256)
        return false;

    if (m[x][x]->getID() == 0)
        return true;

    return is_there_zero(m, ++x);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry guess I should have stated that I will be editing the values in the array. And with my setup it wouldn't work when just calling return isThereZero(m);
0

Since you state, your code does not represent your actual code, it is difficult to give an educated answer (imho).

To pass by-reference as you state in your question:

int isThereZero(m256x256 & m);

If you do not change the reference you can pass by const-reference:

int isThereZero(const m256x256 & m);

This allows for some optimizations.

Of course if you pass a pointer to the array by-value:

int isThereZero(object ***m);

This will more or less work like the const-reference version, since it only passes a pointer, not the whole array.

I hope this answers your question

3 Comments

My problem was that I could not figure out how to pass the reference the second time when all I have is the reference. But I will try the passing a pointer to the array byval as it seems I could figure out how to pass that a second time much more easily. No answer but it did give me an idea. Thanks!
With the const reference method could I still edit the original array from within the function with the reference remaining constant?
@user3268412 The reference will be passed on to the next iteration if you use the reference-version. With the const-reference, you still can change the contents of the array.
0

Ah thank you all. You all contributed a little bit to helping me find my problem. As most of you probably realized. Just calling return isThereZero(m); Should have worked. This helped me find that the error was in the syntax of other areas of my program and only caused the compiler to tell me that the problem was with the passing and calling of this array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.