Magic Square with C++

Sabtu, 23 Juli 2011

This time we will create magic squares with applications using the programminglanguage C. Earlier, I'll explain what that magic squares;

Magic square is a symmetry matrix is the sum of all lines are equal, eitherhorizontal, vertical, or diagonal. For example, I will give examples of magicsquares with the order of 3x3.

8 1 6
3 5 7
4 9 2
To make the application, I have prepared a flowchart, scripts and sample results.This application can only be used for the order is odd, because the even do not know the plot. J

This flowchart:




Source Code :

#include

void main(){

static int i, j, a, b[20][20],x=1, input,c;

printf("masukkan jumlah ordo = "); scanf("%d", &input);

c = input*input;

j = (input/2);

for(i=0, a=1; a<=c; i--, j++, a++){

if(x == 0){

i+=2;

j-=1;

}

else if(i<0){ i += input; } else if(j>=input){

j -= input;

}

b[i][j] = a;

x = a%input;

}

for(i=0; i

for(j=0; j

printf("%d \t", b[i][j]);

printf("\n");

}

}


Output :


0 komentar:

Posting Komentar

 
 
 

Label

Label