且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查矩阵是否为数独

更新时间:2023-11-25 22:04:46

此外,确定对角线总和的逻辑也被弄乱了.它不会返回正确的值,因此无论如何实际上都没有任何意义.

这应该是非常简单的逻辑,并且使它变得更加复杂.

将其分成两个对角线.第一个对角线非常简单.您应该只有一个for语句,并且要增加一个变量,因为您只是将
中的值相加 [0,0]
[1,1]
[2,2]
[3,3]

第二个对角线稍微有些困难,尽管仍然没有那么困难.您正在将
中的值相加 [0,3]
[1,2]
[2,1]
[3,0]

您的sumrow逻辑也有问题.您创建了一个名为sum的数组,该数组用于存储每一行​​的总和.然后,您创建了两个for循环. i是行数. j是列数.但是然后,您实际上将列加起来,因为您说了sum[j].

检查行是否相等也变得太复杂了.您无需将每行与其他行进行检查.您需要做的就是将第一行与另一行进行对照.如果第一行等于所有其他行,则其他行也等于其他行.

您在sumcol函数中做了同样的事情.

您还将checksum声明为bool函数,但没有返回值.

如果您不打算返回值,则应将其声明为void.你自己的吗?

您不了解数组编号的方式.它们从0开始.这意味着如果声明一个包含2个元素的数组(如sum[2]={0,0};所示,则第一个元素为sum[0],第二个元素为sum[1].

您已尝试访问不存在的数组成员,因为您将它们视为基于1的数组.


Hi,
I tried a program that takes an input square matrix and wanted to check if it''s a sudoku or not. (If the sum of each of the rows, diagonals, columns is equal, then its a sudoku aka a magic square).
I wrote all of the class definitions and class methods. The program takes the input and doesn''t give any output. I have been stuck on this.
Here''s the source download link (271 kB):
http://www.mediafire.com/file/e2gnw4yzezpknc3/MagicSquare.zip[^]

Sample Input square matrix:
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1

Thanks!

Edit--
Here''s the code (not working):
sudoku.h

#include<iostream>
const int maxrow=4,maxcol=4;
class sudoku
{
  public:
    void initialize();
    bool checksum();
  private:
    int square[maxrow][maxcol];
    bool equality;
    int sumdg();
    int sumrow();
    int sumcol();
};




sudoku.cpp

#include"sudoku.h"
#include<iostream>
using namespace std;
void sudoku::initialize()
{
    /*Pre:*/
    /*Post: We have a square box filled with numbers*/
    int i,j;
    cout<<"Enter the elements in each cell: ";
    for(i=1;i<=maxrow;i++)
    {
    for(j=1;j<=maxcol;j++)
    {
    cout<<"a["<<i<<j<<"]= :";
    cin>>square[i][j];
    }
    }
    checksum();
}
int sudoku::sumdg()
{
    /*Pre: We have the square of elements*/
    /*Post: Sum of diagonals is returned*/
    int i,j, sum[2]={0,0};
    for(i=1;i<=maxrow;i++){
    for(j=1;j<=maxcol;j++){
    if(i==j){
    sum[1]=sum[1]+square[i][j];}
    if((i+j)==(maxrow+1)){
    sum[2]=sum[2]+square[i][j];}
    }
    }
    if(sum[1]!=sum[2])
    {
        equality=false;
    }
    cout<<sum[1];
    return sum[1];
}
int sudoku::sumrow()
{
    /*We have the square of elements*/
    /*Post: Sum of rows is returned*/
    int i,j, sum[maxrow];
    for(i=1;i<=maxrow;i++){
        sum[i]=0;}
    for(i=1;i<=maxrow;i++){
    for(j=1;j<=maxcol;j++){
        sum[j] = sum[j] + square[i][j];
    }
    }
    for(j=1;j<=maxcol;j++)
    {
    for(i=j+1;i<=maxcol;i++)
    {
        if(sum[j]!=sum[i])
        {
        equality = false;
        }
    }
    }
    cout<<sum[0];
    return sum[0];
}
int sudoku::sumcol()
{
    /*We have the square of elements*/
    /*Post: Sum of columns of returned*/
    int i,j, sum[maxcol];
    for(i=1;i<=maxrow;i++){
        sum[i]=0;}
    for(j=1;j<=maxcol;j++){
    for(i=1;i<=maxrow;i++){
    sum[i]= sum[i] + square[i][j];
    }
    }
    for(j=1;j<=maxcol;j++){
    for(i=j+1;j<=maxcol;i++){
        if(sum[j]!=sum[i])
        {
            equality=false;
        }
    }
    }
    cout<<sum[0];
    return sum[0];
}
bool sudoku::checksum()
{
    /*Pre: We have the sum of each of the diagonals, rows and columns*/
    /*Post: True if returned if sum of each of the diagonals, rows and columns is equal. */
    int a=sumdg();
    int b=sumrow();
    int c=sumcol();
    if(a==b && a==c && b==c && equality==true)
    {
    cout<<"This is a magic square";
    }else{
    cout<<"This isn't a magic square";
    }
}




main.cpp

#include <iostream>
#include"sudoku.h"
using namespace std;

int main()
{
    void instructions();
    sudoku check;
    check.initialize();
    //if(check.checksum())
    //{
    //    cout<<"This is a magic square";
    //}
    //else
    //{
    //cout<<"Not a magic square";
    //}
    //return 0;
}
void instructions()
{
    cout<<"Welcome to Sudoku checker."<<endl;
    cout<<"If the sum of each of the rows, columns, and diagonals will be equal,"<<endl;
    cout<<"then it is a magic square else not"<<endl;
    cout<<"Enter the elements of the dquare now."<<endl;
}

Also, your logic for determining the sum of the diagonals is all messed up. It doesn''t return the right values, and for that matter really doesn''t make any sense anyway.

It should be pretty simple logic and you made it more complicated.

Separate it into the two diagonals. The first diagonal is very simple. You should have a single for statement with one variable that you increment because you''re simply adding up the values in
[0,0]
[1,1]
[2,2]
[3,3]

The second diagonal is a little more difficult, though still not that difficult. You''re adding up the values in
[0,3]
[1,2]
[2,1]
[3,0]

You also have problems with your sumrow logic. You created an array called sum that is meant to store the sum of each row. Then, you created two for loops. i is the number of rows. j is the number of columns. But then, you actually add up the columns because you say sum[j].

You also get too complicated with checking if the rows are equal. You don''t need to check every row against every other row. All you need to do is check the first row against the other. If the first row is equal to all of the other rows, then the other rows are equal to the others as well.

You did the same thing in the sumcol function.

You also declared checksum a bool function, but you don''t return a value.

If you''re not going to return a value, you should declare it as a void.


are you taking a class for this right now or just trying to figure it out on your own?

You don''t understand the way that arrays are numbered. They are 0-based. That means that if you declare an array with 2 elements (as in sum[2]={0,0}; then, the first element is sum[0] and the second element is sum[1].

You have tried to access members of arrays that don''t exist because you are treating them as if they were 1-based.