Next: About this document

V22.0101 Homework Assignment 2 Fall 1998
Digital Displays

Due: Thurs Oct 8, 11:59 pm.

Microwave ovens, clock radios, electronic watches etc. typically use a standard digital display where each digit consists of 7 bars (line segments) which are either lighted or not. For example, the digit 8 has all bars lighted, while the digit 3 has all but two lighted, like this:

        -     -
       | |     |
        -     -
       | |     |
        -     -

This assignment asks you to read a sequence of integers from the input, and display them in the bar format using Turtle graphics.

Start by writing

  procedure draw(xstart, ystart: integer;
    top, middle, bottom, upperleft, lowerleft, upperright, lowerright: boolean);
This procedure should use Turtle graphics to draw a 7-bar digit. The turtle starts at pixel (xstart,ystart), which is the middle left of the digit, and moves along the 7 bars making up the digit 8. The turtle's pen is either down or up for each bar depending on whether the corresponding Boolean parameters top, middle, etc. are true or false (this is the only place in the program where you need to use the if statement). So, for example, the digit 8 is drawn if all Boolean parameters are true, and the digit 3 is drawn if all but upperleft and lowerleft are true. The size of the digit should be chosen so you can fit 9 digits across the screen and 7 from top to bottom. Debug this procedure by running it for various input values (including combinations of bars which are not valid digits) before going on to the next part.

Procedure draw is at the lowest level of our design and will be called by the next procedure you should write:

  procedure process_digit(xstart, ystart: integer; digit: char);
  begin
     case digit of
        '3': draw(xstart, ystart, true, true, true, false, false, true, true);
        '8': draw(xstart, ystart, true, true, true, true, true, true, true);
      ...
     end
  end.
This procedure uses the case statement to call draw with the correct choice of parameters to draw the digit in each case. The case statement is discussed on p.302 of the text. You need to fill in lines for the other digits. Debug this procedure by running it for all the different digits (and different values of xstart, ystart) before going on to the next part. Note that digit has type char, not integer.

Procedure process_digit is at the middle level of our design and should be called by the next procedure you should write:

  procedure process_line(ystart: integer);
This procedure reads an 8-digit number from the input and, as each digit is read, calls process_digit to display it. Zeros are represented explicitly, so the number 123 would be input as 00000123. It is easiest to read the digits as character input.

Use the readkey statement to read each character without echoing it. The function readkey waits for a key to be struck. When this is done the character sent by the key does not appear on the screen and the character is immediately transferred to the program with out the user hitting the Enter key. In order to use readkey, you must place uses crt; immediately after the program heading. An example of its use is letter := readkey. letter must be declared a char variable.

The tricky part is making sure xstart, ystart are set correctly each time you call process_digit, moving across the screen from left to right. Notice that ystart is a parameter supplied to process_line specifying the vertical position to write on the screen, but xstart, which specifies the horizontal position, is set to several different values inside process_line. Leave some space on the left before drawing the first digit (you will find out why later). Debug this procedure by running it for various choices of 8-digit inputs before going on to the next part.

Procedure process_line is at the top level of our design. The main program now calls process_line 6 times, processing exactly 6 lines of input, each of which is an 8-digit number. This should be done using another for loop. Each 8-digit number should appear on a different line in the graphics screen, in a visually appealing way (e.g. no overlaps or giant spaces!). Some space should be left at the bottom (see next paragraph).

Once your program to accomplish all this is working, modify it further to add up the numbers, draw a line under the last one, and write out the sum below, remembering that you need one extra space on the left for a carry digit in the sum. This makes the assignment trickier, because you need to translate the character input into an integer variable (use type longint as the 2 bytes reserved for type integer are not enough for 8 digits). Procedure process_line needs to do this as it reads the input characters, translating the digit characters to digit integers with the ord function (p.183 of the text), multiplying the last digit by 1, the second-to-last by 10, etc., and adding these values to get the 8-digit integer. This longint should be passed back to the main progam as a var parameter, so that main program can add these numbers up in the for loop. You then need to separate the digits of the sum as explained on p.78 of the text, and write them out at the bottom of the screen using another procedure.

An alternative solution would be to read all the input as longint variables, separate them into digits and then display them, instead of using readkey. But this is not as nice since then the digits are not displayed as they are read, but only after the whole line is read.

You may like to keep your main program simpler by calling a procedure process_all_lines to do everything, i.e. make the repeated calls to process_line, add the numbers up, and display the sum.

Your program should be clearly organized and well documented with comments. It should not use global variables. All variables should be declared only in the procedures (or main program) where they are needed. You can be creative with your use of color, but this is optional. Avoid using sound, as it can be annoying to the other students in the lab.

Steps to use in writing the program

A solution demo is available on the PC network in the ACF computer lab, in the file DIGITS.EXE in the directory Z:\courses\v220101. (The demo asks you to specify the number of digits per line and number of lines instead of using 8 and 6 respectively, but you should not include this feature in this assignment.)





Michael Overton
Wed Sep 23 15:14:45 EDT 1998