HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Show a diamond shape with numbers

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
showwithdiamondnumbersshape

Problem

After seeing this post on StackOverflow, I thought I'd try it out. What I have now is a diamond shape with numbers like this:

1
   212
  32123
 4321234
543212345
 4321234
  32123
   212
    1


My code looks like this:

int i, j, k, n = 6, l = 3, o = 2;
for (i = 1; i = 1; k--)
    {
        Console.Write(" ");
    }
    n--;
    for (j = i; j >= 1; j--)
    {
        Console.Write(j);
    }
    o = 2;
    for (j = 1; j = 1; i--)
{
    for (k = l; k >= 1; k--)
    {
        Console.Write(" ");
    }
    l++;
    for (j = i; j >= 1; j--)
    {
        Console.Write(j);
    }
    o = 2;
    for (j = i; j > 1; j--)
    {
        Console.Write(o);
        o++;
    }
    Console.WriteLine();
}
Console.Read();


I don't think this is a nice solution for this task. Is there any nicer and shorter way to solve this without using that many loops?

Solution

Code issues that decrease readability and maintainability

  • Plenty of loops.



  • Non-meaningful variable names: n, l, o.



We can declare the following helper methods:

private static string CreateDigitsString(int length)
{
    char[] buffer = new char[length];
    for (int i = 0; i < buffer.Length; i++)
    {
        buffer[i] = (char)('1' + Math.Abs(buffer.Length / 2 - i));
    }
    return new string(buffer);
}

private static string CreateLine(int length, int spaces)
{
    return new string(' ', spaces) + CreateDigitsString(length - 2 * spaces) + new string(' ', spaces);
}


Then use it like:

const int N = 5;
const int TotalRows = N * 2 - 1;

for (int row = 0; row < TotalRows; row++)
{
    Console.WriteLine(CreateLine(TotalRows, Math.Abs(N - row - 1)));
}


Another approach

In the 2-dimensional loop (i, j) we can calculate the distance between the current cell and the center of the diamond as follows:

int rowDistance = Math.Abs(row - N + 1);
int colDistance = Math.Abs(col - N + 1);


Let's output the colDistance for cells that are within rowDistance + colDistance <= N - 1, and space - otherwise.

const int N = 5;
const int TotalSize = N * 2 - 1;

for (int row = 0; row < TotalSize; row++)
{
    for (int col = 0; col < TotalSize; col++)
    {
        int rowDistance = Math.Abs(row - N + 1);
        int colDistance = Math.Abs(col - N + 1);
        Console.Write(rowDistance + colDistance <= N - 1 ? (char)('1' + colDistance) : ' ');
    }
    Console.WriteLine();
}

Code Snippets

private static string CreateDigitsString(int length)
{
    char[] buffer = new char[length];
    for (int i = 0; i < buffer.Length; i++)
    {
        buffer[i] = (char)('1' + Math.Abs(buffer.Length / 2 - i));
    }
    return new string(buffer);
}

private static string CreateLine(int length, int spaces)
{
    return new string(' ', spaces) + CreateDigitsString(length - 2 * spaces) + new string(' ', spaces);
}
const int N = 5;
const int TotalRows = N * 2 - 1;

for (int row = 0; row < TotalRows; row++)
{
    Console.WriteLine(CreateLine(TotalRows, Math.Abs(N - row - 1)));
}
int rowDistance = Math.Abs(row - N + 1);
int colDistance = Math.Abs(col - N + 1);
const int N = 5;
const int TotalSize = N * 2 - 1;

for (int row = 0; row < TotalSize; row++)
{
    for (int col = 0; col < TotalSize; col++)
    {
        int rowDistance = Math.Abs(row - N + 1);
        int colDistance = Math.Abs(col - N + 1);
        Console.Write(rowDistance + colDistance <= N - 1 ? (char)('1' + colDistance) : ' ');
    }
    Console.WriteLine();
}

Context

StackExchange Code Review Q#146132, answer score: 7

Revisions (0)

No revisions yet.