Add your own alternative version
Stats
286.3K views 3.3K downloads 36 bookmarked
Posted
8 Sep 2003

Comments and Discussions



I'm getting constant 0's in the black screen. Here's my code, I need so much help with this project, what do I need?
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
ofstream outfile;
ifstream infile;
int main(){
outfile.open("output.txt");
infile.open("numbers.txt");
int row, col, n = 5;
int MagicSquare[5][5], m = 0, s = m/2;
int ms = n * n; int classification; cout << "Enter order of the square: ";
while (!cin.eof()){
cin >> MagicSquare[5][5];
classification = MagicSquare[5][5];
MagicSquare[1][3] = 1;
if (classification % 2 == 0){
cout << "Please enter odd integers only: ";
}
for (int i = 1; i <= ms; i++){
MagicSquare[m][s] = i;
m++;
s;
}
for (int row = 0; row < n; row++){
for (int col = 0; col < n; col)
{
cout << MagicSquare[m][s];
}
cout << MagicSquare[m][s];
}
}
system("pause");
return 0;
}





thank you for doing such a program...
i got the output and now i thought of doing that program
i saw your program but it is somewhat complicated...
i have done it in a very very very simple manner...
any way thanks for giving me an idea for telling such a title name...
you please better try to write programs in a very easy and fast complication..
the program which you gave takes lot of compilation time and not fine..
thanks for giving idea...............................





what you've done is a good example of coding a wellknown algorithm. the code is reasonably concise.
the downside is that your program will only generate a limited number of magic squares, i.e, only those that can be generated using the known algorithms for odd, even and doublyeven orders.
now, the number of possible magic squares increases dramatically with order. for eg., order 4 has 880 possible magic squares, 5 has 275305224 and 6 has approximately 1.7745×10^19. so, there are many magic squares your program will never generate. i have written a program in java that generates a magic square for every order, without using the known algorithms (apart from certain bits without which it might take a billion years to finish).
my program produces results for orders upto 9 well under 1 second in each case. order 10 sometimes fails because of the random and bruteforce elements in my algorithm. i am working on improving its performance for doubledigit and greater orders. i'll post the code once i'm stuck for ideas.
it will be great if you can contribute to the algorithm with any insight you might have about the construction of magic squares. i will discuss my algorithm in greater detail if you're interested.







hi im doing a domino game so i need to know how i can design an algorithm for move the pieces (paint repaint) on the table
just give me an idea





salam
to ro khoda be dadam beresin man ye vahed barname nevisi daram ostademun behemun goft bayad algoritm va zaban c++ moraba jadooyi ro benevisid baram ersal konid man aslan nemidunam ke bayad chi benevisam mishe komakam konid??
inam gmailme: na.asghari@gmail.com
to ro khoda komakam konid man in vahed ro pas konam





#include
#define row 5
#define column
int main(void)
{
int n,i,j,k,I,J;
int table[5][5];
{
for (i=0; i<n; i++)
for (j=0; j<n; j++)
{
if (I[i][j]==J[i][j])
matrix[i][j]=n*n+1matrix[i][j];
}
}
}
void CalculateOddMagicSquare()
{
int 5;
int matrix[5][5];
int nsqr = n * n;
int i=0, j=n/2;
for (int k=1; k<=nsqr; ++k)
{
matrix[i][j] = k;
i;
j++;
if (k%n == 0)
{
i += 2;
j;
}
else
{
if (j==n)
j = n;
else if (i<0)
i += n;
}
}
}
void PrintMagicSquare([5][5])
{
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
printf(" %3d", matrix[i][j]);
printf("\n");
}
printf("\n\n");
}
That the program correct?that can output the correct 5x5 magic square?
help me please constructing an n x n magic square for any odd integer n. Place 1 in the middle of the top row. Then after integer k has been placed, move up one row and one column to the right to place the next integer k + 1, unless one of the following occurs:
• If a move takes you above the top row in the jth column, move to the bottom of the jth column and place k + 1 there.
• If a move takes you outside to the right of the square in the ith row, place k + 1 in the ith row at the left side.
• If a move takes you to an already filled square or if you move out of the square at the upper righthand corner, place k + 1 immediately below k.
TEACH ME 2 HOW CONSTRUCT 5X5 MAGIC SQUARE..THANKS A LOT AND MORE POWER..
IM NEEDING UR REPLY..





plzz..help me..can u make me a program in magic square using C Programming Language..plzz send it 2 me by email..





Can anyone give me a blue jay array source code about magic square with a
A. method createArithmetic
B. method matricize
C. method reverseDiagonal
D. method magicCheck
E. method printMatrix
The method written in parts A through E should be general enough to apply to an array of any size.
Anyone knows about this, plzz.. share it with me..
e mail me at mr.dongky@hotmail.com





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 Praise Rant Admin Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

