0

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?

  • 1
    this may help: https://stackoverflow.com/questions/2151084/map-a-2d-array-onto-a-1d-array – mokumus Aug 31 '21 at 11:52
  • 3
    `flat` has *four* elements, you try to print five. Going out of bounds leads to *undefined behavior* which makes your whole program ill-formed. – Some programmer dude Aug 31 '21 at 11:52
  • 2
    Beyond that, please learn how to use a *debugger* to step through your code, statement by statement while monitoring variables and their values. Consistent indentation also helps when trying to understand the code. – Some programmer dude Aug 31 '21 at 11:53
  • Also, add a space between each number to get a better view of what is actually being printed: `"%d "` – kaylum Aug 31 '21 at 11:53
  • 1
    Even 5 elements may not be enough if you are assigning values in the 4-loop nest with `flat[k] = disp[i][j]; k++;` – Weather Vane Aug 31 '21 at 11:54
  • Can you show a way to do this please? @WeatherVane – user14997606 Aug 31 '21 at 11:54
  • The `k++;` looks like it is in the wrong place. You should probably place it after the 2 innermost loops, near the `printf("%d ", mx);` call. In fact you could use `flat[k++] = mx;` at that position and remove the `flat[k] = disp[i][j];` statement. – Ian Abbott Aug 31 '21 at 11:59
  • It isn't clear as to *why* the output should be `24 32 26 8` – Weather Vane Aug 31 '21 at 12:01
  • Running through for loop it should look like 24 32 26 8 (1 D array) @WeatherVane Basically im just trying to print it like that – user14997606 Aug 31 '21 at 12:08
  • I mean why those *particular values*, IOW what does the code *do*? Is this an [XY problem?](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Weather Vane Aug 31 '21 at 12:09
  • @WeatherVane It is supposed to print and store the maximum value from each quadrant of the matrix (dividing the 4x4 matrix into 4 2x2 matrices and printing and storing the maximum value from each 2x2 matrix). – Ian Abbott Aug 31 '21 at 12:15

1 Answers1

2

I made a few changes to the code and marked them with comments.

The essential problem was that writing the maximum to the array (and incrementing its write index) was inside the loop for determining the max element. In your code you added each 'increment' to the array and not the final value as intended.

Also note, that there is no bounds check on k. So it might overflow the flat-array. It might be a good idea to add that if you use this code on other matrix sizes as well.

#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 < N && i < x + quadrant_size ; i++) {
//                 ~~~~~~~~ additional bounds check
        for (j = y ; j < N && j < y + quadrant_size ; j++) {
//                   ~~~~~~~~ additional bounds check
          if (disp[i][j] > mx) {
            mx = disp[i][j];
//          removed code here
          }
        }
      }
//    move code here
//    only accept the maximum
      flat[k] = mx;
      k++;
      printf("%d ", mx);
    }
    printf("\n");
  }

  for (i = 0; i < k; i++) {
//                ~ use k as bound, k knows how many elements where inserted
    printf("%d ", flat[i]);
//            ~ space for readability
  }
  return 0;
}
Burdui
  • 1,242
  • 2
  • 10
  • 24