summaryrefslogtreecommitdiff
path: root/algorithms/magic.c
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2014-06-15 03:24:33 +0200
committerPatrick Simianer <p@simianer.de>2014-06-15 03:24:33 +0200
commitcf3a29feb5887344b6633ead1b4b6d5657a15a4b (patch)
treef1149508f7305a48dba0226699dfafdd68d81969 /algorithms/magic.c
parent5ddc763ab9953eebdaf78af4eb72288d7955b310 (diff)
old stuff: algorithms
Diffstat (limited to 'algorithms/magic.c')
-rw-r--r--algorithms/magic.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/algorithms/magic.c b/algorithms/magic.c
new file mode 100644
index 0000000..31e50ea
--- /dev/null
+++ b/algorithms/magic.c
@@ -0,0 +1,65 @@
+#include <stdio.h>
+
+
+/*
+ 1...25
+
+ 0 0 1 0 0
+ 0 0 0 0 0
+ 0 0 0 0 0
+ 0 0 0 0 0
+ 0 0 0 0 0
+*/
+void
+magic(unsigned int n)
+{
+ int a[n][n];
+ unsigned int i, j;
+
+ for(i=0; i<n; i+=1) {
+ for(j=0; j<n; j+=1) {
+ a[i][j] = 0;
+ }
+ }
+
+ int k;
+ int row = n - 1;
+ int col = n / 2;
+ for (k = 1; k <= n * n; ++k) {
+ a[row][col] = k;
+ if (k % n == 0) {
+ --row;
+ if (row < 0)
+ row = n - 1;
+ } else {
+ ++row;
+ if (row == n)
+ row = 0;
+ ++col;
+ if (col == n)
+ col = 0;
+ }
+ }
+
+ for(i=0; i<n; i+=1) {
+ for(j=0; j<n; j+=1) {
+ printf("\t%d\t ",a[i][j]);
+ }
+ printf("\n");
+ }
+}
+
+int main(void)
+{
+ int n = 3;
+
+ if(n%2==0) {
+ printf("n has to be odd\n");
+ exit(1);
+ }
+
+ magic(n);
+
+ return 0;
+}
+