1

Trying to make a program in C that solves N sized triangular board with M queens problem. Can't even get it to work. My guess it crashes because I use arrays incorrectly. Could you please explain to me what I am doing wrong here?

#include <stdlib.h>
#include <stdio.h>

int checkPlaceability(int n, int (*board)[n], int row, int col);
int placeQueens(int n, int m, int (*board)[n], int col);

void main()
{
    int n;
    int board[n][n];
    int m;

    printf("Enter board size: \n");
    scanf("%d", &n);
    printf("Enter queen count: \n");
    scanf("%d", &m);

    if(placeQueens(n, m, board, 0) == 0)
    {
        printf("Solution doesn't exist");
    }
    else
    {
        printf("Solution exists");
    }
}

checkPlaceability(int n, int (*board)[n], int row, int col)
{
    int i;
    int j;
    for(i = col; i < i++)
    {
        if(board[i][col] == 1)
        {
            return 0;
        }
    }
    for(i = 0; i < n; i++)
    {
        if(board[row][i] == 1)
        {
            return 0;
        }
    }
    for(i = abs(row - col)+1, j = 0; i < j && j < n; i++, j++)
    {
        if(board[i][j] == 1)
        {
            return 0;
        }
    }
    return 1;
}

int placeQueens(int n, int m, int (*board)[n], int col)
{
    int i;
    int queenCount = m;
    if(col >= n)
    {
        return 1;
    }
    for(i = 0; i < m; i++)
    {
        if(checkPlaceability(n, board, i, col) == 1)
        {
            board[i][col] = 1;
            queenCount--;
            if(queenCount == 0)
            {
                return 1;
            }
            if(placeQueens(n, queenCount, board, col+1) == 1)
            {
                return 1;
            }
            board[i][col] = 0;
        }
    }
    return 0;
}

1 Answers1

3

You are declaring:

 int board[n][n];

Before you initialize n...

SOLUTION:

move:

int board[n][n];

to after your input statements.

that-ru551an-guy
  • 300
  • 1
  • 12
  • 1
    ...and the contents of `board ` are also uninitialized. – EOF Apr 13 '15 at 18:21
  • @EOF - He initializes them as he goes. That is not the issue. You can declare an array size and not give the indices values until later. – that-ru551an-guy Apr 13 '15 at 18:25
  • I do, however, have an issue with the args he passes his functions. I'm not all too familiar, so I might be wrong, but wouldn't (*board)[n] be wrong in some way? wouldnt he want board[n][] as the argument passed to function? – that-ru551an-guy Apr 13 '15 at 18:28
  • Thanks it worked! But is it ok by coding standarts? Shouldn't all variables be declared before any actual code? – Ričardas Mikelionis Apr 13 '15 at 18:30
  • 2
    So, first `board` is uninitialized, then `placeQueens()` is called. Still uninitialized. `placeQueens()` calls `checkPlaceability()` without initializing `board`, `checkPlaceability()` reads `board` in `if(board[i][col] == 1)`. **NOT. INITIALIZED**. – EOF Apr 13 '15 at 18:31
  • @EOF - You are correct sir! yea in this case set them all to -1 is my best bet. Good catch! – that-ru551an-guy Apr 13 '15 at 18:33
  • 1
    @that-ru551an-guy: The arguments `board[n][]` is of "incomplete element type"; only the first dimension may be inferred, i.e. empty. So it's `board[][n]`, which is equivalent to `(*board)[n]`. [See here](http://stackoverflow.com/questions/29605133/passing-3d-array-as-parameter-to-a-function-in-c/29605265?noredirect=1#comment47357028_29605265), for example. – M Oehm Apr 13 '15 at 18:34
  • @that_ru551an-guy Any easy way to set all values in an array? I know java has Array.fill but does C? – Ričardas Mikelionis Apr 13 '15 at 18:38
  • 2
    @RičardasMikelionis look up memset() – ryyker Apr 13 '15 at 18:41
  • in java, Arrays, much like strings, have been converted to classes and objects, with methods etc... In c, they are not, so no methods can be called, as no methods exist. You could use rand() to fill each slot, but you run the risk of inserting a 1 or a 0, which throws off your program... there is no logic error in having it all start at -1 – that-ru551an-guy Apr 13 '15 at 18:41
  • @ryyker Thanks. Found another thread on here. [MemsetOnSO](http://stackoverflow.com/questions/4066522/setting-an-array-to-one-value) If anyone needs this too, Thanks for all your help! – Ričardas Mikelionis Apr 13 '15 at 18:42
  • @ryyker 's suggestion would set them all equal to the char conversion of whatever you set, so memset(board, -1, sizeof(int)*n*n) would set them all to ASCII char -1, which is an error I believe – that-ru551an-guy Apr 13 '15 at 18:45
  • @that-ru551an-guy - That was not my suggestion. My suggestion was to ***look up memset()***. That would assume asker would look it up , learn its usage and apply it if appropriate. Your example of memset() is indeed a problem. – ryyker Apr 13 '15 at 18:48
  • @ryyker - alright, fair enough. If you were using a char array it would be useful, like if(board[ i ][ j ] == 'p') for is placeable. But then it gets all convoluted. Really its up to u OP – that-ru551an-guy Apr 13 '15 at 18:50
  • @that-ru551an-guy: There is no "ASCII char -1". There is, however, `void *memset(void *s, int c, size_t n)`. Note the `int`. – EOF Apr 13 '15 at 19:40