
Comments and Discussions



I am looking at the code and I see a header file and a .cpp file but where is the main file?





Hello people. I developed in C a magical square of uneven dimension (3*3). The program do the following functions:
1. It constructs at least two different Magical Squares with the uneven dimension chosen (3*3)
2. It verifies if a square constructed for the program or indicated by the user is magical; However, the program is not complete and is necessary to allow the user to play the Magical Square with three degrees of difficulty. I don't know to do this last function. Please, help me to finish my program!





I have to complete a magic square for odd numbers. The size of the array is found from the end user. My main problem is printing out the corresponding number in their correct matrix form. Here is what I have so far...Thankx
#include
#include
using namespace std;
const int MAX_SIZE = 29;
typedef int Matrix[MAX_SIZE][MAX_SIZE];
//Function Prototypes
void Clear_Matrix(Matrix);
void Print_Matrix(Matrix, int);
//Function Definitions
void Clear_Matrix(Matrix M)
{
for(int i = 0; i < MAX_SIZE; i++)
{
for(int j = 0; j < MAX_SIZE; j++)
M[i][j] = 0;
}
}
void Print_Matrix(Matrix M, int sz)
{
for(int i = 0; i < sz; i++)
for(int j = 0; j < sz; j++)
cout << M[i][j] <<endl;
}
//Main Statement
int main()
{
//Declared Variables
Matrix Magic_Square;
int n;
int nrow;
int ncol;
bool odd = true;
char count = 'Y';
//Sentinel Loop
while ((count == 'Y') && odd)
{
cout << " Enter the size of the Magic Square (value should be a positive ODD number) ";
cin >> n;
if (n <= 0 && n % 2 == 1)
{
odd = false;
}
else
{
Clear_Matrix(Magic_Square);
int crow = 0;
int ccol = n / 2 + 1; // start position
Magic_Square[crow][ccol] = 1;
for (int i = 2; i <= n * n; i++)
{
nrow = crow  1;
ncol = ccol + 1;
if((crow  1) < 0)
nrow = n  1;
if(ncol >= n)
ncol = 0;
//checking for occupied numbers
if(Magic_Square[nrow][ncol] != 0)
{
nrow = crow + 1;
ncol = ccol;
}
Magic_Square[nrow][ncol] = i;
crow = nrow;
ccol = ncol;
count++;
}
Print_Matrix(Magic_Square, n);
cout <<endl;
}
cout << " Do you want to do it again (Y/N)?? ";
cin >> count;
}
count++;
return 0;
}
 modified at 20:25 Monday 11th June, 2007





A Magic Square is a matrix with the same number of rows and columns; where rows, columns and diagonals all sum up to the same number called the Magic Number.
Example:
6 7 2 15
1 5 9 15
8 3 4 15
15 15 15 15 15
Write a C++ program that fills random squares of 3 by 3, with unique numbers from 1 to 9, and tests if the generated matrix forms a Magic Squares.
Your program should loop until three magic squares are generated and printed to the screen.
Finally before exiting, the program should print the number of tries it took to achieve the task.
________________________________________
Note:
• Modular programming technique should be used. Thus your program should be divided into 3, 4 or more functions. (Some grades will be allocated to the efficient choice of functions)
• Your are not allowed to use global variables… Communication between the function and the caller should be done through parameters passing.
Nizar Hawat
Secretary & Archive
Depart. IEEENDU Lebanon





dear Sir,
I've got a problem about matrix operation which involve, character, polynomial, and double data format, in C++.
For example;
if we given
A= 0 1 0; 3*3marix B= 0; 3*1matrix
0 0 1; 0;
1 5 6; 1;
1.how to create program to solve
detsIA+BX which yield result =[s 1 0 ] [0] 
[0 s 1 ] + [0]*[x1 x2 x3] 
[1 5 s+6] [1] 
= s 1 0 
 0 s 1 
1+x1 5+x2 s+6+x3 
=s^3+(6+x3)s^2+(5+x2)s+1+x1;
and for addition if we have an equation before ex:s^3+(14)s^2+(60)s+200;
we can get 6+x3=14; 5+x2=60; 1+x1=200;
x1=199; x2=55; x3=8;
X=[ 199 55 8 ];
at last we got new variable: a1=6; a2=5; a3=1;
2.how to create a new matrix M which its components are
M=[ B  AB  (A^2)B ]
so we get
M=[ 0  0  1
0  1  6
1  6  31 ] 3*3matrix
thanx for your attention and understanding, sorry if there are too much for the problem and question
you can also reply me at cux_cux_23@yahoo.com for suggestions
Sincerely yours,
thanx soo much
OK!!





I really am fascinated with this code, however I am new to coding, and I was wondering if you could break down the steps for me into an algorithm. Maybe that will make it easier to follow all fo the steps.
Thank you very much!
Shay





Has anyone actually read this article? Note this line:
A. Riazi writes:
It is not hard to show that this sum must be n [ ( n2 + 1) / 2 ]. If we use this formula for that example output which is below, for [5x5] matrix; 5 [ ( 52 + 1 ) / 2 ] = 65.
So if we take the first expression: n[(n^2 + 1) / 2]. Now the next expression lookes like it puts 5 in for n. 5[(n^2 + 1)/2]. Did anyone bother to notice that 52 does not equal 5^2? Beyond that, 5[(52 + 1) / 2] = 65. 52+1 = 53. 53/2 = 26.5. 26.5 * 5 = way more than 65.
I don't know, his code may be right, but I stopped reading after this obvious math error. In fact, the square root of 52 is 7.21110255092..., so n really should equal 7.21110255092... A. Riazi, it is important to pay attention to your details. Make sure your math equations are right. If this is one of those equations that really isn't a math equation, that should be noted when written.
Danny





