#include <iostream>
using namespace std;
bool isMagicSquare(int** matrix, int len) {
int sum = 0;
// row sum
for (int i = 0; i < len; i++) {
int rowSum = 0;
for (int j = 0; j < len; j++) {
rowSum += matrix[i][j];
}
if (sum == 0) {
sum = rowSum;
} else {
if (rowSum != sum) {
return false;
}
}
}
// column sum
for (int i = 0; i < len; i++) {
int colSum = 0;
for (int j = 0; j < len; j++) {
colSum += matrix[j][i];
}
if (colSum != sum) {
return false;
}
}
// main diagonal
int diaGonalSum = 0;
for (int i = 0; i < len; i++) {
diaGonalSum += matrix[i][i];
}
if (diaGonalSum != sum) {
return false;
}
// reverse diagonal
diaGonalSum = 0;
for (int i = 0; i < len; i++) {
diaGonalSum += matrix[i][len - 1 - i];
}
if (diaGonalSum != sum) {
return false;
}
return true;
}
void printMatrix(int **matrix, int len) {
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
int main() {
int size = 3;
int **matrix = new int*[size];
for(int i=0; i<size; i++) {
matrix[i] = new int[size];
for(int j=0; j<size; j++) {
cout << "Enter cell value at (" << i << ", " << j << "): ";
cin >> matrix[i][j];
if(matrix[i][j] < 0) {
cout << "Invalid entry.";
j--; // so that this cell is repeated
}
}
}
printMatrix(matrix, size);
if(isMagicSquare(matrix, size)) {
cout << "Matrix is a magic square" << endl;
} else {
cout << "Matrix is not a magic square" << endl;
}
}
9. Lo Shu Magic Square The Lo Shu Magic Square is a grid with three rows and three columns that h...
please use java language
please used ArrayList
The Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in Figure 7-31. The • The sum of each row, each column, and each diagonal all add up to the same number 20. Lo Shu Magic Square Lo Shu Magic Square has the following properties: • The grid contains the numbers 1 through 9 exactly. This is shown in Figure 7-32. In a program you can simulate a...
I am using C++ Write a program to determine if a gird follows the rules to be classified as a Lo Shu Magic Square. The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in figure below. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 through 9 exactly. The sum of each row, each column, and each diagonal all add up to the same number. This is shown...
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...
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...
Concepts tested by the program:
Working with one dimensional parallel arrays
Use of functions
Use of loops and conditional statements
Description
The Lo Shu Magic Square is a grid with 3 rows and 3 columns
shown below.
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.
s is shown below:
Write a program that...
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...
Help pls! and kindly explain how you do this as well. Thaank you Write a program to test whether a square is a 3x3 magic square. A magic square is a grid with 3 rows and 3 columns, like the figure below. A magic square has the following properties: the grid contains only the numbers 1 through 9 the sum of each row, each column, and each diagonal all add up to the same number Notes: I have provided the...
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...
Write a Python program that generate randomly a magic square of 3 x 3 elements. For example: 4 3 8 9 5 1 2 7 6 = Matrix1 Matrix 1 above is an example of magic square. The rows total, the columns total, and the diagonal totals are all 15. Your program should randomly generate a 3x3 matrix. Check if it is a magic square. If it is a magic square, then print some information and quit. If the...
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....