A collection of tricks, thoughts, ideas and solutions from a graphics programmer. This blog contains my experiences, tips and tricks, everyday problems and their solutions. This blog serves not only as my reference but also for the whole world at large.
Saturday, October 25, 2008
Odd Magic Square NxN C++ code
The blogger interface is stupid it keeps formatting my input so I am attaching the whole cpp file. Get the C++ Code. Ofcourse rename the file remove the .zip extension
Posted by
MMMovania
Magic Squares Algorithm & C++ implementation
Hi all,
This time around its something a bit tricky generating an odd magic square. Details are given everywhere. The wiki entry is sufficient, check it out http://en.wikipedia.org/wiki/Magic_square.
I base on the same logic which is commonly followed the one up one right stepping strategy. Here is the pseudocode,
This time around its something a bit tricky generating an odd magic square. Details are given everywhere. The wiki entry is sufficient, check it out http://en.wikipedia.org/wiki/Magic_square.
I base on the same logic which is commonly followed the one up one right stepping strategy. Here is the pseudocode,
//n is the dimension of magic square for a 3*3 square n=3;
int magic[n][n]={0};
curRow=0, curCol=1;
oldRow=0, oldCol=0;
count =0;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
//See if we are into an empty cell
if( magic[curRow][curCol]==0 )
{
magic[curRow][curCol] = ++count;
}
else
{
//Move down from the previous row but keep the same column
magic[(oldRow+1)%n][oldCol] = ++count;
curRow = (oldRow+1)%n;
curCol = oldCol;
}
//Store the last row and column index
oldRow=curRow;
oldCol=curCol;
//Move one up and one right
curRow=(curRow-1)%n;
curCol=(curCol+1)%n;
//Wrap around the size
if(curRow<0)
curRow =n-1;
}
}
Posted by
MMMovania
Sunday, October 19, 2008
Subscribe to:
Posts (Atom)
Popular Posts
-
In this tutorial, I will show you how to create a simple box at a specific position and let it bounce under influence of gravity. We will...
-
Recently, for one of my projects, I had to learn about skeletal animation and GPU skinning. As always, I first went to google which gave me...
-
There has been a major revamp of the PhysX API from version 3. I will try to convert all of the existing tutorials into PhysX3 so here I go ...
-
I went to see the power of the NVIDIA PhysX sdk. So I downloaded the sdk from the PhysX developer site. While the sdk contains a lot of de...
-
In this tutorial, I will show you how to create a simple box at a specific position and let it bounce under influence of gravity. We will be...
Copyright (C) 2011 - Movania Muhammad Mobeen. Awesome Inc. theme. Powered by Blogger.