in Development

Back to the University

Past week, I was speaking with a guy who just started his first year at Computer Science, he explained me the task he needs to do and I felt some nostalgia about those days when you should try to solve "impossible" algorithms, learning everyday new development techniques … mmm that doesn’t sound that different than nowadays … anyway …

I found funny to come back to the University and I decided to try it myself, I took a paper and a pen and I wrote the pseudocode. Believe me if I say that I asked the guy to keep me informed about all his assignments, it was funnier than doing a sudoku. 

The idea is to draw a symmetric rhombus using ASCII chars, each concentric rhombus must be drawn using a different ASCII char. Next you can see a C# version of it, they use Modula-2 as programming language, so I didn’t use any fancy .net features.

   1: class Program
   2: {
   3:    const int startingAsciiCode = 32;
   4:    const int maxSize = 40;
   5:    static void Main(string[] args)
   6:    {
   7:       while (true)
   8:       {
   9:          Console.Clear();
  10:          Console.WriteLine("Please insert the size: (ctrl + c to exit)");
  11:          string size = Console.ReadLine();
  12:          int parsedSize;
  13:          if (!int.TryParse(size, out parsedSize) || (parsedSize < 2 || parsedSize > 94))
  14:          {
  15:             Console.WriteLine("Please introduce a valid integer value between 2 and 94");
  16:             Console.ReadKey();
  17:          }
  18:          else
  19:          {
  20:             Console.Clear(); 
  21:             
  22:             // we calculate the diagonal size
  23:             int diagonal = (parsedSize * 2) - 1;
  24:             int charsToDraw = 1;
  25:             // Iterate all the rows
  26:             for (int row = 1; row <= diagonal; row++)
  27:             {
  28:                // Calculate the blank spaces and the total amount of chars to draw
  29:                int blankSpaces = (int)((diagonal - charsToDraw) / 2);
  30:                int totalSize = blankSpaces + charsToDraw; 
  31:                
  32:                // Iterate the columns
  33:                for (int i = 1; i <= totalSize; i++)
  34:                {
  35:                   if (i < blankSpaces)
  36:                   {
  37:                      // Draw blank spaces
  38:                      Console.Write(' ');
  39:                   }
  40:                   else
  41:                   {
  42:                      // Draw non blank chars
  43:                      // We use a different char for each concentric rhombus
  44:                      int asciiOffset;
  45:                      if (i <= parsedSize)
  46:                      {
  47:                         asciiOffset = i - blankSpaces;
  48:                      }
  49:                      else
  50:                      {
  51:                         asciiOffset = (totalSize + 1) - i;
  52:                      }
  53:                      Console.Write((Char)(startingAsciiCode + asciiOffset));
  54:                   }
  55:                }
  56:                Console.WriteLine();
  57:                if (row < parsedSize)
  58:                {
  59:                   charsToDraw = charsToDraw + 2;
  60:                }
  61:                else
  62:                {
  63:                   charsToDraw = charsToDraw - 2;
  64:                }
  65:             }
  66:             Console.WriteLine();
  67:             Console.WriteLine("Press a key to continue");
  68:             Console.ReadKey();
  69:          }
  70:       }
  71:    }
  72: }