0

I'm learning C# (coming from Ruby) and decided to dive into multidimensional arrays. To do this, I decided to solve an "Image Blur" problem using C#. This involves finding all "1"'s inside the multi-array, and changing all the values above, below, to the right and to the left of the "1" with a "1".

For example:

0000
0010
0000
0000

Should turn into:

0010
0111
0010
0000

In my blur(int[,]) method (line 26) I'm getting an IndexOutOfRangeException. I'm not sure why, because as far as I can see the point I'm trying to modify is inside the range of the array. Can anyone please help me out?

Thanks in advance!

using System;


/* For every topic that I had to research, I wrote a REF down below (at the end of the
 * document). They are all numbered and have a link to the resource. */

namespace ImageBlurCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] multiarray =     { { 0, 0, 0, 0 },
                                      { 2, 0, 1, 0 },
                                      { 3, 0, 0, 0 },
                                      { 4, 0, 0, 0 } };


            Blur(multiarray);


            OutputArray(multiarray);
            Console.ReadLine();
        }

        public static void Blur(int[,] multiarray)
        {
            int[,] blurredArray = multiarray;

            for (int row = 0; row < multiarray.GetLength(0); row++)
            {
                for (int col = 0; col < multiarray.GetLength(1); col++)
                {
                    int point = multiarray[row, col];

                    if (point == 1)
                    {
                        blurredArray[row - 1, col] = 1;
                        blurredArray[row + 1, col] = 1;
                        blurredArray[row, col - 1] = 1;
                        blurredArray[row, col + 1] = 1;
                    };

                }
                OutputArray(blurredArray);
            }
        }

        // Method outputs 2d array to the console.
        static void OutputArray(int[,] multiarray)
        {
            // REF#1 - Difference between .GetLength() and .Length()
            for (int row = 0; row < multiarray.GetLength(0); row++)
            {
                for (int col = 0; col < multiarray.GetLength(1); col++)
                {
                    int s = multiarray[row, col];
                    Console.Write(s);
                }
                Console.WriteLine("");
            }
        }


    }
}


/* REF:
    - Difference between Array.GetLength() and Array.Length():
    http://stackoverflow.com/questions/2044591/what-is-the-difference-between-array-getlength-and-array-length

*/
  • Standard duplicate covers your exception (what do you expect from `blurredArray[4 + 1, 0] = 1;`?). Note that it is likely that `int[,] blurredArray = multiarray;` is not doing what you expect - you may need to read on https://www.bing.com/search?q=c%23+array+copy+vs+assign – Alexei Levenkov Feb 18 '17 at 00:53
  • Because first, you get to the '1' in point (1,2), and set all surrounding points to one. Then, you get to point (1,3), which is now also 1. And then when you try to set the index at col+1, col+1=4, which is out of range. To solve this, create working copy of the array and set changes to that array, instead of the original array. – GregorMohorko Feb 18 '17 at 00:53
  • @AlexeiLevenkov Ah... That's exactly it. Thank you so much :)! – Eric Crescioni Feb 18 '17 at 02:16
  • @GregaMohorko Thanks :)! I Like Alexei said, I need to find a way to duplicate the array. – Eric Crescioni Feb 18 '17 at 02:17

0 Answers0