How can I convert my array matrix into dynamic? I know this code works in certain compilers but some wont do it so I want to converted it to dynamic , I used other methods like initializing the array by "looping" but it did not work at all. Right now my code only works if I use a number say 5 or 3 for n and it works well but I need to work from the users input.Thank you for your time!
#include <fstream>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
void CalculateOddMagicSquare(int n)
{
int matrix[n][n];
int nsqr = n * n;
int i=0, j=n/2; // start position
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;
}
}
for (int x = 0; x < n; x++) {
for (int y = 0; y < n; y++) {
cout << matrix[x][y] << " ";
}
cout << endl;
}
}
int main()
{
int n;
cin >> n;
CalculateOddMagicSquare(n);
return 0;
}
This post has been edited by osu1: 26 February 2013 - 08:05 PM

New Topic/Question
Reply



MultiQuote




|