The fact is I must type 5 [ (5^2 + 1) / 2]=65 but unfortunately typed 5 [ (52 + 1) / 2]=65!
A. Riazi





Thank you. That confused the heck out of me and I couldn't read anymore.
Danny





i just want to ask how if i want to enter a more number in the program in the computer was say that the number i enter is not accurate in the program of magic square because is it not equal to the total sum of each enter ?





Hi Guyz,
I am currently working on program that will generate a complete set of solutions. So for a 4x4 square, a set 880 results will be generated, for squares larger than that, a whole lot more will be generated 8) . Due to the NP nature of this problem, I plan to distribute the work. Does anyone have any recommendations for how the work should be divided and/or the communications structure to use? I had written it in VB6, but due to the known memory leak and the increased efficiency of C++, I am rewriting it. I will probably use Visual Studio.Net for the distributed version instead of gcc/g++ which I am currently using for the single machine version.
Thanks In Advance
Joe





Your so called "complete work" is incomplete. Go back to Iran!
F





You are the one who should get out of here.
Sonork 100.41263:Anthony_Yio





And you are an anonymous coward.





Fyou, Anonymous!
Forum Magic Square
Subject: Re: Your Complete Work
Sender: Anonymous
Date: 20:55 15 Nov '03
Your so called "complete work" is incomplete. Go back to Iran!
F





vector<vector<int> > matrix(n, vector<int< (n, 0));
More specifically:
vector<int< (n, 0)
Thanks.
William
Fortes in fide et opere!





WREY wrote: vector<int< (n, 0)
That's easy: "vector" is smaller than "int", and "int" is smaller than, well, never mind





it must be:
"a N size of vectors of N size or integers prefilled with the 0 value"
let's say... a NxN matrix of int's where the default cell value is "0".
Nice no? a easy way to define a matrix in STL





No, I don't think so.
I believe the author made a typo some place, because if you look at his article, it has:
vector<vector<int> > matrix(n, vector<int> (n, 0));
which is different than what the program has. The program shows the second enclosing angle bracket turned around like it was a "less than" sign.
vector<int< (n, 0)
The thing is, either way that angle bracket is turned, it compiles and runs without error (which doesn't seem right).
William
Fortes in fide et opere!





WREY wrote:
vector<vector<int> > matrix(n, vector<int> (n, 0));
It produces a matrix with n rows and n columns. in other hand nxn matrix!
WREY wrote:
vector<int> (n, 0)
It's simple. Create vector of integers with n initial elements and zero value for any elements.
Combining them, makes nxn zero matrix.
A. Riazi





Razy,
William was saying another thing... in your code (in the zip file) the vector declaration is different from the code posted in the article, and it's mistaken.
In your code, you wrote a declaration like:
vector<int< (n, 0)
instead of
vector<int> (n, 0)
The VC6 compiles without problems, VC7 reports the error.






check line 21 and 105 of your magicsquare.cpp file, better if you get the one you post in Codeproject.





I change both lines and upload new demo project.
A. Riazi





Your program produces the exact same magic square each time it is run, making one think that it is unique. The user should be able to specify the row and column of the first '1' or it should be selected randomly. I was playing around with it this morning, and the following seems to work for odd 'n'.
void OddMagicSquare(vector > &matrix, int n)
{
srand((unsigned)time(NULL));
int nRow = rand()%n;
int nCol = rand()%n;
int nSqr = n * n;
int nIndex = 0;
while (nIndex < nSqr)
{
nIndex++;
matrix[nRow][nCol] = nIndex;
if (matrix[(nRow1 < 0) ? n1 : nRow1][(nCol+1 == n) ? 0 : nCol+1] == 0)
{
nRow = (nRow1 < 0) ? n1 : nRow1;
nCol = (nCol+1 == n) ? 0 : nCol+1;
}
else
{
nRow = (nRow+1 == n) ? 0 : nRow+1;
}
}
}





Sorry, but my code doesn't actually create a magic square every time (the main diagonals don't have the correct sum). I based it on the line "It begins by placing a 1 in any location..." in the description of the Siamese method, but it now seems that the location of the initial 1 must be (0, n/2). So, my question now is whether or not there is only one magic square per n?





It looks like you can move the first block, where you place 1, k blocks down and left or up and right (with wrapping), just the same way you use for the following numbers. After k=5 moves, the 1 is back to the initial position (0, n/2), so it looks like there are n different magic squares.
I'm not a math guru, it's just an observation from the flash animation. I expect it to be wrong.
Paolo

Why spend 2 minutes doing it by hand when you can spend all night plus most of the following day writing a system to do it for you?  (Chris Maunder)





There are other methods of making magic squares that produce different patterns. If you browse the Web you can find lots of information





All of stuff that you see on the net is for odd magic squares. I did not see any algorithm on the net that can cover even squares.
A. Riazi





... when our math prof was driving us crazy with the square.









General News Suggestion Question Bug Answer Joke Rant Admin Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

