//programa: magico.c
//descripcion: Imprime un cuadrado magico
//autor: Victor Reyes
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
  int k,a[20][20];
  int r,c,n,v,t;
  clrscr();
  randomize();
  textcolor(14);
  do {
   cprintf("Da un numero impar <3-19> ");
   scanf("%d",&n);
  }while( (n<3 || n>19) || (n%2==0) );
  clrscr();
  for(k=0;k<n;k++)
   for(v=0;v<n;v++)
    a[k][v]=0;
  r=0;
  c=n/2;
  a[r][c]=1;
  for(k=2;k<=n*n;k++)
  {
   t=r;
   r--;
   c++;
   if(r<0) r=n-1;
   if(c>(n-1)) c=0;
   if(a[r][c]==0) a[r][c]=k;
   else
    {
     c--;
     r=t+1;
     if(c<0) c=n-1;
     a[r][c]=k;
    }
  }
  for(k=0;k<n;k++)
  {
   for(v=0;v<n;v++)
   {
    t=random(14)+2;
    if(t==8) t++;
    textcolor(t);
    cprintf("%4d",a[k][v]);
   }
   printf("\n");
  }
  textcolor(137);
  gotoxy(35,23);
  cprintf("IT'S MAGIC!");
  getch();
}
