Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

magic square

Question posted by: angaz (Newbie) on July 23rd, 2008 11:41 AM
do you have a program in C??that make a magic square??
JosAH's Avatar
JosAH
Chief Editor
7,786 Posts
July 23rd, 2008
12:32 PM
#2

Re: magic square
Years ago I wrote the following little monster and used it as my .signature. It prints
a magic square of odd size and it doesn't use any memory proportional to the size
of the square. Have fun with it.

Expand|Select|Wrap|Line Numbers
  1. main(o,O0)char**O0;{int OO,O;O=--o?atoi(O0[!0]):!
  2. 0;for(o=((OO=O*O)-O+!0+!0)>>!0;OO;o+=((--OO%O)?-!
  3. 0:((((o-!0)%O)?O:0)+!0))-(((o-!0)%O)?O:0)){printf
  4. ("\n%*d "+!!(OO%O),!0<<!0<<!0,o+=(o<!0)?O*O:0);}}


kind regards,

Jos

Reply
Nepomuk's Avatar
Nepomuk
Moderator
1,493 Posts
July 23rd, 2008
12:48 PM
#3

Re: magic square
Quote:
Years ago I wrote the following little monster and used it as my .signature. It prints
a magic square of odd size and it doesn't use any memory proportional to the size
of the square. Have fun with it.

Expand|Select|Wrap|Line Numbers
  1. main(o,O0)char**O0;{int OO,O;O=--o?atoi(O0[!0]):!
  2. 0;for(o=((OO=O*O)-O+!0+!0)>>!0;OO;o+=((--OO%O)?-!
  3. 0:((((o-!0)%O)?O:0)+!0))-(((o-!0)%O)?O:0)){printf
  4. ("\n%*d "+!!(OO%O),!0<<!0<<!0,o+=(o<!0)?O*O:0);}}


kind regards,

Jos

Phew Jos, that's really... It shows very well how...
It's just awful code! Absolutely awful! (At least for someone trying to understand it.) Well done! :-D

But wait a minute, isn't that spoon feeding code? ;-)

Greetings,
Nepomuk

Reply
arnaudk's Avatar
arnaudk
Needs Regular Fix
330 Posts
July 23rd, 2008
12:49 PM
#4

Re: magic square
Quote:
Years ago I wrote the following little monster and used it as my .signature. It prints
a magic square of odd size and it doesn't use any memory proportional to the size
of the square. Have fun with it.

kind regards,

Jos

You could have won a prize with that...

Reply
arnaudk's Avatar
arnaudk
Needs Regular Fix
330 Posts
July 23rd, 2008
12:58 PM
#5

Re: magic square
One of my favourite 2001 entries: believe it or not, this program is a self-contained speech synthesizer that tells the current time through the computer's sound card - just compile and run, redirecting the output to /dev/audio, and here is a flight simulator program, in the shape of an aeroplane...

Reply
angaz's Avatar
angaz
Newbie
6 Posts
July 23rd, 2008
01:42 PM
#6

Re: magic square
wow....nice monster code..hehehehe...
thanks!....

Reply
JosAH's Avatar
JosAH
Chief Editor
7,786 Posts
July 23rd, 2008
05:06 PM
#7

Re: magic square
Quote:
Phew Jos, that's really... It shows very well how...


How what?

kind regards,

Jos

Reply
JosAH's Avatar
JosAH
Chief Editor
7,786 Posts
July 23rd, 2008
05:08 PM
#8

Re: magic square
Quote:
You could have won a prize with that...


I've always been way too modest to actively participate in the IOCCC ;-)

kind regards,

Jos

Reply
Nepomuk's Avatar
Nepomuk
Moderator
1,493 Posts
July 23rd, 2008
05:20 PM
#9

Re: magic square
Quote:
How what?

kind regards,

Jos
...how ugly code can be, even if it's yours? :-D

Greetings,
Nepomuk

Reply
JosAH's Avatar
JosAH
Chief Editor
7,786 Posts
July 23rd, 2008
05:37 PM
#10

