0

I had been trying to find out the reason why this error is happening but found out nothing. This is the code for n-queen problem from backtracking. But this doesn't matter. I want to find out why this error is occurring and how to resolve it.

#include <iostream>

using namespace std;

bool attack_checking(int arr[][s], int s, int i, int j) {
    for(int x = 0; x < s; x++)
        if(arr[i][x] == 1)
            return true;

    for(int x = 0; x < s; x++)
        if(arr[x][j] == 1)
            return true;

    for(int x = 0; x < s; x++) {
        for(int y = 0; y < s; y++) {
            if(x == i && y == j)
                continue;

            if((x + y) == (i + j)) {
                if(arr[x][y] == 1)
                    return true;
            }
            if((x - y) == (i - j)) {
                if(arr[x][y] == 1)
                    return true;
            }
        }
    }
    return false;
}

bool queen(int arr[][s], int s, int n) {
    if(n == 0)
        return true;

    for(int i = 0; i < s; i++) {
        for(int j = 0; j < s; j++) {
            if(attack_checking(arr, s, i, j))
                continue;
            arr[i][j] = 1;
            if(queen(arr, s, n - 1))
                return true;
            arr[i][j] = 0;
        }
    }

    return false;
}

int main() {
    int n, s;
    cin>>n;
    s = n;
    int arr[s][s];
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            arr[i][j] = 0;

    bool ans = queen(arr, s, n);

    if(ans)
    {   
        cout<<"YES\n";
        for(int i = 0; i<n; i++){
            for(int j = 0; j<n; j++){
                cout<<arr[i][j]<<" ";
            }
            cout<<"\n";
        }
    }

    cout<<"NO";

    return 0;
}

new.C:5:32: error: use of undeclared identifier 's' bool attack_checking(int arr[][s], int s, int i, int j) { ^

new.C:32:22: error: use of undeclared identifier 's'

bool queen(int arr[][s], int l) { ^

2 errors generated.

Anonymous
  • 63
  • 2
  • 9
  • I've never seen arguments placed within the brackets of other arguments. Is that allowed? – Carcigenicate Mar 22 '18 at 13:52
  • 1
    @Carcigenicate Well, no, and that is what the compiler says `bool attack_checking(int arr[][s], int s, int i, int j)`; `s` is not known in the context of `arr[][s]`. IMO the question should be retitled to "Use of undeclared identifier in parameter list", 90% of the code removed just to replicate the issue, and the issue would become self-evident. It's probably a dupe in some form as well, but I don't have time and energy to seek for one right now. Sorry. – luk32 Mar 22 '18 at 13:55
  • @Carcigenicate As far as I know, that is something you can only do in C and not in C++. Even in C, you'd have to rearrange the parameters so that 's' is declared before it's used – Burak Mar 22 '18 at 14:27

1 Answers1

0

This:

bool attack_checking(int arr[][s], int s, int i, int j)
                               ^

is not allowed in C++. It is allowed in C as far as I know, although you'd have to declare the parameters before you use them.

One solution is to use a vector<vector<int>> instead of a multidimensional int array.

Burak
  • 667
  • 1
  • 11
  • 19