Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To solve upside-down rendering?

I have written this segment of C# to help me understand how nested for loops can be used to render 2 dimensional data.

Here is what the output looks like.

████
███
██
█

I would like to make it so that the 4 blocks up top are rendered at the bottom, basically in the reverse order so that the steps ascend. However the console window only renders downward, so the conventional thinking won't be right. The following is my code.

static void Main(string[] args)
    {
        int i = 0;
        int j = 0;

        for (i = 0; i < 4; i++) 
        {
            Console.Write('\n');
            for (j = i; j < 4; j++) 
            {
                Console.Write("█");
            }
        }
        Console.ReadKey();
    }

This is what I'd like the output to look like.

    █
   ██
  ███
 ████
like image 906
Monte Emerson Avatar asked Nov 30 '25 16:11

Monte Emerson


1 Answers

You need to reverse your loop condition from inremant to decremant..

for (i = 0; i < 4; i++) 
{
    Console.Write('\n');
    for (j = i; j >= 0; j--) 
    {
        Console.Write("█");
    }
}

Output will be;

enter image description here

Here is a DEMO.

UPDATE: Since you change your mind, you need to add space every column (column number isi) 4 - 1 times.

   public static void Main(string[] args)
    {
        int i = 0;
        int j = 0;
        for ( i = 0; i < 4; i++ )
        {
            for ( j = 0; j < 4; j++ )
            {
                if ( j < 3 - i )
                    Console.Write(" ");
                else
                    Console.Write("█");
            }
            Console.Write('\n');
        }

        Console.ReadKey();
    }

enter image description here

Here is a DEMO.

like image 139
Soner Gönül Avatar answered Dec 02 '25 06:12

Soner Gönül



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!