Question

Write C++ programs that create TEN(10) different N*N magic squares. A square matrix is the arrangement of the numbers 1, 2, ., N2, in which the sum of rows, columns, and diagonals are the same. The users (i.e., TAs) will specify the size of the square matrix: N. The value N must be an odd number between 3 and 15. Example Run For example, you program is expected to run as the following way. NOTE: We only list 5 magic squares on this example INPUT> Enter the size of a magic square: 3 OUTPUT» Magic Square #1 is: OUTPUT> Checking the sums of every row OUTPUT> Checking the sums of every column: 15 15 15 OUTPUT>> Checking the sums of every diagnoal: 15 15 15 15 15
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot of the code:

//Include this header file while using //visual studio. #include stdafx.h //Include the required header file #include <iost//Make the row index to 0. //Otherwise. else //If the column index is equal to num, //then make it column index to 0. if (j nfor (i-0i< num; //start the for loop till size of matrix < num; j++) //Display the mtrix elements //num. for (j 0; cout << mafor (int col index 0; col index < num; col index++) //Initialize thevariable sumOfCols to 0. //start the for loop till the sifor (int col index = 0; col index < num; ++col index) //If the both row and column index are if ( (row in dex + colindex)-(nu//Increment the index of the loop indext+ elsee cout << Please enter an odd value; cout << of; cout << size of matrix.

Sample Output:

Enter the size of a magic square: 3 Magic Squre #1 is: 2 7 6 9 5 1 4 3 8 OUTPUT> Checking the sums of every row: 15 15 15 OUTEnter the size of a magic square: 9 Magic Squre #4 is: 35 25 15 5 76 66 56 46 45 24 14 4 75 65 55 54 44 34 13 3 74 64 63 53 432 18 4 159 145 131 13θ 116 102 88 74 60 46 17 3 158 144 143 129 115 1ΘΊ 87 73 59 45 31 2 157 156 142 128 114 10θ 86 72 58 44Enter the size of a magic square: 7 Magic Squre #8 is: 20 12 4 45 37 29 28 11 3 44 36 35 27 19 2 43 42 34 26 18 10 49 41 33 2

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;
}

Add a comment
Know the answer?
Add Answer to:
Write C++ programs that create TEN(10) different N*N magic squares. A square matrix is the arrangement...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Magic Squares; Java code

    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...

    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...

    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,...

    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...

    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...

    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...

    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 h...

    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...

    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...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT