Goals Developing problemsolving skills 2dimensional arrays P

Goals: Developing problem-solving skills, 2-dimensional arrays

Problem: A Magic Square is a 2-dimensional array with the same number of rows and columns (hence the term square). What makes it “magic” it that is filled with the whole numbers 1 to the number of rows and columns squared in an arrangement that the sum of each column and each row is the same value. A Magic Square is considered to be a Lo Shu Magic Square when the sum of the diagonals is the same number.  

Review the 3 tables below. Each is a square because the number of rows is the same as the number of columns (3). Each has been filled with the numbers 1 through 9.   Both A and B are Magic Squares because the sum of each row and column is the same value,15 in this example. B is a Lo Shu Magic Square because the sum of the diagonals (4 + 5 + 6 and 2 + 5 + 8) is also 15.
       3 5 7                 4 9 2              1 2 3

A:    4 9 2        B:      3 5 7      C:    4 5 6     

      8 1 6                  8 1 6              7 8 9                 

A template file has Lab9Template.cpp is online and has code to enable a user to input numbers into a square array. You may assume the user has entered the correct whole numbers. You need to write the introductory comments and code to output the user’s input. Next you need to write the code that determines if the user’s input creates a Lo Shu Magic Square, Magic Square, or not a magic square. The code has been written that the user can create up to a 20 by 20 square. You may assume the user does not enter a size greater than 20.


Template:
/* Add your introductory comments here. */
#include <iostream> using namespace std;
int main() { int magicsquare[20][20];
int rowsandcolumns; cout << \"What size of a magic square would you like to test?\"
<< \"\ (Because it is a square the number of rows equals the number
of columns.) \";
cin >> rowsandcolumns;
                 //assume the user enters a value <= 20
cout << \"Enter the values for the square one value at a time.\"; //assume the user enters correct values
for (int r = 0; r < rowsandcolumns; r++) {
for (int c = 0; c < rowsandcolumns; c++) {
cout << \"Enter the value for row \" << r + 1 << \" column \" << c
+ 1 << \". \";
cin >> magicsquare[r][c];
}
} //Print out their input array here as a table
//test if it is a Lo Shu Magic Square, Magic Square (not Lo Shu), // or not a magic square and output the result
return 0;
}

Solution

#include <iostream>

using namespace std;

int main()

{

int magicsquare[20][20];

int rows;

int columns;

cout << \"What size of a magic square would you like to test?\"

<< \"\ (Because it is a square the number of rows equals the number of columns.) \";

cin >> rows;

cin>>columns;

//assume the user enters a value <= 20

cout << \"Enter the values for the square one value at a time.\";

//assume the user enters correct values

for (int r = 0; r < rows; r++)

{

for (int c = 0; c < columns; c++)

{

cout << \"Enter the value for row \" << r + 1 << \" column \" << c + 1 << \". \";

cin >> magicsquare[r][c];

}

}

//Print out their input array here as a table

for(int i=0;i<rows;i++)

{

for(int j=0;j<columns;j++)

{

cout<<magicsquare[i][j];

}

cout<<\"\ \";

checkType(magicsquare,rows,columns);

return 0;

}

void checkType(int mat[][],int r,int c)

{

int rsc,csc;

int dc;

rsc=csc=dc=0;

int sum=mat[0][0]+mat[0][1]+mat[0][2];

for(int i=0;i<r;i++)

{

int sum1=0;

for(int j=0;j<c;j++)

{

sum1=sum1+mat[i][j];

}

if(sum1==sum)

rsc++;

sum1=0;

}

for(int i=0;i<c;i++)

{

int sum2=0;

for(int j=0;j<r;j++)

{

sum2=sum2+mat[i][j];

}

if(sum2==sum)csc++;

sum2=0;

}

sum1=0;

sum2=0;

int a=0;

int b=c;

for(int i=0;i<r;i++)

{

for(int j=0;j<c;j++)

{

sum1=sum1+mat[i][a];

sum2=sum2+mat[i][b];

a++;

b--;

}

}

if(sum1==sum2&&sum2==sum)dc=2;

if(rsc==r&&csc==c&&dc==2)

cout<<\"\ Lo Shu magic square\";

else if(rsc==r&&csc==c&&dc!=2)

cout<<\"\ magic square\";

else cout<<\"\ not a magic square\";

}

Goals: Developing problem-solving skills, 2-dimensional arrays Problem: A Magic Square is a 2-dimensional array with the same number of rows and columns (hence
Goals: Developing problem-solving skills, 2-dimensional arrays Problem: A Magic Square is a 2-dimensional array with the same number of rows and columns (hence
Goals: Developing problem-solving skills, 2-dimensional arrays Problem: A Magic Square is a 2-dimensional array with the same number of rows and columns (hence
Goals: Developing problem-solving skills, 2-dimensional arrays Problem: A Magic Square is a 2-dimensional array with the same number of rows and columns (hence

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site