0% found this document useful (0 votes)
20 views

Magic Square

This document contains the code for generating magic squares of varying sizes. It includes functions to generate odd and even sized magic squares, output the generated magic square, and swap elements within the 2D array representing the magic square. The main body prompts the user for a size, generates the corresponding magic square using the odd/even functions, outputs it, and provides credits and comments at the end.

Uploaded by

Xiruki Têpë
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Magic Square

This document contains the code for generating magic squares of varying sizes. It includes functions to generate odd and even sized magic squares, output the generated magic square, and swap elements within the 2D array representing the magic square. The main body prompts the user for a size, generates the corresponding magic square using the odd/even functions, outputs it, and provides credits and comments at the end.

Uploaded by

Xiruki Têpë
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

MAGICSQUARE BY XIRU bwahahaha if(n<3){

In c++ printf("\n\nyou did not follow the instructions\!


Run the program again... BYE!"); break;
#define sized 160
}
#include <stdio.h> if(n>160) {
#include "stdlib.h" printf("\n\nthat's too much! grrrrr byee! "); break;
#include <conio.h> }}
while(1);
int m[sized][sized];
printf("\n\n* .. [[ < MAGIC SQUARE ROCKS > ]] .
void odd(int n); .. *\n\n\n");
void even(int n); getch();
void out(int n); return 0;
void swp(int i_1, int x1, int i_2, int x2); }
void odd(int n) {
int main(void) int i,j,num=1;
{ int nn=n*3/2;
int i,j,n,color;
char *s=" "; for(i=0; i < n; i++)
for(j=0; j < n; j++)
textattr(128 + 120); m[(j-i+nn)%n][(i*2-j+n)%n]=num++;
clrscr(); }
void even(int n) {
cprintf("{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ int i,j,num=1;
{{\n"); int n_min=n-1,nmid=n/2,nn=n*n+1;
cprintf("MAGIC SQUARE\!\! \n"); int osl=0;
int swtchrw[2];
cprintf("}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} int lasswitch_col;
}}\n"); int firs_blck=(n-2)/4,sec_blck=n_min-firs_blck;
int f1_ins=n/4,s2_ins=n_min-f1_ins;
while(1) {
printf("\n\n\n:-) Input a num(3-%d): (note: type -2 for(j=0; j < n; j++)
to exit) ",sized); for(i=0; i < n; i++) {
gotoxy(50,0);gets(s); if(i >= f1_ins && i <= s2_ins && j >= f1_ins
n=atoi(s); && j <= s2_ins)
if(n < 3 || n > sized) break; m[i][j]=num;
if(n%2) odd(n); else if((i > firs_blck && i < sec_blck) || (j >
else even(n); firs_blck && j < sec_blck))
out(n); } m[i][j]=nn-num;
do{ else m[i][j]=num;
if(n==-2) num++;
}
{printf("\n\n\n\n*****************bye********* if(!(n%4)) return;
********\n Have a nice day!"); break; }
swtchrw[0]=random(nmid-1)+firs_blck+1;
swtchrw[1]=random(nmid-1); gotoxy(25,0); printf("\n\nSUCCESS\! Press any
if(swtchrw[1] >= firs_blck) swtchrw[1]+=(nmid+1); button to view\! Type N to cancel");
lasswitch_col=random(nmid-1); ch=getch();
if(lasswitch_col >= firs_blck) if(ch=='n' || ch=='N') return;
lasswitch_col+=(nmid+1); gotoxy(50,0); printf("\n\n---
+--------------------------------------------------------------------
for(i=0; i < nmid; i++) { -------");
if(i==firs_blck || i==sec_blck) { for(j=0; j < n; j++) {
osl=1-osl; printf("\n\n\t\t\t ",j+1);
continue; for(i=0; i < n; i++)
} printf("|%4d",m[i][j]);
swp(sec_blck, i, sec_blck, n_min-i); printf("|\n__________________________________
swp(i, firs_blck, n_min-i, firs_blck); ___________________________________________
swp(i, sec_blck, n_min-i, sec_blck); __\n");
swp(i, swtchrw[osl], n_min-i, swtchrw[osl]); }
} gotoxy(50,0); printf("\n\n---
for(i=firs_blck+1; i < sec_blck; i++) { +--------------------------------------------------------------------
swp(firs_blck, i, sec_blck, i); -------");
swp(i, firs_blck, i, sec_blck); }
}
swp(firs_blck, nmid, sec_blck, nmid); void swp(int i_1, int x1, int i_2, int x2)
swp(lasswitch_col, firs_blck, lasswitch_col, {
sec_blck); int k;
}
k=m[i_1][x1];
void out(int n) m[i_1][x1]=m[i_2][x2];
{ m[i_2][x2]=k;
int i,j,ch,errorr=0;
unsigned long sum,sl,sc,sd1=0,sd2=0; }

sum=(unsigned long)n*(n*n+1)/2;
gotoxy(25,0); printf(";-) SUM = %lu. * ..check Credits:
sum... . . ",sum);
for(j=0; j < n; j++) { www.xiruki.co.nr
sd1+=m[j][j];
www.daniweb.com
sd2+=m[j][n-j-1];
sl=0;
sc=0;
for(i=0; i < n; i++) { BRAND X DOES NOT ALWAYS LOSE!
sl+=m[i][j];
sc+=m[j][i]; Anime haters shall perish
}
} Devilish xiru is a devil.
if(errorr) printf("\n\n* %d error\!",errorr); DECEIVER >:) bwahahahahahahahaahahhaha
else printf("- O.K. -");

You might also like