# Split array values into an new array?

I was not sure if I should make a new thread or post in my old thread, but i have another array related problem.

Im trying to take the first 5 value from the buffer array and put them in new_array and then move on with the 5 next values, for example like this:

buffer[0] = 1
buffer[1] = 2
buffer[2] = 3
buffer[3] = 4
buffer[4] = 5
buffer[5] = 6
buffer[6] = 7
buffer[7] = 8
buffer[8] = 9
buffer[9] = 10

So value 1,2,3,4,5 should be put in new_buffer[0] and 6,7,8,9,10 into new_buffer[1], and so on.
My buffer array contains 400 values if this makes any difference.
I hope i explained it somewhat understandable, would greatly appreciate help on this.
Best Regards
Lasse

So, the 0, 5, 10, 15, etc. elements of the incoming array go into position 0 of a different array.
The 1, 6, 11, 16, etc. elements go into position 1 of a different array.
The 2, 7, 12, 17, etc. elements go into position 2 of a different array.

See a pattern?

A for loop running from 0 to 400/5, with 80 statements in it would work.

However, splitting 400 elements into 80 arrays makes little sense.

Learning to index a single array as though it was a 2D array would make more sense.

What i mean is that the first 5 elements of the buffer array should be put in new_buffer[0] and the next 5 elements of the buffer array should be put in new_buffer[1] and so on, the buffer array contains about 400 elements so the last 5 elements in the buffer array would be put in something like new_buffer[80]. Or maybe i just misunderstood you? and not the other way around.

What i mean is that the first 5 elements of the buffer array should be put in new_buffer[0] and the next 5 elements of the buffer array should be put in new_buffer[1] and so on

You want to put 5 elements from one array into one element of another array? Unless new_buffer is a 2D array, you can't. If it is, the the elements of buffer[0] to buffer[4] go into new_buffer[0][0] to new_buffer[0][4].

But, I still fail to see the need to copy the arrays. With proper indexing a 1D array can be accessed as though it was a 2D array. After all, the 2D array is actual stored in memory as a sequence of memory locations, just like a 1D array. A little arithmetic goes on behind the scenes to compute an offset from the start of the array to find element [r]`.`

yea you are probably right that there's a better way to do it.
What im trying to do is take my buffer array that is filled with binary values and convert it to numbers. buffer[0][4] is equal to one number and buffer[5][9] equal to another, maybe you could help me with the code? i i would greatly appreciate it. this is what i got so far:

``````				int size = sizeof(buffer2) / sizeof(buffer2[0]);
int amount_chunks = size / 5;

int ic = 0;
int ic2 = ic + 1;
int ic3 = ic2 + 1;
int ic4 = ic3 + 1;
int ic5 = ic4 + 1;

for(int count=0; count<amount_chunks; count++)
{

if (buffer2[ic] == 0 & buffer2[ic2] == 0 & buffer2[ic3] == 0 & buffer2[ic4] == 0 & buffer2[ic5] == 1){
Serial.print("0");
}
else if (buffer2[ic] == 1 & buffer2[ic2] == 0 & buffer2[ic3] == 0 & buffer2[ic4] == 0 & buffer2[ic5] == 0){
Serial.print("1");
}

else if (buffer2[ic] == 0 & buffer2[ic2] == 1 & buffer2[ic3] == 0 & buffer2[ic4] == 0 & buffer2[ic5] == 0){
Serial.print("2");
}

else if (buffer2[ic] == 1 & buffer2[ic2] == 1 & buffer2[ic3] == 0 & buffer2[ic4] == 0 & buffer2[ic5] == 1){
Serial.print("3");
}

else if (buffer2[ic] == 0 & buffer2[ic2] == 0 & buffer2[ic3] == 1 & buffer2[ic4] == 0 & buffer2[ic5] == 0){
Serial.print("4");
}

else if (buffer2[ic] == 1 & buffer2[ic2] == 0 & buffer2[ic3] == 1 & buffer2[ic4] == 0 & buffer2[ic5] == 1){
Serial.print("5");
}

else if (buffer2[ic] == 0 & buffer2[ic2] == 1 & buffer2[ic3] == 1 & buffer2[ic4] == 0 & buffer2[ic5] == 1){
Serial.print("6");
}

else if (buffer2[ic] == 1 & buffer2[ic2] == 1 & buffer2[ic3] == 1 & buffer2[ic4] == 0 & buffer2[ic5] == 0){
Serial.print("7");
}

else if (buffer2[ic] == 0 & buffer2[ic2] == 0 & buffer2[ic3] == 0 & buffer2[ic4] == 1 & buffer2[ic5] == 0){
Serial.print("8");
}

else if (buffer2[ic] == 1 & buffer2[ic2] == 0 & buffer2[ic3] == 0 & buffer2[ic4] == 1 & buffer2[ic5] == 1){
Serial.print("9");
}
ic + 5;
}
``````

I really hate code like this:

``````				 int ic = 0;
int ic2 = ic + 1;
int ic3 = ic2 + 1;
int ic4 = ic3 + 1;
int ic5 = ic4 + 1;
``````

If you are counting things, do you say "Yes, 2, 3, 4..."? No. The first item being counted is numbered 1. So, why isn't ic called ic1?

``````    if (buffer2[ic] == 0 & buffer2[ic2] == 0 & buffer2[ic3] == 0 & buffer2[ic4] == 0 & buffer2[ic5] == 1){
Serial.print("0");
}
``````

Time to learn about bit manipulation.

``````for(int count=0; count<amount_chunks; count++)
{
int val = 0;
for(int i=0; i<5; i++)
{
bitWrite(val, i, buffer[ic+i]);
}
if(val == 0b00001)
Serial.print"0");
else if(val == 0b10000)
Serial.print("1");
// etc.

ic+=5;
}
``````