Re: magic square
Quote:
...how ugly code can be, even if it's yours? :-D


I do beg your pardon? Ugly? Young whippersnapper please note that this little
gem, this proof of excellence, clearly shows:

1) a consistent and pleasing use of white space and empty lines;
2) a consistent variable naming convention;
3) intelligent terseness of code;
4) clever use of a necessary understanding of a bit of mathematics;
5) code that is completely conforming to K&R1;
6) awareness of our precious rain forests.

kind regards,

Jos ;-)

Reply
Nepomuk's Avatar
Nepomuk
Moderator
1,493 Posts
July 23rd, 2008
06:31 PM
#11

Re: magic square
Quote:
I do beg your pardon? Ugly? Young whippersnapper please note that this little
gem, this proof of excellence, clearly shows:

1) a consistent and pleasing use of white space and empty lines;
2) a consistent variable naming convention;
3) intelligent terseness of code;
4) clever use of a necessary understanding of a bit of mathematics;
5) code that is completely conforming to K&R1;
6) awareness of our precious rain forests.

kind regards,

Jos ;-)
Jos, you're right. From now on, I'll write all of my code in the same way. Oh, and you'll be my Tech support. :-P

Greetings,
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int pow(int,int);int main(){printf("%c%c%c%c%c%c%c",0x04E,'E'+32,0x38*2,pow(2,7)-(pow(2,pow(2,2))+pow(121,0)),230-0xB*013,021*5+0x20,(pow(7,2)+4)*2+pow(1,1));}int pow(int a,int b){return((b==0?1:(b==1?a:a*pow(a,--b))));}

Reply
JosAH's Avatar
JosAH
Chief Editor
7,786 Posts
July 24th, 2008
09:36 AM
#12

Re: magic square
Quote:
Jos, you're right. From now on, I'll write all of my code in the same way. Oh, and you'll be my Tech support. :-P

Greetings,
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int pow(int,int);int main(){printf("%c%c%c%c%c%c%c",0x04E,'E'+32,0x38*2,pow(2,7)-(pow(2,pow(2,2))+pow(121,0)),230-0xB*013,021*5+0x20,(pow(7,2)+4)*2+pow(1,1));}int pow(int a,int b){return((b==0?1:(b==1?a:a*pow(a,--b))));}


Nice try (for a beginner) grasshopper ;-) But that pow function is a giveaway.
First, don't call that thing 'pow' but something like 'o0' and apply a better algorithm.
Study this one:

Expand|Select|Wrap|Line Numbers
  1. int o0(int O,int o){int O0;return!o?!0:(O0=o0(O, o>>!0),O0*O0*(o&!0?O:!0));}


Again:

1) there are no unnecessary spaces nor parentheses because they slow things
down during runtime.
2) terse but clear variable naming conventions applied consistently.
3) a better algorithm
4) no silly 'magic constants' except for zero.

and most important (this is a 'real programmers rule'):

5) if it were hard to write it should be hard to read.

kind regards,

Jos ;-)

Reply
r035198x's Avatar
r035198x
Administrator
10,754 Posts
July 24th, 2008
09:58 AM
#13

Re: magic square
After good having a good giggle ...
@OP
Do not ask for programs or code. Rather ask for help on how to write your own programs. For an odd N magic square, use de la Loubere's algorithm which can be expressed as :
  • Begin by placing a 1 in the middle location of the top row:
  • Now put successive integers in an upward-right diagonal path.
    • If the upward-right movement results in a location outside the boundaries of the square, place the new number at the opposite end of the row or column that would contain the new number, if the rows and columns were not bounded.
    • If the upward-right square is already occupied, place the new number directly below the current one.
Convert that to pseudo code first then translate it into a C program.

P.S And yes you can ignore most of the posts in this thread ... for now. Come back to them after you're done with your version.

Reply
Nepomuk's Avatar
Nepomuk
Moderator
1,493 Posts
July 24th, 2008
10:25 AM
#14

