You are on page 1of 11

Source Code of the Program

Main.cpp

//Name:Pavithran Mohan
//ID:5605027
//Assignment:3 (Magic Square)
//This is a program , to Create, Check and Mirror Magic Squares, based on the given tasks
present in the Assignment

//There seems to be a problem with the saving, where the program will crash after saving,
please run the program again for checking
//and mirroring.
#include"functions.h"
using namespace std;

int main()
{
int User;
int n=0;
char filename[20];

do
{
cout<<"Menu"<<endl;
cout<<"..........................................................."<<endl;
cout<<"1. Construct a Magic Square."<<endl;
cout<<"2. Check an existing Magic Square (Create the magic square using the first option)
"<<endl;
cout<<"3. Generate a mirrored version of a chosen magic square (Create the magic square
using the first option)"<<endl;
cout<<"4. Quit program."<<endl;
cout<<"..........................................................."<<endl;
cout<<"Please choose an option\nChoice:";
cin>>User;
while(User<1||User>4)
{
cout<<"Please enter a valid choice,try again.\nChoice:";
cin>>User;
}

switch(User)
{
case 1: MagicSquareMaker(filename,n);
break;

case 2: CheckFile(filename,n);
break;

case 3: MirrorMagicS(filename,n);
break;

case 4: return 0;
}
}while(User!=4);

Function.h
//Headers
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
//Structure
struct mSquare

{
int value;
bool filled=false;
};
//Functions
void MagicSquareMaker(char[],int);
void WriteToFile(mSquare**,int,char[],int);
mSquare** reading(char[],int&,bool&);
void CheckFile(char[],int);
void MirrorMagicS(char[],int);

Functions.cpp
#include "functions.h"
using namespace std;
//Functions
void MagicSquareMaker(char filename[20],int n)
{
int row=0,col=0,indexValue,rowNum=0,tempRow=0,tempCol=0;
char save;

cout<<"Enter a number to construct a Magic Square (Ensure that the provided number is an
odd number):";
cin>>n;
while(n%2==0)
{
cout<<"Please ensure that the number entered is an Odd number.\nTry again:";
cin>>n;
}

mSquare**test;
test=new mSquare*[n];
for(int a=0;a<n;a++)
{
test[a]=new mSquare[n];
}

for(int j=0;j<n;j++)
{
for(int k=0;k<=n;k++)
{
test[j][k].value=0;
test[j][k].filled=false;
}
rowNum++;
}

col=n/2;
indexValue=1;
test[row][col].value = indexValue;
test[row][col].filled = true;

for(int l=0;l<((n*n)-1);l++)
{
indexValue++;
tempRow=row;
tempCol=col;
row--;
col++;

if(row<0)
{
row=n-1;
}
if(col>n-1)
{
col=0;
}
if(test[row][col].filled==true)
{
row=tempRow;
col=tempCol;
row=row+1;

if(row>n-1)
{
row=0;
}
}
test[row][col].value=indexValue;
test[row][col].filled=true;
}

for(int m=0;m<n;m++)
{
for(int o=0;o<n;o++)
{
cout<<test[m][o].value;
if(o<n)
{
cout<<" ";
}
}
cout<<"\n"<<endl;
}

cout<<"save this Magic square?(Y/N)"<<endl;


cin>>save;
if(save=='Y')
{
WriteToFile(test,n,filename,rowNum);
cout<<"Magic Square saved."<<endl;
delete test;
test=NULL;
return;
}
else if(save=='N')
{
cout<<"Magic Square is not saved."<<endl;
return;
}
}

void WriteToFile(mSquare** test,int n,char filename[20],int Ssize)


{
cout<<"Enter the desired filename (add .txt as the extension): ";
cin>>filename;

ofstream outfile;
outfile.open(filename,ios::out);

outfile<<Ssize<<endl;
for(int g=0;g<n;g++)
{
for(int h=0;h<n;h++)
{
outfile<<test[g][h].value<<" ";
}
outfile<<endl;
}
outfile.close();
}

mSquare** reading(char filename[],int&n,bool&found)


{
ifstream infile;

infile.open(filename,ios::in);

if(infile.good())
{
infile>>n;

mSquare**pointerArr;
pointerArr=new mSquare*[n];
for (int a=0;a<n;a++)
{
pointerArr[a]=new mSquare[n];
}

for(int j=0;j<n;j++)
{
for(int k=0;k<n;k++)
{
infile>>pointerArr[j][k].value;
}
}

return pointerArr;
delete pointerArr;
pointerArr=NULL;
}
else
{
cout<<"File not found,returning to menu."<<endl;
found=false;
infile.close();
}
}

void CheckFile(char filename[20],int n)


{
bool valid=true,found=true;
int rowSum,colSum,diagtopSum=0,diagbottomSum=0,x,y;
mSquare**holdingcheck;
cout<<"Please enter the file name for checking.\nFile name:";
cin>>filename;
holdingcheck=reading(filename,n,found);
int sumCalc=n*(pow(n,2)+1)/2;

if(found==true)
{
for(x=0;x<n;x++)
{
for(y=0;y<n;y++)
{
cout<<holdingcheck[x][y].value<<" ";
}
cout<<endl;
}
for(x=0;x<n;x++)
{
rowSum=0;
for(y=0;y<n;y++)
{
rowSum+=holdingcheck[x][y].value;
}
if(rowSum!=sumCalc)
{
valid=false;
}
}

for(y=0;y<n;y++)
{
colSum=0;
for(x=0;x<n;x++)
{
colSum+=holdingcheck[x][y].value;
}
if(colSum!=sumCalc)
{
valid=false;
}
}

y=0;
for(x=0;x<n;x++)
{
diagtopSum+=holdingcheck[x][y].value;
y++;
}
if(diagtopSum!=sumCalc)
{
valid=false;
}

y=n-1;
for(x=0;x<n;x++)
{
diagbottomSum+=holdingcheck[x][y].value;
y--;
}
if(diagbottomSum!=sumCalc)
{
valid=false;
}
}
else if(found==false)
{
return;
}

if(valid==true)
{
cout<<"Square is valid."<<endl;
delete holdingcheck;
holdingcheck=NULL;
return;
}
else if(valid==false)
{
cout<<"Square is invalid."<<endl;
delete holdingcheck;
holdingcheck=NULL;
return;
}
}

void MirrorMagicS(char filename[20],int n)


{
bool filefound=true;
int c,d;
int rowcount=0,colcount=0,colrevert=0;
mSquare**mirrorHolder;
cout<<"Please enter the file name.\nFile name:";
cin>>filename;
mirrorHolder=reading(filename,n,filefound);

if(filefound==true)
{
for(c=0;c<n;c++)
{
rowcount++;
for(d=0;d<n;d++)
{
cout<<mirrorHolder[c][d].value<<" ";
}
colcount++;
colrevert++;
cout<<endl;
}
cout<<""<<"\n"<<""<<endl;

for(c=0;c<n;c++)
{
for (d=0;d<n;d++)
{
cout<<mirrorHolder[rowcount-1][colcount-1].value;
colcount--;
cout<<" ";

}
cout<<"\n";
rowcount--;
colcount=colrevert;
}

delete mirrorHolder;
mirrorHolder=NULL;
return;
}
return;
}
Images of the code in Action:-

You might also like