#include <stdio.h>
#include <stdlib.h>
#define ROWS 3
#define COLS 4
void collect_non_zero_elements(int rows, int cols, int array[][cols], int *nonZeroList, int *count) {
*count = 0;
// Collect non-zero elements
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (array[i][j] != 0) {
nonZeroList[(*count)++] = array[i][j];
}
}
}
}
int main() {
int array[ROWS][COLS] = {
{ 1, 0, 8, 2 },
{ 0, 7, 3, 0 },
{ 9, 0, 0, 4 }
};
int nonZeroList[ROWS * COLS] = {0}; // Assuming a maximum of 12 elements
int count = 0;
collect_non_zero_elements(ROWS, COLS, array, nonZeroList, &count);
for (int i = 0; i < count; i++) {
printf("%d ", nonZeroList[i]);
}
return 0;
}
/*
run:
1 8 2 7 3 9 4
*/