Magic Square Program (C++)

前端 未结 5 472
没有蜡笔的小新
没有蜡笔的小新 2020-12-17 01:12

For those unfamiliar with the classic magic square algorithm: A magic square is a two dimensional array (n x n) which contains a numerical value between the values 1 and n^2

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-17 01:39

    You forgot to initialize your MagicSquare to contain all zeros:

      for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
          MagicSquare[i][j] = 0;
        }
      }
    

    Thus this check will almost always fail:

    if ( MagicSquare[newRow][newCol] == 0 ) {
       i = newRow;
       j = newCol;
    }
    

    As C/++ doesn't initialize them to 0 for you.

提交回复
热议问题