Please help me with the task: Draw lit area

Hello, I’m working on this task:

Next, take the class Point from the previous task and rename it to Point2D (2D stands for two-dimensional). Also take the LitArea class and rename it to LitArea2D. Add a public method Draw() to the LitArea2D class that takes no arguments and returns void. In the Main method, read four integers from the console, each from the new line: xTopLeft , yTopLeft , xBottomRight, and yBottomRight . Create an object of type LitArea2D, passing these four integers as parameters to a constructor. These four integers represent coordinates on a 2D grid: the coordinate system starts in the top left corner; X axes are horizontal, and Y are vertical. See the following figure for an example. Implement method Draw() in the class LitArea2D to draw an area using ASCII art, according to integer coordinates that the user input. The dark area to the left and above the lit area should be indicated with a dot (“.”), and the lit area itself with a pound sign (“#”). Then, call this method from Main. For example:
>2
>3
>5
>6



…####
…####
…####
…####
/* In the above example, the lit area starts at point [2, 3] and ends at point [5, 6] */

Example 2:
>0
>0
>2
>4

/* In the above example, the lit area starts at point [0, 0] and ends at point [2, 4] */

Here is my code:


namespace AccessModifiers
{
   class Point2D
   {
       public int X;
       public int Y;
   }

   class LitArea2D
   {
       private Point2D _topLeft;
       private Point2D _bottomRight;

       public LitArea2D(int xTopLeft, int yTopLeft, int xBottomRight, int yBottomRight)
       {
           _topLeft = new Point2D
           {
               X = xTopLeft,
               Y = yTopLeft
           };

           _bottomRight = new Point2D
           {
               X = xBottomRight,
               Y = yBottomRight
           };
       }

       public void Draw()
       {
           for (int i = 0; i <= _bottomRight.X; i++)
           {

               if (i < _topLeft.Y)
               {
                   for (int j = 0; j < _bottomRight.X; j++)
                   {
                       Console.Write(".");
                   }
                   Console.WriteLine(".");
               }


               if (i >= _topLeft.Y && i <= _bottomRight.Y)
               {


                   for (int j = 0; j <= _bottomRight.X; j++)
                   {

                       if (j < _topLeft.X)
                       {
                           Console.Write(".");
                       }

                       if (_topLeft.X <= j && j <= _bottomRight.X)
                       {
                           Console.Write("#");
                       }
                   }
                   Console.WriteLine();
               }
           }
       }
   }

   class DrawLitArea
   {
       public static void Main()
       {
           int xTopLeft = int.Parse(Console.ReadLine());
           int yTopLeft = int.Parse(Console.ReadLine());
           int xBottomRight = int.Parse(Console.ReadLine());
           int yBottomRight = int.Parse(Console.ReadLine());

           var someLitArea = new LitArea2D(xTopLeft, yTopLeft, xBottomRight, yBottomRight);
           someLitArea.Draw();


       }

   }
}

At the first glance the code works as it should, but the platform shows an error:
image
Tested this input and can’t understand what’s wrong.
Can anyone please help me to find my mistake?

Just checking the test results I can see, that you’re missing one last line. Take the first example and compare your output with example above:
Your output:

>2
>3
>5
>6
......
......
......
..####
..####
..####

But to fix the problem, I’d suggest same as to Maria: to fill any 2 dimensional array it’s much-much better to use for in for:

for (int i = 0; i <= hight; i++) 
{
      for (int j = 0; j <= lengthOfRow; j++)
      {
            //if your condition to print one sign
            {
                Console.Write(".");
            }
            else
            {
                Console.Write("#");
            }
      }
      Console.WriteLine();
}
1 Like

Great! Now it works, thank you so much. Your for loop is much more laconic and elegant :slight_smile: