I am not very good with words so I included a visual sample of my code. I would appreciate the help. Thank you (To add I am also a fresh programmer trying to learn algorithms but im struggling on this one)
Is there a working way to store a 2d array into a 1D array?? I have tried to do it in this way but I'm getting garbage values instead when I print it out.
#include <stdio.h>
int main() {
int disp[4][4] = {{12, 14, 32, 9},
{19, 24, 3, 4},
{11, 26, 3, 8},
{13, 24, 7, 5}
};
int quadrant_size = 2;
int k = 0;
int flat[4] = {0};
int N = 4;
int x, y, i, j;
for (x = 0 ; x < N ; x += quadrant_size) {
for (y = 0 ; y < N ; y += quadrant_size) {
int mx = disp[x][y];
for (i = x ; i < x + quadrant_size ; i++) {
for (j = y ; j < y + quadrant_size ; j++) {
if (disp[i][j] > mx) {
mx = disp[i][j];
flat[k] = disp[i][j];
k++;
}
}
}
printf("%d ", mx);
}
printf("\n");
}
for (i = 0; i < 4; i++) {
printf("%d", flat[i]);
}
return 0;
}
Basically, the output of this program will give you a result of
24 32
26 8
I am trying to print this in a 1D array using flat[k]
running through a for loop will generate (Code sample included) without the pattern shown above. However, I am getting random values instead of getting a result of (When converting 2D Arrays to a 1D array)
Should be: 24 32 26 8
Instead, I am getting: 141924268
Is there an efficient way of executing this?