Screenshot of the code:
Sample Output:
Code to copy:
//Include this header file while using
//visual studio.
#include "stdafx.h"
//Include the required header file.
#include <iostream>
#include <string.h>
//Use the standard naming convention.
using namespace std;
//Define the function createMagicSquare().
void createMagicSquare(int num)
{
//Create an array to store magic
square
//matrix elements.
int** magicSqMatrix = new int*[num];
for (int i = 0; i < num; ++i)
magicSqMatrix[i] = new int[num];
//Initialize all the elements of the
//magic square matrix to 0.
for (int i = 0; i < num; i++)
{
for (int j =
0; j < num; j++)
{
magicSqMatrix[i][j] = 0;
}
}
//Initialize the i and j index values
of
//the matrix.
//The row position is num/2 and
column
//position is num-1 for the first
element.
int i = num / 2;
int j = num - 1;
//Start the for loop from 1 to n^2.
for (int index = 1; index <=
num*num;)
{
//If the row
index and column index is -1
//and num
respectively.
if (i == -1
&& j == num)
{
//Decrement the column index by 2.
j = num - 2;
//Make the row index to 0.
i = 0;
}
//Otherwise.
else
{
//If the column index is equal to num,
//then make it column index to 0.
if (j == num)
j = 0;
//If the row index is less than 0,
//then make the row index to num - 1.
if (i < 0)
i = num - 1;
}
//If the
element of the matrix at the
//index (i,
j) is not equal to 0.
if
(magicSqMatrix[i][j])
{
//Decrement the column index by 2.
j = j - 2;
//Increment the row index by 1.
i++;
//Continue the loop.
continue;
}
//Otherwise.
else
//Make the matrix element at the index
//(i, j) to num++.
magicSqMatrix[i][j] = index++;
//Increment
the row index by 1.
j++;
//Decrement
the column index by 1.
i--;
}
//Start the for loop till size of matrix
num.
for (i = 0; i < num; i++)
{
//Start the
for loop till size of matrix
//num.
for (j = 0;
j < num; j++)
{
//Display the mtrix elements.
cout << magicSqMatrix[i][j] << " ";
}
cout
<< "\n";
}
//Iitialize the required variables to
store row
//sum, column sum, and diagonal sum.
int sumOfRows, sumOfCols,
sumOfDiagonals;
sumOfRows = 0;
sumOfCols = 0;
sumOfDiagonals = 0;
//Display the sum of each rows.
cout << "OUTPUT>> Checking the
sums of every row: ";
//Start the for loop till the size of
matrix.
for (int index = 0; index < num;
index++)
{
//Initialize
the variable sumOfRows to 0.
sumOfRows =
0;
//Start the
for loop till the size of matrix.
for (int
col_index = 0; col_index < num;
col_index++)
{
//Calculate the sum of rows.
sumOfRows += magicSqMatrix[index]
[col_index];
}
//Display
the sum of rows.
cout
<< sumOfRows << " ";
}
cout << endl;
//Sum of each column
cout << "OUTPUT>> Checking the
sums of every ";
cout << "column: ";
//Start the for loop till the size of
matrix.
for (int col_index = 0; col_index <
num;
col_index++)
{
//Initialize
the variable sumOfCols to 0.
sumOfCols =
0;
//Start the
for loop till the size of matrix.
for (int
index = 0; index < num; index++)
{
//Calculate the sum of columns.
sumOfCols += magicSqMatrix[index]
[col_index];
}
//Display
the sum of columns.
cout
<< sumOfCols << " ";
}
cout << endl;
//Display the sum of diagonals.
cout << "OUTPUT>> Checking the
sums of every ";
cout << "diagonal: ";
//Start the for loop till the size of
matrix.
for (int row_index = 0; row_index <
num;
++row_index)
{
for (int
col_index = 0; col_index < num;
++col_index)
{
//If the both row and column index are
//equal.
if (row_index == col_index)
//Calculate the diagonal sum.
sumOfDiagonals +=
magicSqMatrix[row_index][col_index];
}
}
//Display the sum of diagonals.
cout << sumOfDiagonals << "
";
sumOfDiagonals = 0;
//Start the for loop till the size of
matrix.
for (int row_index = 0; row_index <
num;
++row_index)
{
for (int
col_index = 0; col_index < num;
++col_index)
{
//If the both row and column index are
//equal.
if ((row_index + col_index) == (num - 1))
//Calculate the diagonal sum.
sumOfDiagonals +=
magicSqMatrix[row_index][col_index];
}
}
//Display the sum of diagonals.
cout << sumOfDiagonals << "
";
cout << endl;
for (int i = 0; i < num; ++i) {
delete[]
magicSqMatrix[i];
}
delete[] magicSqMatrix;
}
//Start the execution of the main() method.
int main()
{
//Declare the required variables.
int index;
int size;
index = 1;
//Start the while loop till 10.
while (index <= 10)
{
//Prompt the
user to enter the size of the
//matrix.
cout
<< "INPUT>> Enter the size of a magic square: ";
cin >>
size;
if (size
>= 3 && size <= 15)
{
if (size % 2 != 0)
{
cout << "OUTPUT>> Magic Squre #" << index
<< " is:" << endl;
createMagicSquare(size);
cout << endl;
//Increment the index of the loop.
index++;
}
else
{
cout << "Please enter an odd value ";
cout << "of ";
cout << "size of matrix." << endl;
}
}
else
{
cout << "Please enter the value of size ";
cout << "of matrix between 3 and 15."
<< endl;
}
}
//Use this command while using visual
studio.
system("pause");
return 0;
}
Write C++ programs that create TEN(10) different N*N magic squares. A square matrix is the arrangement...
Magic squares. An n × n matrix that is filled with the numbers 1, 2, 3, ..., n2 is a magic square if the sum ofthe elements in each row, in each column, and in the two diagonals is the same value. For example,16 3 2 135 10 11 89 6 7 124 15 14 1Write a program that reads in n2 values from the keyboard and tests whether they form a magic squarewhen arranged as a square matrix. You...
C++ Magic Squares. An n x n matrix that is filled with the numbers 1,2,3,…,n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. The following algorithm will construct magic n x n squares; it only works if n is odd: Place 1 in the middle of the bottom row. After k has been placed in the (i,j) square, place k+1 into the square...
I need this written in C++. Magic Squares. An n x n matrix that is filled with the numbers 1,2,3,…,n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. The following algorithm will construct magic n x n squares; it only works if n is odd: Place 1 in the middle of the bottom row. After k has been placed in the (i,j) square,...
Java Magic Square: A n x n matrix that is filled with the numbers 1, 2, 3,.... n^2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. I need to write an application that gets 16 values as user input, in any order. Store these values in an ArrayList. When the numbers are put into a square, this would be a 4x4 two-dimensional array....
Use basic java for this after importing PrintWriter object You will make a simple Magic Square program for this Java Programming Assignment. Carefully read all the instructions before beginning to code. It is also required to turn in your pseudocode or a flowchart along with this program. Here are the instructions: At the beginning of the program, briefly describe to the user what a Magic Square Matrix is (described further on), and then allow them to enter “start” to begin...
This assignment requires you to write a program to implement an odd-order Magic Square. Prompt the user to enter the “order” of the square and then produce for the user a magic square of that order. Run your code for at least four different inputs – all odd. ODD ORDER MAGIC SQUARES 1. Start by placing 1 in the middle column of the top row. 2. Continue by always placing the next number (say k+1) in the square diagonally up...
The definition of a magic square is a matrix of distinct positive integers in which every row, column and diagonal adds up to the same number. Given a magic square of unknown size, encoded as a nested tuple, write a python function checkMagic(square) that, given a magic square square creates a custom exception MuggleError if a given magic square is not actually magic. Otherwise, it does nothing. python 3
C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand how to traverse a two dimensional array Code and run a program that processes a two dimensional array Instructions: A magic square is a matrix (two dimensional arrays) in which the sum of each row, sum of each column, sum of the main diagonal, and sum of the reverse diagonal are all the same value. You are to code a program to determine...
8. Ann x n matrix that is filled with the numbers 1,2,3, ....n2 is a magic square if the sum of the elements in each row, in each column, and in the two main diagonals is the same value. For example, 16 3 213 5 10 11 8 9 6 7 12 4 15 14 1 Write the program that reads in 16 values from the keyboard and tests whether they form a magic square when put into a 4...
I need help as quick as possible, thanks beforehand. Please provide the test output The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown below. 35 The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 - 9 exactly The sum of each row, each column and each diagonal all add up to the same number. This is shown below: 15 4 92 15 - 81 + 15 15 15...