0

I've programmed with other languages, but now that I am learning C++, I've found a problem. I am trying to solve a problem with recursion, with a method that takes an array as an argument. I thought about using a public array, maybe, but I can't use the array either way.

From what I've read, it seems to me that it has something to do with the memory. (I thought that, even though it consumes a lot of memory, creating it again with each call would work.)

Here's some code:

static void FindSolution(int row, int column, bool answer[][8][8]) {
    for(int i = 0; i < 8; i++) 
        //Some processing…
        bool temp = true;
        FindSolution(0, column + 1, answer[row][column] = temp);
    }
}

How do I get to actually use the array? Somehow.

The error:

error: array type 'bool [8]' is not assignable
         FindSolution(0, column + 1, answer[row][column] = temp);
Prashant Kumar
  • 14,945
  • 14
  • 46
  • 63
OFRBG
  • 1,076
  • 9
  • 23
  • Is there an error you are receiving? – 0x499602D2 Oct 01 '13 at 01:14
  • What is `temp`? Is it an array, integer? – 0x499602D2 Oct 01 '13 at 01:17
  • Your `answer` array is 3-dimensional, but you only provided two indexes in the assignment. And why are you putting the assignment in the function call? – Barmar Oct 01 '13 at 01:19
  • 1
    `answer` has the type *array of an unknown number of arrays of 8 arrays of 8 `bool`* and decays to *pointer to an array of 8 arrays of 8 `bool`*, and `answer[row][column]` has type *array of 8 `bool`*. – dyp Oct 01 '13 at 01:20
  • @Barmar The C++ documentation states the following: Notice that the first brackets [] are left empty while the following ones specify sizes for their respective dimensions. This is necessary in order for the compiler to be able to determine the depth of each additional dimension. – OFRBG Oct 01 '13 at 01:20
  • You still have to specify all 3 dimensions when accessing or assigning an array element. – Barmar Oct 01 '13 at 01:21
  • 1
    @Fiire That is *one* C++ tutorial / help site, not the *official* C++ documentation (which that might sound like) -- there's no official *documentation* btw (only a Standard). – dyp Oct 01 '13 at 01:21

3 Answers3

2

You have an extra [] on your array. You've declared it as a 3D array, but then you try to assign to it like it is a 2D array. The compiler gets upset because you try to assign a bool value to an array, which is exactly what you are doing:

answer[row][column] = temp;

temp has type bool, but answer[row][column] has type bool[8].

Instead declare the argument without the extra []:

static void FindSolution(int row, int column, bool answer[8][8]) {
cdhowie
  • 133,716
  • 21
  • 261
  • 264
0

You keep incrementing 'column', but you never check it to make sure it doesn't reach 8. When it does reach 8, you're off the end of the array, and you get an error.

Adam Miller
  • 747
  • 1
  • 10
  • 22
  • 1
    Actually you won't get an error, but you *will* trigger undefined behavior -- an important difference. Nonetheless, it is an error in the program. – cdhowie Oct 01 '13 at 01:22
  • Given that there's a very vague `//Some processing...` comment in there, I doubt this is all of his code, not really a relevant answer as there is a far more obvious error here. – Jason Larke Oct 01 '13 at 01:25
0

There are a few immediate problems with this.

First Problem: Function signature is incorrect You've declared the third parameter as a 3-dimensional array, but you only want to deal with two dimensions it seems. There are a couple of ways you can redeclare this function to accept a 2D array, for all the options see the accepted answer here. Personally, in this situation I'd go with a template option unless there is a specific reason not to. Something like the following:

template<size_t _rows, size_t _columns>
static void FindSolution(int row, int column, bool (&answer)[_rows][_columns]) {
    // todo: Some processing...
}

This allows you to accurately know the size of the array at compile time, of course this won't work so well with dynamically allocated arrays but seeing as you seemed to know the dimensions of the array already at compile time, I figured this wasn't an issue. If it is, check the other ways of passing a 2D array to a function in the link I attached.

Second issue: Recursive call The second issue is how you're doing your recursive call.

FindSolution(0, column + 1, answer[row][column] = temp);

The result of the assignation of temp to the specific location in the answer array is not the answer array, but rather the value of temp. Effectively the following statement:

answer[row][column] = temp

Is trying to pass a single bool value as a 2-dimensional array, which won't work. In order to correctly call the method again you'll need to do your assignation of temp to the answer array, then call the function again.

answer[row][column] = temp;
FindSolution<_rows,_columns>(0, column + 1, answer);

Should work fine. (Note the explicit template arguments here <_rows,_columns>, this is only needed if you're using the function signature I posted above which made use of templates)

Community
  • 1
  • 1
Jason Larke
  • 4,648
  • 22
  • 28
  • Why would you pass an array by reference? It already decays into a pointer. You gain nothing by passing it by reference. – cdhowie Oct 01 '13 at 02:08
  • No, they aren't. But in this case, how does using a reference in any way change the semantics of the parameter? – cdhowie Oct 01 '13 at 03:02
  • One obvious difference is that you can't pass NULL directly to the function. With my definition `FindSolution<1,5>(0, 0, NULL);` will fail to compile, omitting the reference allows that call to compile without issue. Sure, you can still fool the compiler by typecasting a NULL value, but it can be useful. – Jason Larke Oct 01 '13 at 07:38