adding pointer arrays
-
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!
-
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!
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.
-
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!
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
tobool
when you copy the elements of the arrays. -
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
tobool
when you copy the elements of the arrays.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!
-
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!
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
} -
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
}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!
-
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!
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 elementspBool += 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.
-
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 elementspBool += 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.
Thanks a lot!! Now it's working fine :) Many thanks again!!
-
Thanks a lot!! Now it's working fine :) Many thanks again!!