-1

I'm trying to create a method that draws a rectangle like this:

--------------
|            |
| ********** |
| ********** |
| ********** |
| ********** |
| ********** |
| ********** |
| ********** |
| ********** |
|            |
--------------

I want to display the whole array matrix in console, but when I try to run the program, I get this error:

Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.

I was trying to fix this for hours, but can't find the problem. Here's my code:

public static class Canvas
{
    public static char[,] Draw (uint width, uint height)
    {
        char[,] page = new char[width + 4, height + 4];

        for (uint i = 1; i < width + 3; ++i)
        {
            for (uint j = 1; j < height + 3; ++j)
            {
                page[i, j] = '1';
            }
        }

        for (uint i = 0; i < width + 5; ++i)
        {
            page[0, i] = '-';
        }
        for (uint i = 0; i < width + 5; ++i)
        {
            page[height + 4, i] = '-';
        }
        for (uint j = 1; j < height + 4 ++j)
        {
            page[j, 0] = '|';
        }
        for (uint j = 1; j < height + 4; ++j)
        {
            page[j, width + 4] = '|';
        }
        return page;
   }
}
Tobias Tengler
  • 6,848
  • 4
  • 20
  • 34
nanorobo
  • 29
  • 2
  • 1
    The error message speak for itself. You used an array index which is greater than the array. Keep in mind that an array in C# starts with index 0. char[4] means index must be 0..3. You use `i < width +5`in your loops, but the array size is `i+4`, that means the largest index is `i+3`. – H.G. Sandhagen Jun 22 '19 at 16:41
  • 1
    I think you switched height and width in the 4th and 6th for loop when accessing the page at an index. You declared width to be the first dimension and height the second, but you use `height + 4` and `width + 4` in the wrong place. – Tobias Tengler Jun 22 '19 at 16:41
  • 1
    Possible duplicate of [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – Ňɏssa Pøngjǣrdenlarp Jun 22 '19 at 16:45

1 Answers1

1

Firstly, I don't know if it was a typo, but your forth FOR loop is missing a ";" after the second argument.

Secondly, there is a difference between ++i and i++. In a for loop, usually i++ is used, because it firstly uses the value, then adds 1. If you use ++i it will fisty add 1 then use the value. This is throwing your counter (i) out of the array boundary.

And after that, in some loops you used Height +4 or Width +4 to write on the array, but this results in a position out of the array, because arrays starts at 0, and you used Height +4 and Width +4 on the array constructor.

Your code with comments:

public static class Canvas
{


    public static char[,] Draw(uint width, uint height)
    {
        char[,] page = new char[width + 4, height + 4];

        for (uint i = 1; i < width + 3; i++) //goes from 1 to 4 - 0 is null
        {
            for (uint j = 1; j < height + 3; j++) //goes from 1 to 4 - 5 is null
            {
                page[i, j] = '1';
            }
        }

        for (uint i = 0; i < width + 5; i++)  // goes from 0 to 6, but array ends at 5
        {
            page[0, i] = '-';
        }
        for (uint i = 0; i < width + 5; i++) // goes from 0 to 6, but array ends at 5
        {
            page[height + 4, i] = '-'; //the argument "height +4" throw the position out of the array, because arrays starts at 0
        }
        for (uint j = 1; j < height + 4; j++)
        {
            page[j, 0] = '|';
        }
        for (uint j = 1; j < height + 4; j++)
        {
            page[j, width + 4] = '|'; //the argument "width +4" throw the position out of the array, because arrays starts at 0
        }
        return page;
    }
}

New code:

    public static class Canvas2
{


    public static char[,] Draw(uint width, uint height)
    {
        char[,] page = new char[width + 4, height + 4];

        for (uint i = 1; i < width + 3; i++) 
        {
            for (uint j = 1; j < height + 3; j++) 
            {
                page[i, j] = '1';
            }
        }

        for (uint i = 0; i < width + 4; i++) 
        {
            page[0, i] = '-';
        }
        for (uint i = 0; i < width + 4; i++)
        {
            page[height + 3, i] = '-';
        }
        for (uint j = 1; j < height + 4; j++)
        {
            page[j, 0] = '|';
        }
        for (uint j = 1; j < height + 4; j++)
        {
            page[j, width + 3] = '|';
        }
        return page;
    }
}

Output:

enter image description here