0

I want to get a two-dimensional array consisting of Fibonacci numbers, where each subsequent row continues a sequence of Fibonacci numbers from the previous row. Something like that:

Array[4 rows][4 columns];
{{0,   1,   1,   2},
{3,   5,   8,   13}, 
{21,  34,  55,  89}, 
{144, 233, 377, 610}}

I need to implement this in C#. But I started with writing in C++. This is my current code in C++ and it works right (as I need):

#include <iostream>

int main() 
{
  const int rows = 3;
  const int cols = 4;

  int long A[rows][cols];
  A[0][0] = 0;
  A[0][1] = 1;

  for(size_t i = 0; i < rows; i++)
  {
    if (i == 0)
      for(size_t j = 2; j < cols+1; j++)
        A[i][j] = A[i][j - 2] + A[i][j - 1];
    else
      for(size_t j = 0; j < cols+1; j++)
        A[i][j] = A[i][j - 2] + A[i][j - 1];
  }

  // Output:
  //   0  1  1  2
  //   3  5  8  13
  //   21  34 55 89
  for(size_t i = 0; i < rows; i++)
  {
    for(size_t j = 0; j < cols; j++)
      std::cout << A[i][j] << "  ";
    std::cout << '\n';
  }
}

Then I decided to move it to C#:

using System;

class MainClass {
  public static void Main (string[] args) {
    var rows = 3;
    var cols = 4;
    var A = new long[rows, cols];
    A[0, 0] = 0;
    A[0, 1] = 1;

    for (var i = 0; i < rows; i++)
    {
      if (i == 0)
        for (var j = 2; j < cols + 1; j++)
          A[i, j] = A[i, j - 2] + A[i, j - 1];
      else
        for (var j = 0; j < cols + 1; j++)
          A[i, j] = A[i, j - 2] + A[i, j - 1];
    }

    for(var i = 0; i < rows; i++)
    {
      for(var j = 0; j < cols; j++)
        Console.WriteLine($"{A[i, j]}  ");
      Console.WriteLine("\n");
    } 
  }
}

Algorithmically identical code doesn't work in C#.

The error occurs here: for (var j = 2; j < cols + 1; j++)

And the error itself: System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

I tried to replace the multi-dimensinal array with jagged, and also remove the "cols/rows + 1" in loops, but it didn't help.

What it could be? Maybe it’s something about the nuances of C#. Because in C++, the code works, even though I didn’t change the algorithm.

If you need any additional information, please let me know and I will clarify. Thank you for your time.

  • Also I removed .Length from my code or GetLength() and replaced with constants `cols` and `rows`. – lrdfthflies Mar 28 '21 at 14:51
  • 2
    `Algorithmically identical code doesn't work in C#` -- yeah, C# doesn't allow memory corruption. Your code is broken. – Blindy Mar 28 '21 at 14:54
  • Blindy is correct, your code is actually inherrently broken and it's corrupting memory outside of the array in the C++ version; specifically because of the `j < cols + 1;` in both for loops which will eventually try to access `A[0, 4]`, and because of `j=0` & `A[i, j - 2]` in the second loop which attempts to access `A[1, -2]`. – Prime Mar 28 '21 at 14:58
  • This compiles and runs in C++ because in C++ indexers are just pointer offsets and can really go anywhere they want outside of the bounds of the memory that has been allocated for the array, whether as in C# the indexer exists on a functionally higher level which allowed for the ability to check if an index was outside of the bounds of its length. – Prime Mar 28 '21 at 15:01
  • Thanks, I didn’t write in C# before and didn't know that fact. How can I fix that? – lrdfthflies Mar 28 '21 at 15:02
  • Let me ask you something, what value is stored in `A[2, 2]`? – CodingYoshi Mar 28 '21 at 15:19
  • @lrdfthflies It's complicated because computing fibonacci numbers, which is a linear operation, across a 2d array is not ordinarily done AFAIK. Personally I would fill a normal array of `A = new long[rows * cols]`, populate that in one linear loop, then index into it like `A[rowindex * cols + colindex]`, which is structurally the same in memory it's just indexed into in such a way that populating it with the fibonacci sequence is easy. – Prime Mar 28 '21 at 15:27
  • That being said, I was able to completely re-write this to work on a 2d/jagged array; https://dotnetfiddle.net/e35wxV – Prime Mar 28 '21 at 15:35
  • But is it possible to do this through a two-dimensional array? – lrdfthflies Mar 28 '21 at 17:58

0 Answers0