14,640,670 members
Rate this:
See more:
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.
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;
}```
Posted
Updated 22-Dec-10 7:50am
v2
Henry Minute 22-Dec-10 13:37pm

I'm not sure that anyone will follow a link from somebody they don't know. I could be wrong but it would have been better if you added your output code in your question.
TheyCallMeMrJames 22-Dec-10 14:33pm

I thought Sudoku was a 9x9 grid of 3x3 grids. Isn't magic number something different?

Rate this:

## Solution 2

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`.
Rate this:

## Solution 1

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.
optimus_prime1 22-Dec-10 14:58pm

Oh thanks again! Actually learning this stuff myself!