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

the code is required in C++ Description of Problem - A magic square is a 2-dimen

ID: 3715566 • Letter: T

Question

the code is required in C++

Description of Problem - A magic square is a 2-dimensional array of positive integers, which has the following characteristics: . The numbers of rows equals the number of columns . All entries must be positive integers e The sum of all the entries in each row, each column, and the two diagonals are identical In a 2-dimensional array, one diagonal goes from the upper left corner to the lower right corner. The other diagonal goes from the lower left corner to the upper right corner. See the diagram on the following page. Task to perform 1. Populate the array displayed on the following page. 2. Do the Sums for all columns, rows, and diagonals. There will be 8 sums altogether 3. Display all the results. Make sure to identify where the sum was located. Example "Sum of 1st row 4. Multiply the 2nd column of numbers by 4 5. Subtract 3 from the center number 6. Add 3 to the last number in the last row 7. Do the Sums again for all columns, rows, and diagonals 8. Display all the results agairn 9. Draw out the results of the Magic Square 10. Answer the following question. Is the sum of the 2nd column greater than, less than, or equal to the sum of the top left to bottom right diagonal?

Explanation / Answer

The C++ code is as follows:


#include<iostream>
using namespace std;
void calc(int arr[3][3])
{
// to calculate the sum of each row
for(int i = 0 ; i < 3; i++)
{
int sum = 0;
for(int j = 0; j < 3; j++)
{
sum = sum + arr[i][j]; // calcualting sum at each i
}
cout << "The sum of the " << i+1 << " row is " << sum << endl;
}
// to calculate the sum of each row
for(int i = 0; i < 3; i++)
{
int sum = 0;
for(int j = 0; j < 3; j++)
{
sum += arr[j][i]; // calculating sum at each i
}
cout << "The sum of the " << i+1 << " column is " << sum << endl;
}
// to calculate the sum of the two diagonals
int sum = 0;
for(int i = 0; i < 3; i++)
{
sum += arr[i][i]; // calculating sum where both indexes are equal
}
cout << "The sum of the upper left to lower right diagonal will be " << sum << endl;
sum = 0;
//to calculate the sum of the upper right to lower left diagonal
for(int i = 0; i < 3; i++)
{
sum += arr[i][2-i]; //calculating sum where the indexes sum equal to 2
}
cout << "The sum of the upper right to lower left diagonal will be " << sum << endl;
}
int main()
{
int a[3][3] = {{8,3,4}, {1,5,9}, {6,7,2}}; // declaring the array
calc(a); // callin the function
// multiplying seccond column by 4
for(int i = 0; i < 3; i++)
{
a[i][1] = a[i][1] * 4;
}
//subtract three from center number
a[1][1] -= 3;
//add three to the last number in last row
a[2][2] += 3;
calc(a); // calling the function
return 0;
}

Output:

The sum of the 1 row is 15
The sum of the 2 row is 15
The sum of the 3 row is 15
The sum of the 1 column is 15
The sum of the 2 column is 15
The sum of the 3 column is 15
The sum of the upper left to lower right diagonal will be 15
The sum of the upper right to lower left diagonal will be 15
The sum of the 1 row is 24
The sum of the 2 row is 27
The sum of the 3 row is 39
The sum of the 1 column is 15
The sum of the 2 column is 57
The sum of the 3 column is 18
The sum of the upper left to lower right diagonal will be 30
The sum of the upper right to lower left diagonal will be 27

By the output the sum of second column is 57 while the sum of the upper left to lower right diagonal is 30 , so tthe answer to the fill in the blank is greater than