Home > OS >  How to print a diamond pattern outline with C#
How to print a diamond pattern outline with C#

Time:11-21

Here's the code of a solid diamond and I want to remove the middle and leave the edges.

From this,

enter image description here

to this,

enter image description here

public void DiamondOne()  
       {  
           int i, j, count = 1, number;  
           Console.Write("Enter number of rows:");  
           number = int.Parse(Console.ReadLine());  
           count = number - 1;  
           for (j = 1; j <= number; j  )  
           {  
               for (i = 1; i <= count; i  )  
                   Console.Write(" ");  
               count--;  
               for (i = 1; i <= 2 * j - 1; i  )  
                   Console.Write("*");  
               Console.WriteLine();  
           }  
           count = 1;  
           for (j = 1; j <= number - 1; j  )  
           {  
               for (i = 1; i <= count; i  )  
                   Console.Write(" ");  
               count  ;  
               for (i = 1; i <= 2 * (number - j) - 1; i  )  
                   Console.Write("*");  
               Console.WriteLine();  
           }  
           Console.ReadLine();  
  }  

CodePudding user response:

I would go with something like this:

    public void Diamond()
    {
        Console.WriteLine("Enter number of rows:");
        bool isNumber = int.TryParse(Console.ReadLine(), out int rowsNr);

        if (!isNumber)
        {
            Console.WriteLine("Not a number!");
            return;
        }

        // print the upper half
        for (int rowIndex = 0; rowIndex < rowsNr - 1; rowIndex  )
        {
            for (int colIndex = 0; colIndex <= 2 * rowsNr; colIndex  )
            {
                if (colIndex == Math.Abs(rowsNr - rowIndex) || colIndex == Math.Abs(rowsNr   rowIndex))
                {
                    Console.Write("*");
                }
                else
                {
                    Console.Write(" ");
                }
            }
            Console.WriteLine();
        }

        // print the lower half
        for (int rowIndex = 1; rowIndex <= rowsNr; rowIndex  )
        {
            for (int colIndex = 0; colIndex <= 2 * rowsNr; colIndex  )
            {
                if (colIndex == rowIndex || colIndex == 2 * rowsNr - rowIndex)
                {
                    Console.Write("*");
                }
                else
                {
                    Console.Write(" ");
                }
            }

            Console.WriteLine();
        }
    }

Basically you are looking for the x and y coordinates. Each of the 4 edges has its own equation (the upper-left one is x == y for instance) and you check for these while you iterate. I split the code in two parts, one for the upper half and one for the lower half. It's more readable and you don't put together too many if statements and lose the meaning of the code.

CodePudding user response:

This is what I came up with:

// Get the number of rows
int rows;
do
{
    Console.WriteLine("Enter number of rows:");
} while (!int.TryParse(Console.ReadLine(), out rows));

// Print diamond
DiamondOne(rows, Console.Out);

// Wait for key press
Console.WriteLine("Press any key to exit");
Console.ReadKey(true);

static void DiamondOne(int rows, TextWriter output)
{
    for (int currentRow = 0; currentRow < rows; currentRow  )
    {
        OutputRow(rows, output, currentRow);
    }

    for (int currentRow = rows - 2; currentRow >= 0; currentRow--)
    {
        OutputRow(rows, output, currentRow);
    }
}

static void OutputRow(int rows, TextWriter output, int currentRow)
{
    int indentation = rows - currentRow - 1;
    int diamondCentre = Math.Max((currentRow * 2) - 1, 0);

    output.Write(new string(' ', indentation));
    output.Write('*');
    output.Write(new string(' ', diamondCentre));
    if (currentRow != 0) output.Write('*');
    output.WriteLine();
}
  •  Tags:  
  • c#
  • Related