Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Opinions

Opinions

Scheduled Pinned Locked Moved The Weird and The Wonderful
data-structuresquestiondiscussionloungelearning
24 Posts 16 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S SirTimothy

    mmm... last week? maybe the week before?

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #13

    it sure has a modern je-ne-sais-quoi. :)

    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

    Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

    1 Reply Last reply
    0
    • S SirTimothy

      Hey folks, I don't generally post on these forums, but I do enjoy the CP newsletters. Anyways, I wanted to share a lovely snippet I wrote a little while ago. Maybe get some opinions? It's a bubble sort in C. It generates 20 random numbers from 0 to 99, and sorts them! Of course, it's fairly straightforward, so I'm sure you could guess that.

      #include <stdlib.h>
      #include <stdio.h>
      #include <time.h>

      #define ARRAY_SIZE 20

      #define PNT(s, l, a) fprintf(std##s, l, a)
      #define PNT2LINES(s, l1, l2, e) PNT(s, #l1#e#l2#e, 0)

      void print_array(int *array) {
      int x = ARRAY_SIZE;
      char* fmts[] = { "%d, ", "%d\n" };
      --array;
      LBL0:
      if (PNT(out, ((x == 1) & 1)[fmts], *(array = ++array)), --x > 0)
      goto LBL0;
      }

      main() {
      int iarray[ARRAY_SIZE];
      int x, y;
      int ofs = iarray - &ofs;
      void (*prnt_func)(int*) = &print_array;
      srand((unsigned int)time(NULL));
      x ^= x;
      LBL1:
      if ((y = rand(), x < ARRAY_SIZE) && ((((x = x + 1) - 1)[iarray] = (y - (y / 100) * 100)) || 1))
      goto LBL1;
      PNT2LINES(out, Before sort, ---------------, \n);
      (*prnt_func)(&ofs + ofs);
      LBL2:
      y = x &= ~x;
      LBL3:
      if (y + 1 < ARRAY_SIZE) {
      if ((++y, iarray[y] < (y-1)[iarray]) && (y[iarray] ^= iarray[y-1] ^= iarray[y] ^= *(iarray + y - 1), x |= y));
      goto LBL3;
      }
      if (x = x) goto LBL2;
      PNT2LINES(out, After sort, ---------------, \n);
      (*prnt_func)(&ofs + ofs);
      }

      D Offline
      D Offline
      dybs
      wrote on last edited by
      #14

      I personally liked the use of function pointers for a simple method call ;P

      The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen

      S 1 Reply Last reply
      0
      • D dybs

        I personally liked the use of function pointers for a simple method call ;P

        The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen

        S Offline
        S Offline
        SirTimothy
        wrote on last edited by
        #15

        That, of course, is so that I can change the function name, and only need to change one place where it's used. Gotta think about maintainability and future modifications!

        J 1 Reply Last reply
        0
        • S SirTimothy

          Hey folks, I don't generally post on these forums, but I do enjoy the CP newsletters. Anyways, I wanted to share a lovely snippet I wrote a little while ago. Maybe get some opinions? It's a bubble sort in C. It generates 20 random numbers from 0 to 99, and sorts them! Of course, it's fairly straightforward, so I'm sure you could guess that.

          #include <stdlib.h>
          #include <stdio.h>
          #include <time.h>

          #define ARRAY_SIZE 20

          #define PNT(s, l, a) fprintf(std##s, l, a)
          #define PNT2LINES(s, l1, l2, e) PNT(s, #l1#e#l2#e, 0)

          void print_array(int *array) {
          int x = ARRAY_SIZE;
          char* fmts[] = { "%d, ", "%d\n" };
          --array;
          LBL0:
          if (PNT(out, ((x == 1) & 1)[fmts], *(array = ++array)), --x > 0)
          goto LBL0;
          }

          main() {
          int iarray[ARRAY_SIZE];
          int x, y;
          int ofs = iarray - &ofs;
          void (*prnt_func)(int*) = &print_array;
          srand((unsigned int)time(NULL));
          x ^= x;
          LBL1:
          if ((y = rand(), x < ARRAY_SIZE) && ((((x = x + 1) - 1)[iarray] = (y - (y / 100) * 100)) || 1))
          goto LBL1;
          PNT2LINES(out, Before sort, ---------------, \n);
          (*prnt_func)(&ofs + ofs);
          LBL2:
          y = x &= ~x;
          LBL3:
          if (y + 1 < ARRAY_SIZE) {
          if ((++y, iarray[y] < (y-1)[iarray]) && (y[iarray] ^= iarray[y-1] ^= iarray[y] ^= *(iarray + y - 1), x |= y));
          goto LBL3;
          }
          if (x = x) goto LBL2;
          PNT2LINES(out, After sort, ---------------, \n);
          (*prnt_func)(&ofs + ofs);
          }

          L Offline
          L Offline
          Lutoslaw
          wrote on last edited by
          #16

          Check out the following hidden and tricky features of C: FOR statemnt[^] Commentaries[^] And DO NOT USE GOTO[^]

          Greetings - Jacek

          S 1 Reply Last reply
          0
          • L Lutoslaw

            Check out the following hidden and tricky features of C: FOR statemnt[^] Commentaries[^] And DO NOT USE GOTO[^]

            Greetings - Jacek

            S Offline
            S Offline
            SirTimothy
            wrote on last edited by
            #17

            Wow! For loops are neat! I rewrote my code, got rid of the gotos, used for loops, and put in some comments like in the article you linked to. I think it's much improved now. I'm especially proud of my reduction of the number of loops. The main sorting part used to involve two nested loops (one inside the other), but I was able to reduce it to a single loop!

            #include <stdlib.h>
            #include <stdio.h>
            #include <time.h>

            #define ARRAY_SIZE 20

            #define PNT(s, l, a) fprintf(std##s, l, a)
            #define PNT2LINES(s, l1, l2, e) PNT(s, #l1#e#l2#e, 0)

            void print_array(int *array) {
            int x = ARRAY_SIZE;
            char* fmts[] = { "%d, ", "%d\n" }; /* fmts has two strings in it */
            for (--array; PNT(out, ((x == 1) & 1)[fmts], *(array = ++array)), --x > 0;); /* These for loops are really neat!
            I can do a bunch of things all in one line! Do something, test a conditional, do another thing! Or I can leave some of those
            out, if I don't need them. */
            for (;;) break; /* I can even do this! */
            }

            main() {
            int iarray[ARRAY_SIZE];
            int x, y;
            int ofs = iarray - &ofs; /* ofs is an offset */
            void (*prnt_func)/* These comment things are pretty neat too. I can add annotations to my code, without having to hide them in variable names
            or strings that do nothing. */(int*) = &print_array;
            "Here's how I would have had to annotate code before. It's kind of a pain.";
            srand((unsigned int)time(NULL));
            x ^= x; /* ^= looks like a duck. I'm going to call it the "quack operator" */
            for(;((y = rand/*om number generator*/(), x < /* I'm a comment in the middle of an expression */ARRAY_SIZE) && ((((x
            = x + 1) - 1)[iarray] = /*number from 0 to 99*/(y - (y / 100) * 100)) || 1));); /* lot's of nested () there! */
            PNT2LINES(out, Before sort, ---------------, \n);
            for((*prnt_func)(&ofs+ofs),y^=y,x&=~x;(y+1>=ARRAY_SIZE&&(x&&!(y=x&=~x)))||y+1<ARRAY_SIZE;(++y,iarray[y]<(y-1)
            [iarray])&&(y[iarray]^=iarray[y-1]^=iarray[y]^=*(iarray+y-1),x|=y)); /* This used to be two loops, when it was with GOTOs,
            but I was able to
            reduce it to one. That must be faster, right? It's also still easy to read, so it's a total win-win! */
            /* That line had a lot of quack operators! */
            PNT2LINES(out, After sort, ---------------, \n);
            (*prnt_func)(&ofs + ofs);
            }

            L 1 Reply Last reply
            0
            • S SirTimothy

              Wow! For loops are neat! I rewrote my code, got rid of the gotos, used for loops, and put in some comments like in the article you linked to. I think it's much improved now. I'm especially proud of my reduction of the number of loops. The main sorting part used to involve two nested loops (one inside the other), but I was able to reduce it to a single loop!

              #include <stdlib.h>
              #include <stdio.h>
              #include <time.h>

              #define ARRAY_SIZE 20

              #define PNT(s, l, a) fprintf(std##s, l, a)
              #define PNT2LINES(s, l1, l2, e) PNT(s, #l1#e#l2#e, 0)

              void print_array(int *array) {
              int x = ARRAY_SIZE;
              char* fmts[] = { "%d, ", "%d\n" }; /* fmts has two strings in it */
              for (--array; PNT(out, ((x == 1) & 1)[fmts], *(array = ++array)), --x > 0;); /* These for loops are really neat!
              I can do a bunch of things all in one line! Do something, test a conditional, do another thing! Or I can leave some of those
              out, if I don't need them. */
              for (;;) break; /* I can even do this! */
              }

              main() {
              int iarray[ARRAY_SIZE];
              int x, y;
              int ofs = iarray - &ofs; /* ofs is an offset */
              void (*prnt_func)/* These comment things are pretty neat too. I can add annotations to my code, without having to hide them in variable names
              or strings that do nothing. */(int*) = &print_array;
              "Here's how I would have had to annotate code before. It's kind of a pain.";
              srand((unsigned int)time(NULL));
              x ^= x; /* ^= looks like a duck. I'm going to call it the "quack operator" */
              for(;((y = rand/*om number generator*/(), x < /* I'm a comment in the middle of an expression */ARRAY_SIZE) && ((((x
              = x + 1) - 1)[iarray] = /*number from 0 to 99*/(y - (y / 100) * 100)) || 1));); /* lot's of nested () there! */
              PNT2LINES(out, Before sort, ---------------, \n);
              for((*prnt_func)(&ofs+ofs),y^=y,x&=~x;(y+1>=ARRAY_SIZE&&(x&&!(y=x&=~x)))||y+1<ARRAY_SIZE;(++y,iarray[y]<(y-1)
              [iarray])&&(y[iarray]^=iarray[y-1]^=iarray[y]^=*(iarray+y-1),x|=y)); /* This used to be two loops, when it was with GOTOs,
              but I was able to
              reduce it to one. That must be faster, right? It's also still easy to read, so it's a total win-win! */
              /* That line had a lot of quack operators! */
              PNT2LINES(out, After sort, ---------------, \n);
              (*prnt_func)(&ofs + ofs);
              }

              L Offline
              L Offline
              Lutoslaw
              wrote on last edited by
              #18

              SirTimothy wrote:

              for((*prnt_func)(&ofs+ofs),y^=y,x&=~x;(y+1>=ARRAY_SIZE&&(x&&!(y=x&=~x)))||y+1

              I checked it and it gives correct results. I't amazing. Especially, I don't get why

              y[iarray]

              does not cause SEGFAULT or something. Where the O(n^2) complexity is hidden? BTW. I had to implement all sorting algorithms as one of projets on my university. Bubble Sort was among them, but I did not care about performance so much.

              void Bubble::Sort(Table &table, bool descending)
              {
              bool swapped;
              int size = table.size();
              int bound = size - 1;
              int lastSwap = 0;
              do {
              swapped = false;
              for (int i = 0; i < bound; i++)
              {
              TableElement t1 = table[i], t2 = table[i + 1];
              if (descending ? t1 < t2 : t1 > t2) {
              swap(table[i], table[i+1]);
              swapped = true;
              lastSwap = i;
              }
              }
              bound = lastSwap;
              } while (swapped);
              }

              Greetings - Jacek

              S 1 Reply Last reply
              0
              • L Lutoslaw

                SirTimothy wrote:

                for((*prnt_func)(&ofs+ofs),y^=y,x&=~x;(y+1>=ARRAY_SIZE&&(x&&!(y=x&=~x)))||y+1

                I checked it and it gives correct results. I't amazing. Especially, I don't get why

                y[iarray]

                does not cause SEGFAULT or something. Where the O(n^2) complexity is hidden? BTW. I had to implement all sorting algorithms as one of projets on my university. Bubble Sort was among them, but I did not care about performance so much.

                void Bubble::Sort(Table &table, bool descending)
                {
                bool swapped;
                int size = table.size();
                int bound = size - 1;
                int lastSwap = 0;
                do {
                swapped = false;
                for (int i = 0; i < bound; i++)
                {
                TableElement t1 = table[i], t2 = table[i + 1];
                if (descending ? t1 < t2 : t1 > t2) {
                swap(table[i], table[i+1]);
                swapped = true;
                lastSwap = i;
                }
                }
                bound = lastSwap;
                } while (swapped);
                }

                Greetings - Jacek

                S Offline
                S Offline
                SirTimothy
                wrote on last edited by
                #19

                It is kinda amazing, isn't it? The y[iarray] thing, I've seen in a few different websites, basically the compiler just expands it to *(y + iarray) which is the same as iarray[y] and it carries on its merry way. The O(n^2) complexity is hidden in the single loop, I reset the values of x and y in the conditional (the y=x&=~x bit) when y gets past the array length and x is non-zero. Pretty near impossible to prove n^2 runtime, or to prove correctness, or even to prove that it terminates, but it's fun and it works. I thought about writing some more sorts like this, but haven't gotten to it...

                L E 2 Replies Last reply
                0
                • S SirTimothy

                  nah, just felt like doing it

                  G Offline
                  G Offline
                  Gary R Wheeler
                  wrote on last edited by
                  #20

                  We're not taking our meds now, are we?

                  Software Zen: delete this;
                  Fold With Us![^]

                  1 Reply Last reply
                  0
                  • S SirTimothy

                    It is kinda amazing, isn't it? The y[iarray] thing, I've seen in a few different websites, basically the compiler just expands it to *(y + iarray) which is the same as iarray[y] and it carries on its merry way. The O(n^2) complexity is hidden in the single loop, I reset the values of x and y in the conditional (the y=x&=~x bit) when y gets past the array length and x is non-zero. Pretty near impossible to prove n^2 runtime, or to prove correctness, or even to prove that it terminates, but it's fun and it works. I thought about writing some more sorts like this, but haven't gotten to it...

                    L Offline
                    L Offline
                    Lutoslaw
                    wrote on last edited by
                    #21

                    SirTimothy wrote:

                    the compiler just expands it to *(y + iarray) which is the same as iarray[y]

                    It relies on a C-specific feature, where size of int is the same as size of an address, doesn't it? In C# int is always a 32-bit integer... I suppose proving n^2 runtime isn't hard. Each obfuscation can be transformated to a more readable form, which is fully equivalent. Step by step, one could get something similar to my code posted above. I suppose. Sometimes when I have a trouble with theory I generate a chart and write "The chart says, that the algorithm is quadratic, isn't it?". Unfortunately not every teacher was satisfied with such report... :rolleyes:

                    Greetings - Jacek

                    1 Reply Last reply
                    0
                    • S SirTimothy

                      That, of course, is so that I can change the function name, and only need to change one place where it's used. Gotta think about maintainability and future modifications!

                      J Offline
                      J Offline
                      johannesnestler
                      wrote on last edited by
                      #22

                      ... maintainability and future modifications ... hahaha very funny! Can you tell us why you wrote this code? Are you a teacher showing "bad practice"? If one of my programmers would come up with a piece of code like this -> :thumbsdown:

                      S 1 Reply Last reply
                      0
                      • J johannesnestler

                        ... maintainability and future modifications ... hahaha very funny! Can you tell us why you wrote this code? Are you a teacher showing "bad practice"? If one of my programmers would come up with a piece of code like this -> :thumbsdown:

                        S Offline
                        S Offline
                        SirTimothy
                        wrote on last edited by
                        #23

                        Nah, not a teacher. Actually, just graduated from university. Why'd I write it? Bored, felt like taking some reasonably clean code and totally butchering it, thought I'd see how ugly I could make it :) Of course, it could be much worse, but whatever, it was fun!

                        1 Reply Last reply
                        0
                        • S SirTimothy

                          It is kinda amazing, isn't it? The y[iarray] thing, I've seen in a few different websites, basically the compiler just expands it to *(y + iarray) which is the same as iarray[y] and it carries on its merry way. The O(n^2) complexity is hidden in the single loop, I reset the values of x and y in the conditional (the y=x&=~x bit) when y gets past the array length and x is non-zero. Pretty near impossible to prove n^2 runtime, or to prove correctness, or even to prove that it terminates, but it's fun and it works. I thought about writing some more sorts like this, but haven't gotten to it...

                          E Offline
                          E Offline
                          Euhemerus
                          wrote on last edited by
                          #24

                          I think you've got way too much time on your hands :laugh:

                          There is only one satisfying way to boot a computer.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups