I\'m looking for a simple code to develop a\" Magic Square 4*4\" that uses two \
ID: 3624281 • Letter: I
Question
I'm looking for a simple code to develop a" Magic Square 4*4" that uses two "dimensional arrays" and " loops"!! I have been working on one , but still stuck on that, it would not give me the right output !!Help please!!
here is the question:
const int MAX = 4;
int sumRow (const int square[], int row);
// Precondition: square is an initialized matrix, MAX rows x MAX columns
// 0 <= row < MAX
// Postcondition: returns the sum of the values in row
bool unique (const int square[])
// Precondition: square is initialized with integers.
// Action: Inspects every value in square, checking that each one is a unique
// integer ranging from 1..MAX*MAX
// Postcondition: Returns true if each value is unique from 1..MAX*MAX,
// otherwise returns false
Explanation / Answer
}
void metricize( int myList[], int myMatrix[][ COLUMNS ],int s, int r, int c )
{
for ( int i = 0, k = 0; i < r; i++ )
for ( int j = 0; j < c; j++, k++ )
myMatrix[ i ][ j ] = myList[ k ];
}
void reverseDiagonal( int myMatrix[][ COLUMNS ],int r, int c )
{
for ( int i = 0; i < r; i++ )
for ( int j = 0; j < c; j++ )
if ( ( ( i == j ) || ( i + j == r - 1 ) )
&& ( i < r/2 ) )
{
int temp = myMatrix[ i ][ j ];
myMatrix[i][j]= myMatrix[r-1-i][c-1-j];
myMatrix[r-1-i][c-1-j] = temp;
}
}
void magicCheck( int myList[], int myMatrix[][ COLUMNS ],int size, int r, int c )
{
int s = 0, s1 = 0, s2 = 0, s3 = 0;
bool magic = true;
for ( int i = 0; i < size; i++ )
s += myList[ i ];
s /= 4;
for ( int i = 0; i < r; i++ )
s1 += myMatrix[ i ][ 0 ];
for ( int i = 0; i < r; i++ )
s2 += myMatrix[ i ][ 1 ];
for ( int i = 0; i < r; i++ )
s3 += myMatrix[ i ][ 2 ];
if ( ( s == s1 ) && ( s1 == s2 ) && ( s2 == s3 ) )
{
s1 = s2 = s3 = 0;
for ( int i = 0; i < c; i++ )
s1 += myMatrix[ 0 ][ i ];
for ( int i = 0; i < c; i++ )
s2 += myMatrix[ 1 ][ i ];
for ( int i = 0; i < c; i++ )
s3 += myMatrix[ 2 ][ i ];
if (( s == s1 ) && ( s1 == s2 ) && ( s2 == s3 ))
{
s1 = s2 = s3 = 0;
for ( int i = 0; i < r; i++ )
for ( int j = 0; j < c; j++ )
if ( i == j )
s1 += myMatrix[ i ][ j ];
for ( int i = 0; i < r; i++ )
for ( int j = 0; j < c; j++ )
if ( i + j == r - 1 )
s2 += myMatrix[ i ][ j ];
if ( ( s1 != s ) || ( s2 != s ) )
magic = false;
}
else
magic = false;
}
else
magic = false;
if ( magic )
cout << " It is a magic square.";
else
cout << " It is not a magic square.";
}
void printMatrix( int myMatrix[][ COLUMNS ], int r, int c )
{
for ( int i = 0; i < r; i++ )
{
for ( int j = 0; j < c; j++ )
cout << " " << myMatrix[ i ][ j ];
cout << " ";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.