Hi,
The following is the program to print magic squares.I`m getting
wierd output when i remove the printf statement "printf("a")" from the
code. The required output is obtained by simply substituting that by
putchar('\0'). I would be very glad if someone can let me know.
Please kindly help me regarding this.
Expecting an early rep,
Srujan.K.V.S
************************************************************************
//This is a simple program to design magic squares.
//A magic square is an arangment of numbers of numbers from 1 to n^2 such
// that any row, diagonal or column sums to the same. This program wil
//work for only odd sized squares
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
main() {
int n,x=1,i,j,k,p,r,t;
scanf("%d",&n);
printf("%d\n",n);
p=n*n;
int a[n][n];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
a[i][j]=0;
i=0;j=(n-1)/2;
a[i][j]=1;// starting 1 int the first row central column. :)
//We try to put the next number in the top left diagonal square
//from the recent numbered square.
//If we have already numbered , we then number the square just
//below it.
for(k=1;k<p;k++)
{
if((i-1)<0&&j!=0&&a[n-1][j-1]==0) i=n;
if((j-1)<0&&i!=0&&a[i-1][n-1]==0) j=n;
if(i==0&&j==0&&a[n-1][n-1]==0)
{
i=n;j=n;
}
x++;
if(a[i-1][j-1]==0)
{
//The following printf statement if removed is
alering the output when n=5
printf("a");
a[i-1][j-1]=x;
i=i-1;
j=j-1;
}
else
{
if((i+1)>(n-1)) i=-1;
a[i+1][j]=x;
i=i+1;
j=j;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%4d \t",a[i][j]);
printf("\n");
}
}
*****************************************************************************