0

When I run the function "check_row" within itself, there is a problem with the way I'm trying to pass in the array "sudoku_temp", but I'm not sure what I'm doing wrong. Am I missing something?

int check_row(int j_position, int generated_value, int sudoku_temp[][9]){
for (int i_position = 0; i_position < 9; i_position++)
{
    if (generated_value == sudoku_temp[i_position][j_position])
    {
        generated_value = generate_number();
        check_row(j_position, generated_value, sudoku_temp[][j_position]);
    }
    else
        return generated_value;

}

}

To clarify, the problem is when I try to call on the function within itself. Thanks.

Eskimu
  • 5
  • 1

2 Answers2

0

use a variable instead of fixed length like 9. since in recursion the argument may be different.

int check_row(int j_position, int generated_value, int sudoku_temp[][n])
{
    for (int i_position = 0; i_position < n; i_position++)
    {
        if (generated_value == sudoku_temp[i_position][j_position])
        {
            generated_value = generate_number();
            check_row(j_position, generated_value, sudoku_temp[][j_position]);
        }
        else
            return generated_value;

    }
}
Sridhar DD
  • 1,852
  • 1
  • 7
  • 15
0

When you want to extract a value from an array, you cannot leave empty brackets like you did. On the other hand, the function prototype looks similar and it's perfectly fine. Why?

int sudoku_temp[][9] // A 2D array of integers second dimension of which is of size 9

Or in other words, an array (of unknown size) of arrays of size 9. We are not telling the compiler how big the array really is and we don't have to in this case as it is simply given to us as an argument.

When accessing elements on the other hand, we cannot leave empty brackets for a simple reason: we want to access an element and the compiler has to know which one. Cutting to the chase - removing the empty [] should solve your problem.

Kelm
  • 939
  • 5
  • 14
  • I think my problem is that the second time I try to pass the array into the check_row(line 8), I'm not sure how to do it. The array that I'm passing is from my main function, so I don't know how to call on it when I'm in this check_row function. – Eskimu Dec 04 '14 at 05:24
  • Actually I took a second look at your code. You should try some online lessons about arrays, they are really not so easy to understand at first in C, especially the multi dimensional ones. – Kelm Dec 04 '14 at 05:28