Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++ Write a program that will take a 3x3 matrix and test to see if it is a ma

ID: 3731098 • Letter: I

Question

In C++ Write a program that will take a 3x3 matrix and test to see if it is a magic square. A 3x3 matrix is “magic” if all of the following sums are the same: each row, each column, and both diagonals.

The following example is a magic square because everything adds up to the same number (2+7+6=15, 9+5+1=15, 4+3+8=15, 2+9+4=15, 7+5+3=15, 6+1+8=15, 2+5+8=15, and 4+5+6=15).

2

7

6

9

5

1

4

3

8

Use functions – don’t put everything in main

Use loops to calculate the sums – don’t hardcode the calculation. For example, you don’t want something like sum = matrix[0][0] + matrix[0][1] + matrix[0][2]. That is hard to update if we change the size of the array

2

7

6

9

5

1

4

3

8

Explanation / Answer

/*

You can solve this problem ,
1. create an empty matrix of size 3x3
2. Read input from user
3. Call function to validate
-(i) Declare 8 variables for rows ,columns and diagonals
-(ii) Iterate over all the elements and find sum based on conditions.

4.compare all sums

Top left to bottom right diagonal: YOu can find sum of this diagonal by just checking for same index
because we know indexes are same for this diagonal

Top right to bottom left diagonal: For this diagonal you can check for sum of indexes like
row+col==size-1


Sample Input:
Enter 9 elements:
2 7 6
9 5 1
4 3 8

Sample Output:
Magic

Sample Input 2:
Enter 9 elements: 8 7 9 5 6 4 2 3 5

Sample Output:

Not Magic
*/

//************************************************

//Create .cpp file and copy code from below line to the end of answer

#include<iostream>
#include<fstream>

using namespace std;

//Function to check magic matrix
bool checkMagicMatrix(int matrix[3][3])
{
int row,col;
//Declaring all variables to store sums
int row1=0,row2=0,row3=0,col1=0,col2=0,col3=0,dig1=0,dig2=0;

//Iterating over all the elements and finding sums
for(row=0;row<3;row++)
{
for(col=0;col<3;col++)
{
//Calculating sum of rows
if(row==0)
{
row1 +=matrix[row][col];
}

if(row==1)
{
row2 +=matrix[row][col];
}
if(row==2)
{
row3 +=matrix[row][col];
}

//Calculating sum of column
if(col==0)
{
col1 +=matrix[row][col];
}

if(col==1)
{
col2 +=matrix[row][col];
}
if(col==2)
{
col3 +=matrix[row][col];
}

//Calculating sum of diagonals
//top left to bottom right
if(row==col)
{
dig1 +=matrix[row][col];
}

//top right to bottom left
if(row+col==2)
{
dig2 +=matrix[row][col];
}
}
}

//checking if all sums are same by comparing all with sum of row1
if(row1==row2 && row1==row3 && row1==col1 && row1==col2 && row1&&col3 && row1==dig1 && row1&&dig2)
{
return true;
}
else{
return false;
}
}
//Main method
int main()
{
//Declaring matrix of size 3x3

int matrix[3][3];
int i,j;
bool isMagic;

cout<<"Enter 9 elements: ";
//reading input from user
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>matrix[i][j];
}
}

//Calling function to check if matrix is magic or not
isMagic=checkMagicMatrix(matrix);

//Displaying output
if(isMagic==true)
{
cout<<"Magic";
}
else
{
cout<<"Not Magic";
}
}