0

What is the error in the loops. For example, with a 4x4 matrix, starting at element 13 of the array new_matrix[13] gives incorrect values. With other dimensions matrices the same error.

int *helix_center (int** matrix, int size) {
  int* new_matrix = new int[size * size];
  int count = 0;
  int step = 1;
  int sup_var = size / 2;
  int sup_var_2 = sup_var;
  new_matrix[count] = matrix[sup_var][sup_var_2];
  count++;
  while( step < size ) { 
    for (int j = 0; ((j < step) && (step < size)); j++) {
      if (sup_var_2 != 0)
        new_matrix[count] = matrix[sup_var][--sup_var_2];
      count++;
    }
    for (int j = 0; ((j < step) && (step < size)); j++) {
      if(sup_var != 0) 
        new_matrix[count] = matrix[--sup_var][sup_var_2];
      count++;
    }
    step++;
    for (int j = 0; ((j < step) && (step < size)); j++) {
      new_matrix[count] = matrix[sup_var][++sup_var_2];
      count++;
    }
    for (int j = 0; ((j < step) && (step < size)); j++) {
      new_matrix[count] = matrix[++sup_var][sup_var_2];
      count++;
    }
    step++;
  }
 return new_matrix;
}
Rume One
  • 37
  • 3
  • Consider: In the first `for` loop, what gets stored in `new_matrix[count]` when `sup_var_2 == 0`? – 1201ProgramAlarm Sep 14 '20 at 18:54
  • I don't know, what _is_ the error in the loops? You haven't said what actual problem you have, what the symptoms are, what you expected instead or how to reproduce the problem. – Useless Sep 14 '20 at 20:36

1 Answers1

0

Tried to reproduce it, added some simple main with additional prints:

#include <iostream>

int* helix_center(int matrix[4][4], int size)
{
    int* new_matrix = new int[size * size];
    int count = 0;
    int step = 1;
    int sup_var = size / 2;
    int sup_var_2 = sup_var;
    new_matrix[count] = matrix[sup_var][sup_var_2];
    std::cout << count << std::endl;
    count++;
    while (step < size) {
        for (int j = 0; ((j < step) && (step < size)); j++) {
            std::cout << count << std::endl;
            if (sup_var_2 != 0)
                new_matrix[count] = matrix[sup_var][--sup_var_2];
            count++;
        }
        std::cout << "=====" << std::endl;
        for (int j = 0; ((j < step) && (step < size)); j++) {
            std::cout << count << std::endl;
            if (sup_var != 0)
                new_matrix[count] = matrix[--sup_var][sup_var_2];
            count++;
        }
        std::cout << "=====" << std::endl;
        step++;
        for (int j = 0; ((j < step) && (step < size)); j++) {
            std::cout << count << std::endl;
            new_matrix[count] = matrix[sup_var][++sup_var_2];
            count++;
        }
        std::cout << "=====" << std::endl;
        for (int j = 0; ((j < step) && (step < size)); j++) {
            std::cout << count << std::endl;
            new_matrix[count] = matrix[++sup_var][sup_var_2];
            count++;
        }
        step++;
    }
    return new_matrix;
}

int main()
{
    int matrix[4][4] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } };
    int* output = helix_center(matrix, 4);
    std::cout << output[13] << std::endl;
    return 0;
}

Code yields the following:

0
1
=====
2
=====
3
4
=====
5
6
7
8
9
=====
10
11
12
=====
=====
1414422387

So, as you can see, we never go past 12, thus these indices are not being initialized. Quite a lot going on in your code, kinda hard to track counter's changes.

Here's a great implementation of what you're trying to do: https://stackoverflow.com/a/33701500/5430833

Tweaked it a bit so instead of printing, it puts values to the output buffer:

#include <iostream>
#include <cmath>
#include <vector>

int* print_spiral (int** matrix, int size)
{
    int* new_matrix = new int[size * size];
    int x = 0; // current position; x
    int y = 0; // current position; y
    int d = 0; // current direction; 0=RIGHT, 1=DOWN, 2=LEFT, 3=UP
    int c = 0; // counter
    int s = 1; // chain size

    // starting point
    x = ((int)floor(size/2.0))-1;
    y = ((int)floor(size/2.0))-1;

    for (int k=1; k<=(size-1); k++)
    {
        for (int j=0; j<(k<(size-1)?2:3); j++)
        {
            for (int i=0; i<s; i++)
            {
                new_matrix[c] = matrix[x][y];
                c++;

                switch (d)
                {
                    case 0: y = y + 1; break;
                    case 1: x = x + 1; break;
                    case 2: y = y - 1; break;
                    case 3: x = x - 1; break;
                }
            }
            d = (d+1)%4;
        }
        s = s + 1;
    }
    return new_matrix;
}

int main()
{
    std::vector<std::vector<int>> v{ { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } };
    int **p = new int *[v.size()];

    for ( size_t i = 0; i < v.size(); i++ ) p[i] = v[i].data();
    int* output = print_spiral(p, 4);
    for(int i = 0; i < 16; i++) {
        std::cout << output[i] << std::endl;
    }
    return 0;
}

Which yields the following:

6
7
11
10
9
5
1
2
3
4
8
12
16
15
14
0

Which is what you expect, I believe.

Marek Piotrowski
  • 2,988
  • 3
  • 11
  • 16