magic square

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • angaz
    New Member
    • Jul 2007
    • 6

    #1

    magic square

    do you have a program in C??that make a magic square??
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    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

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      Originally posted by JosAH
      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...
      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

      Comment

      • arnaudk
        Contributor
        • Sep 2007
        • 425

        #4
        Originally posted by JosAH
        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...

        Comment

        • arnaudk
          Contributor
          • Sep 2007
          • 425

          #5
          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

          • angaz
            New Member
            • Jul 2007
            • 6

            #6
            wow....nice monster code..hehehehe. ..
            thanks!....

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by nepomuk
              Phew Jos, that's really... It shows very well how...
              How what?

              kind regards,

              Jos

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by arnaudk
                You could have won a prize with that...
                I've always been way too modest to actively participate in the IOCCC ;-)

                kind regards,

                Jos

                Comment

                • Nepomuk
                  Recognized Expert Specialist
                  • Aug 2007
                  • 3111

                  #9
                  Originally posted by JosAH
                  How what?

                  kind regards,

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

                  Greetings,
                  Nepomuk

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by nepomuk
                    ...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 ;-)

                    Comment

                    • Nepomuk
                      Recognized Expert Specialist
                      • Aug 2007
                      • 3111

                      #11
                      Originally posted by JosAH
                      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,
                      [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

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by nepomuk
                        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,
                        [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]
                        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:

                        [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

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          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.

                          Comment

                          • Nepomuk
                            Recognized Expert Specialist
                            • Aug 2007
                            • 3111

                            #14
                            Originally posted by JosAH
                            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?
                            Originally posted by JosAH
                            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]
                            Admirable. Confusing, but admirable.
                            Originally posted by JosAH
                            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.
                            Originally posted by JosAH
                            2) terse but clear variable naming conventions applied consistently.
                            OK, it'll be lI then. Not pow.
                            Originally posted by JosAH
                            3) a better algorithm
                            I'm still learning... ^^
                            Originally posted by JosAH
                            4) no silly 'magic constants' except for zero.
                            I like magic constants! They are so wonderfully inexplicable!
                            Originally posted by JosAH
                            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 ;-)

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              Originally posted by nepomuk
                              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 ;-)

                              Comment

                              Working...