Skip to content

Instantly share code, notes, and snippets.

@x225am
Created September 27, 2023 10:35
Show Gist options
  • Save x225am/45f990282444af24360900fc319aa865 to your computer and use it in GitHub Desktop.
Save x225am/45f990282444af24360900fc319aa865 to your computer and use it in GitHub Desktop.
2D array in to the 1D arrays
const int numRows = 6;
const int numItems = 23;
// Define the original 2D array
byte originalArray[numRows][numItems] = {
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22},
{23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45},
{46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68},
{69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91},
{92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114},
{115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137}
};
// Define 6 separate 1D arrays
byte separateArray1[numItems];
byte separateArray2[numItems];
byte separateArray3[numItems];
byte separateArray4[numItems];
byte separateArray5[numItems];
byte separateArray6[numItems];
void setup() {
Serial.begin(9600);
// Copy each row into independent 1D arrays
for (int i = 0; i < numItems; i++) {
// separateArray1[i] = originalArray[0][i];
// separateArray2[i] = originalArray[1][i];
// separateArray3[i] = originalArray[2][i];
// separateArray4[i] = originalArray[3][i];
// separateArray5[i] = originalArray[4][i];
separateArray6[i] = originalArray[5][i];
}
Serial.println("Pure Output:");
for (int i = 0; i < numItems; i++)
{
Serial.print(separateArray6[i]);
Serial.print(" ");
}
Serial.println("*************");
Serial.println("Pure Output 2D:");
for (int i = 0; i < numItems; i++)
{
Serial.print(originalArray[5][i]);
Serial.print(" ");
}
Serial.println("*************");
// Print the original 2D array
Serial.println("Original 2D Array:");
printArray(originalArray);
// Print the separate 1D arrays
Serial.println("Separate 1D Arrays:");
Serial.print("Array 1: ");
printArrayRow(separateArray1, numItems);
Serial.print("Array 2: ");
printArrayRow(separateArray2, numItems);
Serial.print("Array 3: ");
printArrayRow(separateArray3, numItems);
Serial.print("Array 4: ");
printArrayRow(separateArray4, numItems);
Serial.print("Array 5: ");
printArrayRow(separateArray5, numItems);
Serial.print("Array 6: ");
printArrayRow(separateArray6, numItems);
}
void loop() {
// Not used in this example
}
// Function to print a row of the array
void printArrayRow(byte arr[], int size) {
for (int i = 0; i < size; i++) {
Serial.print(arr[i]);
Serial.print(" ");
}
Serial.println();
}
// Function to print the entire 2D array
void printArray(byte arr[numRows][numItems]) {
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numItems; j++) {
Serial.print(arr[i][j]);
Serial.print(" ");
}
Serial.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment