It\'s Magic A friend tells you about a nifty type of squarematrix called a \"Mag
ID: 3879810 • Letter: I
Question
It's Magic A friend tells you about a nifty type of squarematrix called a "Magic Square" This friend bets you that you can't code up a C+ programthat can determine "Magic Squares" Matrixes A Matrix is a 2 dimensional array A square Matrix has an equal amount of rows and columns An N Matrix i s a squa re matrix with N rows a nd N columns (N × N). Magic Square An N Magic Square is a N matrix (i.e., a square N x N matrix) with positive integer values for its elements (values in x's)) An N Magic Square has the following properties: 1. Itis an N Matrix 2. The integer elements values range from 1 to N squared (N'N) L.. 1. NN 3. The rows, columns and diagonals all have the same sum, called the Magic Square Sum: Magic Square Sum In (n2+1)]/2. All the integer elements in the magic squareare uniquei.e... no two elements can have the Example of a 5 Magic Square 11 24 07 20 03 04 12 2508 16 17 05 13 2109 10 18 01 14 22 23 06 19 02 15 Criteria Check It is a 5x 5 Matrix, soit's a 5 square .The integer elements values range from i to (S x5)ie. 1. 25 All the integer element values aredistincti.e., different from each other, no two are equal. The rows, columns and diagonals allhave the same mag esum of 65, so the Magic Sum is 65. All the above criteria are true, soit's a 5 magic squarewith a magic sumof 65Explanation / Answer
here is your program : --------------------------->>>>>>>>>>>>>>
#include<iostream>
#include<fstream>
using namespace std;
class MagicSquare{
int **magic;
int n;
int sum;
public:
MagicSquare(int m,int **mag){
n = m;
magic = new int*[n];
for(int i = 0;i<n;i++){
magic[i] = new int[n];
for(int j = 0;j<n;j++){
magic[i][j] = mag[i][j];
}
}
sum = (n*(n*n+1)/2);
}
MagicSquare(){
}
bool isPresent(int r,int c){
for(int i = 0;i<n;i++){
for(int j = 0;j<n;j++){
if(i != r && j != c){
if(magic[i][j] == magic[r][c]){
return true;
}
}
}
}
return false;
}
bool isSumRow(int r){
int sumR = 0;
for(int i =0;i<n;i++){
sumR += magic[i][r];
}
if(sumR == sum){
return true;
}
return false;
}
bool isSumCol(int c){
int sumC = 0;
for(int i =0;i<n;i++){
sumC += magic[c][i];
}
if(sumC == sum){
return true;
}
return false;
}
bool isSumDiagonal(){
int sumD1 = 0,sumD2 = 0;
for(int i =0;i<n;i++){
sumD1 += magic[i][i];
sumD2 += magic[i][n-i-1];
}
if(sumD1 == sum && sumD2 == sum){
return true;
}
return false;
}
bool isMagicSqure(){
if(!isSumDiagonal()){
return false;
}
for(int i = 0;i<n;i++){
if(!isSumRow(i)){
return false;
}
if(!isSumCol(i)){
return false;
}
for(int j = 0;j<n;j++){
if(isPresent(i,j)){
return false;
}
if(magic[i][j] < 1 || magic[i][j] > n*n){
return false;
}
}
}
return true;
}
void print(){
system("cls");
for(int i = 0;i<n;i++){
for(int j = 0;j<n;j++){
if(magic[i][j] < 10){
cout<<"0"<<magic[i][j]<<" ";
}else{
cout<<magic[i][j]<<" ";
}
}
cout<<endl;
}
}
};
MagicSquare read(ifstream &f){
int n;
int **mag;
f>>n;
mag = new int*[n];
for(int i = 0;i<n;i++){
mag[i] = new int[n];
for(int j = 0;j<n;j++){
f>>mag[i][j];
}
}
MagicSquare m(n,mag);
return m;
}
int main(){
ifstream f;
f.open("magic.txt");
MagicSquare mag;
if(f.is_open()){
while(!f.eof()){
mag = read(f);
mag.print();
if(mag.isMagicSqure()){
cout<<" It is a Magic square";
}else{
cout<<" It is not a magic square";
}
cin.get();
cin.ignore();
}
}else{
cout<<"File opening error";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.