Programming in D – Solutions

Functions

  1. import std.stdio;
    
    void printMenu(string[] items, int firstNumber) {
        foreach (i, item; items) {
            writeln(' ', i + firstNumber, ' ', item);
        }
    }
    
    void main() {
        string[] items =
            [ "Black", "Red", "Green", "Blue", "White" ];
        printMenu(items, 1);
    }
    
  2. Here are some ideas:
    • Write a function named drawHorizontalLine() to draw horizontal lines.
    • Write a function named drawSquare() to draw squares. This function could take advantage of drawVerticalLine() and drawHorizontalLine() when drawing the square.
    • Improve the functions to also take the character that is used when "drawing". This would allow drawing each shape with a different character:
      void putDot(Canvas canvas, int line, int column, dchar dot) {
          canvas[line][column] = dot;
      }