this is a magic square generator, but do not know C++, I have some difficulties to convert this code:
#include <vector>
#include <iostream>
using namespace std;
//There two series will be on even in case of magic square
// One of even order will be for multiple of 4
void BuildDoublyEvenMagicSquare(vector<vector<int> > &mat, int Order);
//Other of even order will be for multiple of 2
void SinglyEvenMagicSquare(vector<vector<int> > &mat, int order);
// For odd order
void BuildOddMagicSquare(vector<vector<int> > &mat, int Order);
// For odd order
void BuildOddMagicSquare(vector<vector<int> > &mat, int Order)
{
int SqrOfOrder = Order * Order;
int start=0, mid=Order/2; // start position
for (int loop=1; loop<=SqrOfOrder; ++loop)
{
mat[start--][mid++] = loop;
if (loop % Order == 0)
{
start += 2;
--mid;
}
else
{
if (mid==Order)
mid -= Order;
else if (start<0)
start += Order;
}
}
}
void BuildDoublyEvenMagicSquare(vector<vector<int> > &mat, int Order)
{
vector<vector<int> > A(Order, vector<int> (Order, 0));
vector<vector<int> > B(Order, vector<int> (Order, 0));
int i, j;
//Building of matrixes I and J
int index=1;
for (i=0; i<Order; i++)
for (j=0; j<Order; j++)
{
A[i][j]=((i+1)%4)/2;
B[j][i]=((i+1)%4)/2;
mat[i][j]=index;
index++;
}
for (i=0; i<Order; i++)
for (j=0; j<Order; j++)
{
if (A[i][j]==B[i][j])
mat[i][j]=Order*Order+1-mat[i][j];
}
}
void BuildSinglyEvenMagicSquare(vector<vector<int> > &mat, int order)
{
int ho=order/2;
vector<vector<int> > C(ho, vector<int> (ho, 0));
// For Order is Odd
if (order%2==1)
BuildOddMagicSquare(C, order);
// For Order is Even
else
{
//For Order is Doubly Even Order
if (order % 4==0)
BuildDoublyEvenMagicSquare(C, order);
//For Order is Singly Even Order
else
BuildSinglyEvenMagicSquare(C, order);
}
int i, j, k;
for (i=0; i<ho; i++)
for (j=0; j<ho; j++)
{
mat[i][j]=C[i][j];
mat[i+ho][j]=C[i][j]+3*ho*ho;
mat[i][j+ho]=C[i][j]+2*ho*ho;
mat[i+ho][j+ho]=C[i][j]+ho*ho;
}
if (order==2)
return;
vector<int> A(ho, 0);
vector<int> B;
for (i=0; i<ho; i++)
A[i]=i+1;
k=(order-2)/4;
for (i=1; i<=k; i++)
B.push_back(i);
for (i=order-k+2; i<=order; i++)
B.push_back(i);
int temp;
for (i=1; i<=ho; i++)
for (j=1; j<=B.size(); j++)
{
temp=mat[i-1][B[j-1]-1];
mat[i-1][B[j-1]-1]=mat[i+ho-1][B[j-1]-1];
mat[i+ho-1][B[j-1]-1]=temp;
}
i=k;
j=0;
temp=mat[i][j]; mat[i][j]=mat[i+ho][j]; mat[i+ho][j]=temp;
j=i;
temp=mat[i+ho][j]; mat[i+ho][j]=mat[i][j]; mat[i][j]=temp;
}
int main()
{
int Order;
cout<<"Enter the order of square which you wanna: ";
cin>>Order;
vector<vector<int> > mat(Order, vector<int> (Order, 0));
// For order less than 3 is meaningless so printing error
if (Order<3)
{
cout<<" Order Of Square must be greater than 2";
return -1;
}
// For Order is Odd
if (Order%2==1)
BuildOddMagicSquare(mat, Order);
// For Order is Even
else
{
//For Order is Doubly Even Order
if (Order % 4==0)
BuildDoublyEvenMagicSquare(mat, Order);
//For Order is Singly Even Order
else
BuildSinglyEvenMagicSquare(mat, Order);
}
// Display Results
for (int i=0; i<Order; i++)
{
for (int j=0; j<Order; j++)
{
cout<< mat[i][j]<<" " ;
}
cout<<endl;
}
return 0;
}
for example, how can I write this function call in C?
void BuildDoublyEvenMagicSquare(vector<vector<int> > &mat, int Order);
and what vector<vector<int> > &mat
means?
@Omnifarious
can i use something like this?
int **mat:
*mat = (int **)malloc(sizeof(int*)*Order);
for (int i=0;i<Order;i++)
mat[i] = (int *)malloc(sizeof(int)*Order);