do you have a program in C??that make a magic square??
magic square
Collapse
X
-
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.
[code=c]
main(o,O0)char* *O0;{int OO,O;O=--o?atoi(O0[!0]):!
0;for(o=((OO=O* O)-O+!0+!0)>>!0;OO ;o+=((--OO%O)?-!
0:((((o-!0)%O)?O:0)+!0) )-(((o-!0)%O)?O:0)){pr intf
("\n%*d "+!!(OO%O),!0<< !0<<!0,o+=(o<!0 )?O*O:0);}}
[/code]
kind regards,
Jos -
Phew Jos, that's really... It shows very well how...Originally posted by JosAHYears 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.
[code=c]
main(o,O0)char* *O0;{int OO,O;O=--o?atoi(O0[!0]):!
0;for(o=((OO=O* O)-O+!0+!0)>>!0;OO ;o+=((--OO%O)?-!
0:((((o-!0)%O)?O:0)+!0) )-(((o-!0)%O)?O:0)){pr intf
("\n%*d "+!!(OO%O),!0<< !0<<!0,o+=(o<!0 )?O*O:0);}}
[/code]
kind regards,
Jos
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,
NepomukComment
-
-
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...Comment
-
I do beg your pardon? Ugly? Young whippersnapper please note that this littleOriginally posted by nepomuk...how ugly code can be, even if it's yours? :-D
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 ;-)Comment
-
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. :-POriginally posted by JosAHI 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 ;-)
Greetings,
[code=c]#include <stdio.h>
int pow(int,int);in t main(){printf(" %c%c%c%c%c%c%c" ,0x04E,'E'+32,0 x38*2,pow(2,7)-(pow(2,pow(2,2) )+pow(121,0)),2 30-0xB*013,021*5+0 x20,(pow(7,2)+4 )*2+pow(1,1));} int pow(int a,int b){return((b==0 ?1:(b==1?a:a*po w(a,--b))));}[/code]Comment
-
Nice try (for a beginner) grasshopper ;-) But that pow function is a giveaway.Originally posted by nepomukJos, 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,
[code=c]#include <stdio.h>
int pow(int,int);in t main(){printf(" %c%c%c%c%c%c%c" ,0x04E,'E'+32,0 x38*2,pow(2,7)-(pow(2,pow(2,2) )+pow(121,0)),2 30-0xB*013,021*5+0 x20,(pow(7,2)+4 )*2+pow(1,1));} int pow(int a,int b){return((b==0 ?1:(b==1?a:a*po w(a,--b))));}[/code]
First, don't call that thing 'pow' but something like 'o0' and apply a better algorithm.
Study this one:
[code=c]
int o0(int O,int o){int O0;return!o?!0: (O0=o0(O, o>>!0),O0*O0*(o &!0?O:!0));}
[/code]
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 ;-)Comment
-
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.
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.Comment
-
Hey, I just wanted to write my name! ;-) (By the way, it does. Check it, if you want to.)Originally posted by JosAHNice 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.
But about the name - wouldn't something like Il versus lI be better for this purpose?
Admirable. Confusing, but admirable.Originally posted by JosAHStudy this one:
[code=c]
int o0(int O,int o){int O0;return!o?!0: (O0=o0(O, o>>!0),O0*O0*(o &!0?O:!0));}
[/code]
Do I have any unneeded spaces or parentheses in there? OK, I can see one pair of extra brackets.Originally posted by JosAHAgain:
1) there are no unnecessary spaces nor parentheses because they slow things down during runtime.
OK, it'll be lI then. Not pow.Originally posted by JosAH2) terse but clear variable naming conventions applied consistently.
I'm still learning... ^^Originally posted by JosAH3) a better algorithm
I like magic constants! They are so wonderfully inexplicable!Originally posted by JosAH4) no silly 'magic constants' except for zero.
I kneel before you, begging for forgiveness. I will make my code more difficult to read, I promise. ;-)Originally posted by JosAHand most important (this is a 'real programmers rule'):
5) if it were hard to write it should be hard to read.
Greetings,
He, who is not worthy to be allowed to write his name here any longer ;-)Comment
-
Good; another one down ;-) and remember: always make your code as hard toOriginally posted by nepomukI kneel before you, begging for forgiveness. I will make my code more difficult to read, I promise. ;-)
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 ;-)Comment
Comment