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()
{
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()
{
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()
{
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()
{
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()
{
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();
}
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;
}