Problem 1:

Magic Squares:

An n x n array, that is filled with integers 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 and each value in the array is unique.

16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1


Write a program that reads in n2 integer values from the user and tests whether they form a magic square when put into an n x n array. You need to test two features:

• does each of the numbers 1 to n2 occur in the user input?
• when the numbers are put into a square, are the sums of the rows, columns and diagonals equal to each other?

Input: n and n2 integer numbers: if the user enters 3, the array has 3 rows and 3 columns and the integers in the array range from 1 to 9. As data validation, be sure the input is in the correct range and that each value is entered only once. (All elements in the array must be unique).

Output: a printout of the n x n array with each row of data on a separate line and a message of whether it is a magic square or not.


That is the problem of my assignment. What I have so far is:

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
  int n;

  cout<< "Please enter an odd integer: ";
  cin>>n;

  int MagicSquare[n][n];

  int newRow,
  newCol;

  // Set the indices for the middle of the bottom i
  int i =0 ;
  int j= n / 2;

  // Fill each element of the array using the magic array
  for ( int value = 1; value <= n*n; value++ )
  {
     MagicSquare[i][j] = value;
     // Find the next cell, wrapping around if necessary.
     newRow = (i + 1) % n;
     newCol = (j + 1) % n;
     // If the cell is empty, remember those indices for the
     // next assignment.
     if ( MagicSquare[newRow][newCol] == 0 )
     {
        i = newRow;
        j = newCol;
     }
     elsen2 integer values 
     {
        // The cell was full. Use the cell above the previous one.
        i = (i - 1 + n) % n;
     }

  }


  for(int x=0; x<n; x++)
  {
     for(int y=0; y<n; y++)
         cout << MagicSquare[x][y]<<" ";
     cout << endl;
  }
}

The outcome of this is not like the numbers that the assignment have. Getting really frustrated! Please help if you know!!

Thank you!!!

Thank you. I need this program to be in c++ .... I was wondering if people know my errors, because I don't know my errors.. The outcome of my codes is junk numbers.

There is not one error mentioned in the original post other than a vague "output doesn't look right". Without any details, what can we say?

the magic constant is probably a standard 34 since the numbers are 1..16.
I just updated the solver and moved the solver code after the form (so it works better - form fields are loaded at time script is executed), and I added a function and button to call it which calculates the magic constant based on what is in the magic square.

the numbers of a magic square are supposed to add up to a magic constant. that magic constant is the sum of all the cells / 4.

you are welcome to use the code, since it is imperfect, unless it is for an assignment. in that case, use the properties list and design your own solution. I would have to see your code. and it's probably awful long like mine is, pages and pages with diagrams so you don't lose track of what code does what...

javascript is very close to C++ in most of its syntax. where you see it mostly differ is with creation of variables and variable types, definition of function, and how input and output is done. in my case, I am accessing form controls.

Thank you. I need this program to be in c++ .... I was wondering if people know my errors, because I don't know my errors.. The outcome of my codes is junk numbers.

  1. Of course you don't know your errors. In fact you don't seem to have understood a thing about what the programm needs to do (if this is your piece of code)
  2. The outcome is junk because the code is junk!

what was that? who are you aiming that comment at?

magic squares are a source of continual study and leisure. there is usually somebody coming up with new patterns for various sizes. there is a google group (newsgroup) dedicated to this subject.

completely "solving" for a magic square is not a fully defined task. you can find different magic square properties on different web sites (and then there's the newsgroup). I suppose this is finite, but you wonder sometimes.

the task of solving for one instance of a pattern as a check of validity would usually be adding up the numbers in that pattern and seeing if they add up to the magic constant.

I am not sure about whether this is a true property or not, but I found some interesting dependent symmetry patterns (2 patterns whose sums are not the magic constant but depend upon one another, but no matter what numbers you put in, the symmetries are true). I am not sure if the magic square folk would accept this however, but it was interesting to find. I did it for the challenge and because I thought I could do this with a program.

Edited 5 Years Ago by jmichae3: n/a

If you want to see the javascript code, then do a view source on the web page.

Edited 5 Years Ago by jmichae3: n/a

This article has been dead for over six months. Start a new discussion instead.