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.
A template file has Lab9Template.cpp is on CANVAS 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.
Lab09Template:
/* 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;
}
| A | B | C |
| 3 5 7 | 4 9 2 | 1 2 3 |
| 4 9 2 | 3 5 7 | 4 5 6 |
| 8 1 6 | 8 1 6 | 7 8 9 |
Solution
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int magicsquare[20][20];
int r[20],c[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;
if(rowsandcolumns >=20){
cout << \"\ Again Enter Rows and colums should be <=20\"<<endl;
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
for(int i=0;i<rowsandcolumns;i++){
for(int j=0;j<rowsandcolumns;j++){
cout<<magicsquare[i][j];
}
cout<<\"\ \";
}
// Row Sum
for( int i=0; i<rowsandcolumns; i++ )
{
r[i]=0;
for( int j=0; j<rowsandcolumns; j++ )
r[i] = r[i] + magicsquare[i][j];
}
// Column sum
for ( int j=0; j<rowsandcolumns; j++ ){
c[j]=0;
for ( int i=0; i<rowsandcolumns; i++ )
c[j] = c[j] + magicsquare[i][j];
}
// printing rows and colums sums
for(int i=0;i<rowsandcolumns;i++){
cout<<\"Colums sums\"<<c[i]<<endl;
cout<<\"Row Sums\"<<r[i]<<endl;
}
// testing magic square
for(int i=0;i<rowsandcolumns;i++){
if(r[i]==c[i]){
cout<<\"magic square\"<<endl;
}else
cout<<\"Not magic square\";
}
//test if it is a Lo Shu Magic Square, Magic Square (not Lo Shu),
// or not a magic square and output the result
getch();
return 0;
}