Re: magic square
Quote:
Nice try (for a beginner) grasshopper ;-) But that pow function is a giveaway.
First, don't call that thing 'pow' but something like 'o0' and apply a better algorithm.
Hey, I just wanted to write my name! ;-) (By the way, it does. Check it, if you want to.)
But about the name - wouldn't something like Il versus lI be better for this purpose?
Quote:
Study this one:

Expand|Select|Wrap|Line Numbers
  1. int o0(int O,int o){int O0;return!o?!0:(O0=o0(O, o>>!0),O0*O0*(o&!0?O:!0));}
Admirable. Confusing, but admirable.
Quote:
Again:

1) there are no unnecessary spaces nor parentheses because they slow things down during runtime.
Do I have any unneeded spaces or parentheses in there? OK, I can see one pair of extra brackets.
Quote:
2) terse but clear variable naming conventions applied consistently.
OK, it'll be lI then. Not pow.
Quote:
3) a better algorithm
I'm still learning... ^^
Quote:
4) no silly 'magic constants' except for zero.
I like magic constants! They are so wonderfully inexplicable!
Quote:
and most important (this is a 'real programmers rule'):

5) if it were hard to write it should be hard to read.
I kneel before you, begging for forgiveness. I will make my code more difficult to read, I promise. ;-)

Greetings,
He, who is not worthy to be allowed to write his name here any longer ;-)

Reply
JosAH's Avatar
JosAH
Chief Editor
7,786 Posts
July 24th, 2008
04:43 PM
#15

Re: magic square
Quote:
I kneel before you, begging for forgiveness. I will make my code more difficult to read, I promise. ;-)


Good; another one down ;-) and remember: always make your code as hard to
read as possible. Give them maintenance programmers something to chew on
and never, never document your magic code with more than a single line "the
above is trivial and won't be discussed any further".

On the other hand do elaborate on trivial things such as "i= 0;" and spend at
least a couple of lines (or pages) on what important things had just happened.

Always rely on the darkest corners of a language standard only and refuse to
touch any keyboard if a particular compiler implementation doesn't conform with
that darkest corner.

Whenever possible only keep encrypted versions of your source code and throw
the source code of your decrypter away (if you ever wrote any). That is called:
source code protection. Preferably use EBCDIC.

Manually obfuscate your code: name your variables such that they have nothing
to do with what others expect; "sausage" and "fronobulax" are fine names. "foo"
and "bar" don't make it anymore nowadays. "O0" (Oh-zero) and "l1" (lower case
ell and 1) are fine though especially when use in logical expressions. Alway
prepend any expression with at least 128 white space characters or the same
amount of tabs or use no white space at all.

kind regards,

Jos (<--- real programmer ;-)

Reply
Savage's Avatar
Savage
Expert
1,684 Posts
July 24th, 2008
08:05 PM
#16

Re: magic square
Quote:
Good; another one down ;-) and remember: always make your code as hard to
read as possible. Give them maintenance programmers something to chew on
and never, never document your magic code with more than a single line "the
above is trivial and won't be discussed any further".

On the other hand do elaborate on trivial things such as "i= 0;" and spend at
least a couple of lines (or pages) on what important things had just happened.

Always rely on the darkest corners of a language standard only and refuse to
touch any keyboard if a particular compiler implementation doesn't conform with
that darkest corner.

Whenever possible only keep encrypted versions of your source code and throw
the source code of your decrypter away (if you ever wrote any). That is called:
source code protection. Preferably use EBCDIC.

Manually obfuscate your code: name your variables such that they have nothing
to do with what others expect; "sausage" and "fronobulax" are fine names. "foo"
and "bar" don't make it anymore nowadays. "O0" (Oh-zero) and "l1" (lower case
ell and 1) are fine though especially when use in logical expressions. Alway
prepend any expression with at least 128 white space characters or the same
amount of tabs or use no white space at all.

kind regards,

Jos (<--- real programmer ;-)


And get fired before u even know it. :D

PS:You could write a book,something like:"Awful code revealed" or "Teach your self awful coding in 7 days"

Reply
Reply
Not the answer you were looking for? Post your question . . .
190,078 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top C / C++ Forum Contributors