0

I'm going to make an N-queen with a stack. However, There's a problem with the chords. The console window says "return value 3221225477" I searched it was a scanf problem, but I'm not It hasn't been long since I learned coding, so the code is inefficient and complicated. I'm so sorry. Please give me a kind explanation.

#include <stdio.h>
#include <stack>
#include <math.h>

int n, col[11], k = 1; 

void output(){
    for(int i = 1; i<=n; i++)
        printf("(%d, %d)",i,col[i]);
    printf("\n");
}
int main()
{
    std::stack <int> S; 
    int check, flag = 1, i = 1;
    scanf(" %d", &n);
    for(int j = 1; j <= n;j++)
        S.push(n-j+1);
    while(!S.empty())
    {
        while(S.top() == 0)
        {
            S.pop(); 
            printf("%d",k);
            k--; 
        }
        check = 0;
        col[k] = S.top();
        if(k == n)
            output();
        if(!S.empty())
            S.pop();
        for(int j = 1; j <= n && k < n; j++)
        {
            flag = 1;
            i = 1;
            while(i < k+1){
                if((abs(i-(k+1)) == abs(col[i]-j)) || (col[i] == j))
                    flag  = 0;
                i++;
            }   
            if(flag == 1)
            {               
                if(check == 0)
                {
                    S.push(0);
                    k++;
                    check = 1;
                }
                S.push(j);
            }
        }
    }
}
  • 1
    what is the value of `n`? Array indices go from 0 till size-1 – 463035818_is_not_an_ai Dec 15 '21 at 09:08
  • Did you try to debug the code? Why not? – Quimby Dec 15 '21 at 09:09
  • 1
    `while(S.top() == 0) { S.pop();...}` -- What happens if `S.pop()` causes the stack to be empty? The next `S.top()` will cause undefined behavior. That should be `while (!S.empty() && S.top() == 0)` – PaulMcKenzie Dec 15 '21 at 09:16
  • `for (int i = 1; i <= n; i++)` : array indexes start at 0, not at 1. Not sureif this is the actual problem, but it definitely is a problem. – Jabberwocky Dec 15 '21 at 09:17
  • @463035818_is_not_a_number n is the size of the chessboard. – 1312_이태현 Dec 15 '21 at 09:19
  • what is its value? Please include input, output and expected output in the question – 463035818_is_not_an_ai Dec 15 '21 at 09:19
  • btw you are mixing C and C++. Each of them is difficult enough on their own, you shouldnt mix them when not necessary. If you learned to write code like this from some tutorial, then you need to find better material to learn actual C++ (see eg https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – 463035818_is_not_an_ai Dec 15 '21 at 09:21
  • 1
    @PaulMcKenzie I concur; alas it only prolongs the inevitable. Two lines after that broken while-loop ends `S.top()` is blindly referenced anyway. This algorithm needs a review and some TLC, and above all, the OP needs to stop fighting 0-based indexing and embrace it as part of the language. – WhozCraig Dec 15 '21 at 09:24

0 Answers0