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. Beginners "luck"

Beginners "luck"

Scheduled Pinned Locked Moved The Weird and The Wonderful
c++data-structuresoopperformancelearning
7 Posts 5 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.
  • OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #1

    Here is a code sample from Q&A. Obviously a beginner (so I won't say where or who to spare the OP's blushes) but "How not to do it: convert from ascii to an array of bits":

    array< array< int >^ >^ chartobin(array^arr,int length,int imgsize1)
    {
    //converts entered characters into binary n stores in message array
    int ascii;
    array< array< int >^ >^ messag= gcnew array< array< int >^ >(imgsize1);
    for(int i=0;i<imgsize1;i++)>
    {
    messag[i] = gcnew array(8);
    }
    for(int x= 0;x<length;x++)>
    {
    ascii =(int) arr[x];
    int* binary_reverse = new int [9];
    int* binary = new int [9];
    int y = 0;
    while(ascii != 1)
    {
    if(ascii % 2 == 0) //if ascii is divisible by 2
    {
    binary_reverse[y] = 0; //then put a zero
    }
    else if(ascii % 2 == 1) //if it isnt divisible by 2
    {
    binary_reverse[y] = 1; //then put a 1
    }
    ascii /= 2;
    y++;
    }
    if(ascii == 1) //when ascii is 1, we have to add 1 to the beginning
    {
    binary_reverse[y] = 1;
    y++;
    }
    if(y < 8) //add zeros to the end of string if not 8 characters (1 byte)
    {
    for(; y < 8; y++) //add until binary_reverse[7] (8th element)
    {
    binary_reverse[y] = 0;
    }
    }
    for(int z = 0; z < 8; z++) //our array is reversed. put the numbers in the rigth order (last comes first)
    {
    binary[z] = binary_reverse[7 - z];
    if(binary[z]==0)
    {
    binary[z]=-1;
    }
    }
    for(int z = 0; z < 8; z++)
    {
    messag[x][z]=binary[z];
    }
    delete [] binary_reverse; //free the memory created by dynamic mem. allocation
    delete [] binary;
    }
    return messag;
    }

    You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    D P J N 5 Replies Last reply
    0
    • OriginalGriffO OriginalGriff

      Here is a code sample from Q&A. Obviously a beginner (so I won't say where or who to spare the OP's blushes) but "How not to do it: convert from ascii to an array of bits":

      array< array< int >^ >^ chartobin(array^arr,int length,int imgsize1)
      {
      //converts entered characters into binary n stores in message array
      int ascii;
      array< array< int >^ >^ messag= gcnew array< array< int >^ >(imgsize1);
      for(int i=0;i<imgsize1;i++)>
      {
      messag[i] = gcnew array(8);
      }
      for(int x= 0;x<length;x++)>
      {
      ascii =(int) arr[x];
      int* binary_reverse = new int [9];
      int* binary = new int [9];
      int y = 0;
      while(ascii != 1)
      {
      if(ascii % 2 == 0) //if ascii is divisible by 2
      {
      binary_reverse[y] = 0; //then put a zero
      }
      else if(ascii % 2 == 1) //if it isnt divisible by 2
      {
      binary_reverse[y] = 1; //then put a 1
      }
      ascii /= 2;
      y++;
      }
      if(ascii == 1) //when ascii is 1, we have to add 1 to the beginning
      {
      binary_reverse[y] = 1;
      y++;
      }
      if(y < 8) //add zeros to the end of string if not 8 characters (1 byte)
      {
      for(; y < 8; y++) //add until binary_reverse[7] (8th element)
      {
      binary_reverse[y] = 0;
      }
      }
      for(int z = 0; z < 8; z++) //our array is reversed. put the numbers in the rigth order (last comes first)
      {
      binary[z] = binary_reverse[7 - z];
      if(binary[z]==0)
      {
      binary[z]=-1;
      }
      }
      for(int z = 0; z < 8; z++)
      {
      messag[x][z]=binary[z];
      }
      delete [] binary_reverse; //free the memory created by dynamic mem. allocation
      delete [] binary;
      }
      return messag;
      }

      You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

      D Offline
      D Offline
      DaveAuld
      wrote on last edited by
      #2

      Yes a bit of a horror, but, yes there is a but, without knowing what was the driver for this question or what the OP was upto, they might have been forced to do it this way, particularly if they are on a programming course etc. I am currently doing a degree with the OU, and it is all based around Java. The question papers etc, sometimes tell you must do some things a certain way and not use other methods (or built in libraries/extensions etc. The last paper I was doing was based around threads, it had the 'main' thread, and 3 other worker threads. We had to use semaphores/countdown latches etc. to ensure the workers were finished before the main thread did something, rather than a simple join.

      Dave Don't forget to rate messages!
      Find Me On: Web|Facebook|Twitter|LinkedIn
      Waving? dave.m.auld[at]googlewave.com

      OriginalGriffO 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Here is a code sample from Q&A. Obviously a beginner (so I won't say where or who to spare the OP's blushes) but "How not to do it: convert from ascii to an array of bits":

        array< array< int >^ >^ chartobin(array^arr,int length,int imgsize1)
        {
        //converts entered characters into binary n stores in message array
        int ascii;
        array< array< int >^ >^ messag= gcnew array< array< int >^ >(imgsize1);
        for(int i=0;i<imgsize1;i++)>
        {
        messag[i] = gcnew array(8);
        }
        for(int x= 0;x<length;x++)>
        {
        ascii =(int) arr[x];
        int* binary_reverse = new int [9];
        int* binary = new int [9];
        int y = 0;
        while(ascii != 1)
        {
        if(ascii % 2 == 0) //if ascii is divisible by 2
        {
        binary_reverse[y] = 0; //then put a zero
        }
        else if(ascii % 2 == 1) //if it isnt divisible by 2
        {
        binary_reverse[y] = 1; //then put a 1
        }
        ascii /= 2;
        y++;
        }
        if(ascii == 1) //when ascii is 1, we have to add 1 to the beginning
        {
        binary_reverse[y] = 1;
        y++;
        }
        if(y < 8) //add zeros to the end of string if not 8 characters (1 byte)
        {
        for(; y < 8; y++) //add until binary_reverse[7] (8th element)
        {
        binary_reverse[y] = 0;
        }
        }
        for(int z = 0; z < 8; z++) //our array is reversed. put the numbers in the rigth order (last comes first)
        {
        binary[z] = binary_reverse[7 - z];
        if(binary[z]==0)
        {
        binary[z]=-1;
        }
        }
        for(int z = 0; z < 8; z++)
        {
        messag[x][z]=binary[z];
        }
        delete [] binary_reverse; //free the memory created by dynamic mem. allocation
        delete [] binary;
        }
        return messag;
        }

        You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

        D Offline
        D Offline
        DaveAuld
        wrote on last edited by
        #3

        On a different note from my other post on this, i am a hobbyist programmer and do not do it as my day-to-day line of work. As a result of this i have many many times found myself producing completely bizarre code to do a task, only to find out that there a specific classes/methods available within the .net framework, usually resulting in me giving myself a slap. Its one of those......experience makes a difference.....kind of things i guess.

        Dave Don't forget to rate messages!
        Find Me On: Web|Facebook|Twitter|LinkedIn
        Waving? dave.m.auld[at]googlewave.com

        1 Reply Last reply
        0
        • D DaveAuld

          Yes a bit of a horror, but, yes there is a but, without knowing what was the driver for this question or what the OP was upto, they might have been forced to do it this way, particularly if they are on a programming course etc. I am currently doing a degree with the OU, and it is all based around Java. The question papers etc, sometimes tell you must do some things a certain way and not use other methods (or built in libraries/extensions etc. The last paper I was doing was based around threads, it had the 'main' thread, and 3 other worker threads. We had to use semaphores/countdown latches etc. to ensure the workers were finished before the main thread did something, rather than a simple join.

          Dave Don't forget to rate messages!
          Find Me On: Web|Facebook|Twitter|LinkedIn
          Waving? dave.m.auld[at]googlewave.com

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          I know what you mean, but I think what got me was the way you could see the OP original thought process run through the whole thing: "Good that works." "Damn, forgot the last digit. That fixes it" "Bugger, forgot the zeros if there are any left over...better put them in." "What do you mean its back to front - sod it!" "Oh, yes, I had better include it in the output..." Without at any time wanting to change what the OP already has that is working (sort of)! I do find that a reluctance to look back and change partly broken code does cause a lot of horrors...

          You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Here is a code sample from Q&A. Obviously a beginner (so I won't say where or who to spare the OP's blushes) but "How not to do it: convert from ascii to an array of bits":

            array< array< int >^ >^ chartobin(array^arr,int length,int imgsize1)
            {
            //converts entered characters into binary n stores in message array
            int ascii;
            array< array< int >^ >^ messag= gcnew array< array< int >^ >(imgsize1);
            for(int i=0;i<imgsize1;i++)>
            {
            messag[i] = gcnew array(8);
            }
            for(int x= 0;x<length;x++)>
            {
            ascii =(int) arr[x];
            int* binary_reverse = new int [9];
            int* binary = new int [9];
            int y = 0;
            while(ascii != 1)
            {
            if(ascii % 2 == 0) //if ascii is divisible by 2
            {
            binary_reverse[y] = 0; //then put a zero
            }
            else if(ascii % 2 == 1) //if it isnt divisible by 2
            {
            binary_reverse[y] = 1; //then put a 1
            }
            ascii /= 2;
            y++;
            }
            if(ascii == 1) //when ascii is 1, we have to add 1 to the beginning
            {
            binary_reverse[y] = 1;
            y++;
            }
            if(y < 8) //add zeros to the end of string if not 8 characters (1 byte)
            {
            for(; y < 8; y++) //add until binary_reverse[7] (8th element)
            {
            binary_reverse[y] = 0;
            }
            }
            for(int z = 0; z < 8; z++) //our array is reversed. put the numbers in the rigth order (last comes first)
            {
            binary[z] = binary_reverse[7 - z];
            if(binary[z]==0)
            {
            binary[z]=-1;
            }
            }
            for(int z = 0; z < 8; z++)
            {
            messag[x][z]=binary[z];
            }
            delete [] binary_reverse; //free the memory created by dynamic mem. allocation
            delete [] binary;
            }
            return messag;
            }

            You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

            P Online
            P Online
            PIEBALDconsult
            wrote on last edited by
            #5

            One of the first things I did wen I learned C# was to translate a bunch of my C routines -- only to find that a lot of them were already built-in.

            1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              Here is a code sample from Q&A. Obviously a beginner (so I won't say where or who to spare the OP's blushes) but "How not to do it: convert from ascii to an array of bits":

              array< array< int >^ >^ chartobin(array^arr,int length,int imgsize1)
              {
              //converts entered characters into binary n stores in message array
              int ascii;
              array< array< int >^ >^ messag= gcnew array< array< int >^ >(imgsize1);
              for(int i=0;i<imgsize1;i++)>
              {
              messag[i] = gcnew array(8);
              }
              for(int x= 0;x<length;x++)>
              {
              ascii =(int) arr[x];
              int* binary_reverse = new int [9];
              int* binary = new int [9];
              int y = 0;
              while(ascii != 1)
              {
              if(ascii % 2 == 0) //if ascii is divisible by 2
              {
              binary_reverse[y] = 0; //then put a zero
              }
              else if(ascii % 2 == 1) //if it isnt divisible by 2
              {
              binary_reverse[y] = 1; //then put a 1
              }
              ascii /= 2;
              y++;
              }
              if(ascii == 1) //when ascii is 1, we have to add 1 to the beginning
              {
              binary_reverse[y] = 1;
              y++;
              }
              if(y < 8) //add zeros to the end of string if not 8 characters (1 byte)
              {
              for(; y < 8; y++) //add until binary_reverse[7] (8th element)
              {
              binary_reverse[y] = 0;
              }
              }
              for(int z = 0; z < 8; z++) //our array is reversed. put the numbers in the rigth order (last comes first)
              {
              binary[z] = binary_reverse[7 - z];
              if(binary[z]==0)
              {
              binary[z]=-1;
              }
              }
              for(int z = 0; z < 8; z++)
              {
              messag[x][z]=binary[z];
              }
              delete [] binary_reverse; //free the memory created by dynamic mem. allocation
              delete [] binary;
              }
              return messag;
              }

              You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

              J Offline
              J Offline
              Jordy Kaiwa Ruiter
              wrote on last edited by
              #6

              I also loved:

              int* binary_reverse = new int [9];
              int* binary = new int [9];

              ...

              //add until binary_reverse[7] (8th element)

              1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                Here is a code sample from Q&A. Obviously a beginner (so I won't say where or who to spare the OP's blushes) but "How not to do it: convert from ascii to an array of bits":

                array< array< int >^ >^ chartobin(array^arr,int length,int imgsize1)
                {
                //converts entered characters into binary n stores in message array
                int ascii;
                array< array< int >^ >^ messag= gcnew array< array< int >^ >(imgsize1);
                for(int i=0;i<imgsize1;i++)>
                {
                messag[i] = gcnew array(8);
                }
                for(int x= 0;x<length;x++)>
                {
                ascii =(int) arr[x];
                int* binary_reverse = new int [9];
                int* binary = new int [9];
                int y = 0;
                while(ascii != 1)
                {
                if(ascii % 2 == 0) //if ascii is divisible by 2
                {
                binary_reverse[y] = 0; //then put a zero
                }
                else if(ascii % 2 == 1) //if it isnt divisible by 2
                {
                binary_reverse[y] = 1; //then put a 1
                }
                ascii /= 2;
                y++;
                }
                if(ascii == 1) //when ascii is 1, we have to add 1 to the beginning
                {
                binary_reverse[y] = 1;
                y++;
                }
                if(y < 8) //add zeros to the end of string if not 8 characters (1 byte)
                {
                for(; y < 8; y++) //add until binary_reverse[7] (8th element)
                {
                binary_reverse[y] = 0;
                }
                }
                for(int z = 0; z < 8; z++) //our array is reversed. put the numbers in the rigth order (last comes first)
                {
                binary[z] = binary_reverse[7 - z];
                if(binary[z]==0)
                {
                binary[z]=-1;
                }
                }
                for(int z = 0; z < 8; z++)
                {
                messag[x][z]=binary[z];
                }
                delete [] binary_reverse; //free the memory created by dynamic mem. allocation
                delete [] binary;
                }
                return messag;
                }

                You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

                N Offline
                N Offline
                NormDroid
                wrote on last edited by
                #7

                I almost read it as "Beginners Suck" ;)

                Two heads are better than one.

                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