Home > database >  Draw hourglass in console
Draw hourglass in console

Time:12-17

I got problem with draw the bottom part of hourglass. I got code of the middle if the number if not even but i don't paste it. I tried to reverse the top but it doesn't work.

public static void hourglass(int n)
        { 
            // Top
            for (int i = 0; i < n / 2; i  )
            {
                for (int j = 0; j < i; j  )
                {
                    Console.Write(" ");
                }

                Console.Write("*");

                for (int j = 0; j < n - 2 - 2 * i; j  )
                {
                    if (i == 0)
                    {
                        Console.Write("*");
                    }
                    else
                    {
                        Console.Write(" ");
                    }
                }
                Console.WriteLine("*");
            }
            // middle if not even   

            // Bottom
        }

if n = 8 the hourglass should looks like:

********
 *    *
  *  *
   **
   **
  *  *
 *    *
********

if n = 7

*******
 *   *
  * *
   *
  * *
 *   *
*******

CodePudding user response:

I will try to help the best that I can.

using System;
//First lets draw a cordinate system to see what the values are in each step of the loop (We start from one just to make it easer to see the last row)
DrawMatrix(7);
Console.WriteLine();
DrawMatrix(8);
Console.WriteLine();
Console.WriteLine();
//from the cordinate system we can see that the top and bottom edges are just first (1,...) row and last row (7,...) and since row is the y coordinate the condition is y==1 || y == size 
DrawEdge(7);
Console.WriteLine();
DrawEdge(8);
Console.WriteLine();
Console.WriteLine();
// for the left slash we can see that it will be on the position where x == y 
DrawLeftToRight(7);
Console.WriteLine();
DrawLeftToRight(8);
Console.WriteLine();
Console.WriteLine();
//fot he other slash the sum of the two will be size -1 so we need to have a x y == size 1
DrawRightToLeft(7);
Console.WriteLine();
DrawRightToLeft(8);
Console.WriteLine();
Console.WriteLine();
//Now lets combine everything 
DrawHourglass(7);
Console.WriteLine();
DrawHourglass(8);
Console.WriteLine();
DrawHourglass(9);
Console.WriteLine();
DrawHourglass(10);
Console.WriteLine();
Console.WriteLine();



void DrawMatrix(ushort size)
{
    for(var y = 1; y <= size; y  )
    {
        for(var x = 1; x <= size; x  )
        {
            Console.Write($"({y},{x})");
        }
        Console.WriteLine();
    }
}

void DrawEdge(ushort size)
{
    for(var y = 1; y <= size; y  )
    {
        for(var x = 1; x <= size; x  )
        {
            if(y==1 || y == size)
            { 
                Console.Write('*'); 
            }
            else
            { 
                Console.Write(' ');
            }
        }
        Console.WriteLine();
    }
}


void DrawLeftToRight(ushort size)
{
    for(var y = 1; y <= size; y  )
    {
        for(var x = 1; x <= size; x  )
        {
            if(x==y)
            { 
                Console.Write('*'); 
            }
            else
            { 
                Console.Write(' ');
            }
        }
        Console.WriteLine();
    }
}

void DrawRightToLeft(ushort size)
{
    for(var y = 1; y <= size; y  )
    {
        for(var x = 1; x <= size; x  )
        {
            if(x y==size 1)
            { 
                Console.Write('*'); 
            }
            else
            { 
                Console.Write(' ');
            }
        }
        Console.WriteLine();
    }
}



void DrawHourglass(ushort size)
{
    for(var y = 1; y <= size; y  )
    {
        for(var x = 1; x <= size; x  )
        {
            if( (y==1 || y == size) 
                || (x==y)
                || (x y==size 1)
                )
            { 
                Console.Write('*'); 
            }
            else
            { 
                Console.Write(' ');
            }
        }
        Console.WriteLine();
    }
}

Here is a dot net fiddle link https://dotnetfiddle.net/i6Bay3 I tried to make as simple as possible to understand so you can play around with this to try to make it work in different ways.

CodePudding user response:

If I understand your Q correctly. I think this is all you need to do :)

Image of console with hourglass

            for (int i = 0; i < size; i  )
            {
                for (int j = 0; j < size; j  )
                {
                    if (i == 0 || i == size - 1)
                    {
                        Console.Write("*");
                    }
                    else if (j == i || j == size - 1 - i)
                    {
                        Console.Write("*");
                    }
                    else
                    {
                        Console.Write(" ");
                    }
                }
                Console.WriteLine();
            }
  • Related