Programming contests

20 Programming Exercise for Beginners

January 1, 2019 12:00 AM – December 31, 2019 12:00 AM

Grid Drawing

Create an application which draws a grid using plus, dot, hyphen, and vertical bar characters.

Input Specification

The input consists of two lines, each terminated by a newline character. The first line contains the width, the second line contains the height of the grid, measured in number of cells.

Sample Input

  1. 3
  2. 2
download as text file

Output Specification

The output must be a grid with the specified amount of cells. A cell is denoted by a single dot. The top and bottom borders of each cell are represented by hyphens (one for each), the left and right borders are represented by vertical bars (one for each), and plus signs must be used in the corners.

Output for Sample Input

  1. +-+-+-+
  2. |.|.|.|
  3. +-+-+-+
  4. |.|.|.|
  5. +-+-+-+
download as text file

Hints and Guide

Note that the number of lines in the output is 2 * height + 1 and the number of characters in each line is 2 * width + 1. Every second character in every second line belongs to a cell, the others belong to some border. In odd lines plus signs and hyphens appear, in even lines we could find only vertical bars and dots. Based on this observation, we can determine the next output character with the help of the parity of the row and column indexes only.

One possible solution is available in: main.c.

Possible Improvements

Generating the output character by character can be slow, you can make the application faster if you create two strings for the two types of output lines and then draw them using the puts function.

Acknowledgement This work was supported by the construction EFOP-3.4.3-16-2016-00021. The project was supported by the European Union, co-financed by the European Social Fund.
University of Debrecen; Faculty of Informatics; v. 03/01/2019