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. General Programming
  3. Managed C++/CLI
  4. adding pointer arrays

adding pointer arrays

Scheduled Pinned Locked Moved Managed C++/CLI
data-structuresquestion
9 Posts 3 Posters 14 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.
  • M Offline
    M Offline
    Member 9350237
    wrote on last edited by
    #1

    Hi Guys, I have to make a single bool array combining four integer arrays as follows. I'm not supposed to do as

    connected = new bool [bl1 + bl2 + tr + ct];

    because bl1, bl2,tr,ct are pointer to arrays. Can some one give a solution for this?? Here is my code:

    class Protocol{

    int baseline1\[10\],baseline2\[10\];
    int catcht\[20\];
    int training\[120\];
    
    bool \*connected;
    

    public:
    Protocol(int[10],int[10],int[120],int[20]);
    };

    #include "protocol.h"
    #include "math.h"

    Protocol::Protocol(int bl1[10], int bl2[10], int tr[120], int ct[20]){

    int \*baseline1 = bl1;
    int \*baseline2 = bl2; 
    int \*training  = tr;
    int \*catcht    = ct;
    
    connected = new bool \[bl1 + bl2 + tr + ct\];
    

    }

    Thank you!

    Richard Andrew x64R L 2 Replies Last reply
    0
    • M Member 9350237

      Hi Guys, I have to make a single bool array combining four integer arrays as follows. I'm not supposed to do as

      connected = new bool [bl1 + bl2 + tr + ct];

      because bl1, bl2,tr,ct are pointer to arrays. Can some one give a solution for this?? Here is my code:

      class Protocol{

      int baseline1\[10\],baseline2\[10\];
      int catcht\[20\];
      int training\[120\];
      
      bool \*connected;
      

      public:
      Protocol(int[10],int[10],int[120],int[20]);
      };

      #include "protocol.h"
      #include "math.h"

      Protocol::Protocol(int bl1[10], int bl2[10], int tr[120], int ct[20]){

      int \*baseline1 = bl1;
      int \*baseline2 = bl2; 
      int \*training  = tr;
      int \*catcht    = ct;
      
      connected = new bool \[bl1 + bl2 + tr + ct\];
      

      }

      Thank you!

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      You haven't explained what it means to "make a single bool array combining four integer arrays." Therefore, no one can help you yet.

      The difficult we do right away... ...the impossible takes slightly longer.

      1 Reply Last reply
      0
      • M Member 9350237

        Hi Guys, I have to make a single bool array combining four integer arrays as follows. I'm not supposed to do as

        connected = new bool [bl1 + bl2 + tr + ct];

        because bl1, bl2,tr,ct are pointer to arrays. Can some one give a solution for this?? Here is my code:

        class Protocol{

        int baseline1\[10\],baseline2\[10\];
        int catcht\[20\];
        int training\[120\];
        
        bool \*connected;
        

        public:
        Protocol(int[10],int[10],int[120],int[20]);
        };

        #include "protocol.h"
        #include "math.h"

        Protocol::Protocol(int bl1[10], int bl2[10], int tr[120], int ct[20]){

        int \*baseline1 = bl1;
        int \*baseline2 = bl2; 
        int \*training  = tr;
        int \*catcht    = ct;
        
        connected = new bool \[bl1 + bl2 + tr + ct\];
        

        }

        Thank you!

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Member 9350237 wrote:

        connected = new bool [bl1 + bl2 + tr + ct];

        because bl1, bl2,tr,ct are pointer to arrays.

        That makes no sense, the value in brackets has to be the requested length of the array. So the new array needs to be long enough to contain the combined number of elements of the other arrays. You also need to explain how you will convert from int to bool when you copy the elements of the arrays.

        M 1 Reply Last reply
        0
        • L Lost User

          Member 9350237 wrote:

          connected = new bool [bl1 + bl2 + tr + ct];

          because bl1, bl2,tr,ct are pointer to arrays.

          That makes no sense, the value in brackets has to be the requested length of the array. So the new array needs to be long enough to contain the combined number of elements of the other arrays. You also need to explain how you will convert from int to bool when you copy the elements of the arrays.

          M Offline
          M Offline
          Member 9350237
          wrote on last edited by
          #4

          Thanks for responding!. Sorry about my poor explanation.. I think I conveyed it a wrong way. I'm still in a confusion! Let me try to explain the scenario.. Ultimately I need to make a bool array of length 10+120+20=150.This will be combinations of zeros and ones.. Out of which first 10 and last 20 positions are filled with '0'. 120 positions are divided into 10 sets of 12 members.. out of 12 positions 10 are ones and two zeros have to be randomly placed into this 12 locations.. for exp: Following are 1st and last set of 12 positions.. {1,1,1,1,0,1,1,1,1,0,1,1,.....1,1,0,1,1,1,0,1,1,1,1,1} I'm bit stuck implementing this bool array... As I mentined before

          int baseline1[10], int baseline2[20]

          are 1st 10 and last 20 positions of the bool array which is basically '0', {0}..

          int training[120]

          is to be assigend with zeros and ones in such a way that..

          int catcht[20]

          array of zeros to be combined with array [120] as I mentioned before.. Sorry if my explanation is vague!

          L 1 Reply Last reply
          0
          • M Member 9350237

            Thanks for responding!. Sorry about my poor explanation.. I think I conveyed it a wrong way. I'm still in a confusion! Let me try to explain the scenario.. Ultimately I need to make a bool array of length 10+120+20=150.This will be combinations of zeros and ones.. Out of which first 10 and last 20 positions are filled with '0'. 120 positions are divided into 10 sets of 12 members.. out of 12 positions 10 are ones and two zeros have to be randomly placed into this 12 locations.. for exp: Following are 1st and last set of 12 positions.. {1,1,1,1,0,1,1,1,1,0,1,1,.....1,1,0,1,1,1,0,1,1,1,1,1} I'm bit stuck implementing this bool array... As I mentined before

            int baseline1[10], int baseline2[20]

            are 1st 10 and last 20 positions of the bool array which is basically '0', {0}..

            int training[120]

            is to be assigend with zeros and ones in such a way that..

            int catcht[20]

            array of zeros to be combined with array [120] as I mentioned before.. Sorry if my explanation is vague!

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            OK, so you know the length of the array, so you just allocate it as normal:

            int arraySize = 10+120+20;
            bool* boolArray = new bool[arraySize];
            memset(boolArray, 0, sizeof(bool) * arraySize);

            gives you an array with all elements set to zero. You now just need to add the code to set the various other items as required.

            bool* pBool = boolArray + 10; // skip first 10 elements
            for (int i = 0; i < 10; ++i) // loop round each set of 12 elements, i.e. 10 times
            {
            // use the pBool pointer to set random values into the 12 elements
            // ...
            pBool += 12; // point to the next set, and repeat
            }

            M 1 Reply Last reply
            0
            • L Lost User

              OK, so you know the length of the array, so you just allocate it as normal:

              int arraySize = 10+120+20;
              bool* boolArray = new bool[arraySize];
              memset(boolArray, 0, sizeof(bool) * arraySize);

              gives you an array with all elements set to zero. You now just need to add the code to set the various other items as required.

              bool* pBool = boolArray + 10; // skip first 10 elements
              for (int i = 0; i < 10; ++i) // loop round each set of 12 elements, i.e. 10 times
              {
              // use the pBool pointer to set random values into the 12 elements
              // ...
              pBool += 12; // point to the next set, and repeat
              }

              M Offline
              M Offline
              Member 9350237
              wrote on last edited by
              #6

              Thanks a lot!!, I'm wondering how to set random zeros and ones to pBool pointer! you mean

              pBool = rand()%2;

              Still bit confused! As I mentioned , for each set I have 12 numbers, out of which 10 numbers 'must' be 1 and 2 0s have to be put randomly into this block of ones, so total of 12: One set should be like this {1,1,1,1,0,1,1,0,1,1,1,1,...} and in the 2nd set there must be 10 ones and 2 zeros , but position of zero will be different. will be great if you can help me out on this!

              L 1 Reply Last reply
              0
              • M Member 9350237

                Thanks a lot!!, I'm wondering how to set random zeros and ones to pBool pointer! you mean

                pBool = rand()%2;

                Still bit confused! As I mentioned , for each set I have 12 numbers, out of which 10 numbers 'must' be 1 and 2 0s have to be put randomly into this block of ones, so total of 12: One set should be like this {1,1,1,1,0,1,1,0,1,1,1,1,...} and in the 2nd set there must be 10 ones and 2 zeros , but position of zero will be different. will be great if you can help me out on this!

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                I would do something like:

                bool* pBool = boolArray + 10; // skip first 10 elements
                srand( (unsigned)time( NULL ) ); // set a random seed for the generator
                for (int i = 0; i < 10; ++i) // loop round each set of 12 elements, i.e. 10 times
                {
                memset(pBool, 1, sizeof(bool) * 12); // set all 12 elements to 1s
                int rIndex = rand() % 12; // get index in range [0 .. 11]
                pBool[rIndex] = 0; // set this one to zero
                do
                {
                int rIndex2 = rand() % 12; // get index in range [0 .. 11]
                pBool[rIndex2] = 0; // set this one to zero
                } while (rIndex2 == rIndex); // make sure they are different elements

                pBool += 12;                   // point to the next set, and repeat
                

                }

                I have not tested this so you need to try it a few times to make sure it produces the desired results.

                M 1 Reply Last reply
                0
                • L Lost User

                  I would do something like:

                  bool* pBool = boolArray + 10; // skip first 10 elements
                  srand( (unsigned)time( NULL ) ); // set a random seed for the generator
                  for (int i = 0; i < 10; ++i) // loop round each set of 12 elements, i.e. 10 times
                  {
                  memset(pBool, 1, sizeof(bool) * 12); // set all 12 elements to 1s
                  int rIndex = rand() % 12; // get index in range [0 .. 11]
                  pBool[rIndex] = 0; // set this one to zero
                  do
                  {
                  int rIndex2 = rand() % 12; // get index in range [0 .. 11]
                  pBool[rIndex2] = 0; // set this one to zero
                  } while (rIndex2 == rIndex); // make sure they are different elements

                  pBool += 12;                   // point to the next set, and repeat
                  

                  }

                  I have not tested this so you need to try it a few times to make sure it produces the desired results.

                  M Offline
                  M Offline
                  Member 9350237
                  wrote on last edited by
                  #8

                  Thanks a lot!! Now it's working fine :) Many thanks again!!

                  L 1 Reply Last reply
                  0
                  • M Member 9350237

                    Thanks a lot!! Now it's working fine :) Many thanks again!!

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Happy to help.

                    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