Magic square code
Rating:
4,8/10
1663
reviews

Can i get some direction on this please. The program below shows how we can determine whether a series of 9 numbers forms a magic square or not. Develop a C++ program which determines if a square matrix is a magic square. I'm wondering if it actually is? Therefore, line 34 and 35 are skipped. The number n is called the order of the magic square. The trick with making such a square is to place the number 1 in the first row and middle column.

In order to determine whether it is a magic square, we need to calculate the sum of each row, column, and diagonal, and check whether they are all the same. Obviously, now that we have come across a sum value that is different to the previous one, we no longer need to do further calculations as it is definitely not a magic square. If you scan through to the bottom of the program, you will notice that lines 16, 24, 28, 39, 52, 63, and 75 are also for comments only. A magic square whose rows and columns add up to a magic number but whose main diagonals do not, is known as a semimagic square. Consequently, the statements on lines 34 and 35 are ignored. I asked this over on Stack Overflow and it was recommended I post it here. The statement on line 21 is used to ensure that after each outer cycle, the next output is displayed on a new line, thus producing a square shape as shown in the sample output above.

To learn more, see our. Furthermore, the sum of each row, column and diagonal must be the same. But I'm just learning 2d arrays. As explained previously, lines 12-14 read the integers and keep them in the square array. Does your code gracefully handle the following input? Then, when i becomes 1, the inner loop will start again with j equals to 0.

A magic square is an nxn i. Here is an example where the magic number is 170. The square told the people how big their sacrifice needed to be in order to save themselves. Repeat the process until all the squares are filled. But i will retype, if you can see it crooked that means my professor will pull out a piece of and measure the code alignment on the screen. Then I add the two diagonals.

Keep it short if sum! And again, we use the same method as before to cycle through every cell in the array. } This is just the high-level code. I'd have expected something more like MagicSquare or even SquareUtils. This programming exercise is concerned with creating odd sized magic squares i. So with the current input, the statements on lines 41-49, 54-60, and 65-72 are skipped.

The same goes for the sum of each column, 2+9+4, 7+5+3, and 6+1+8 , and each diagonal 2+5+8 and 6+5+4. Therefore, when the value is checked on line 40, the condition of the statement is true. Argument 1 ; subtype Constants is Natural range 1. When you use the code, move the srand function outside of the while loop but remains in the int main loop. Nothing seemed to work until a child noticed a turtle sporting a magic square on its back that kept circling the sacrifice.

However, this time, we add a condition on line 56 to ensure that only the numbers of particular cells will be considered. The same process goes on until both iand j reached the value of 2. Get answers and train to solve all your tech problems - anytime, anywhere. B: memset function include in cstring header file. List transpose , unfoldr , intercalate import Data.

Remember that we set the initial value of magic to true, and so far this value has not changed. To find where to place the next number, move diagonally upwards to the right i. If they are the same, this means that the square is a magic one. As the algorithm to use actually is described in the question I would like to point out a few issues. Therefore, line 59 is skipped.