COMP 40 Assignment: Interfaces, Implementations, and Images

Interfaces and design checklists for parts A and B Tuesday, September 20 at 11:59PM. Full assignment (all of parts A, B, C, D, and E) due Sunday, September 25 at 11:59PM.

Please read the entire assignment before starting work.

Purpose

This assignment has four goals:
  1. To spur you to think more deeply about programming technique
  2. To give you practice designing your own interfaces, not just using interfaces designed by other people
  3. To give you practice thinking about what familiar algorithms and data structures you can use to solve new problems
  4. To lay a foundation for future assignments. In these future assignments,
The abstractions you build in this assignment will help you represent and manipulate digital images.

Preliminaries

Part A: Two-Dimensional, Polymorphic, Unboxed Arrays

In the supplement to Hanson, Dave Hanson and I provide an abstraction that implements unboxed one-dimensional arrays. For this part of the assignment, you'll adapt the unboxed-array abstraction to support two-dimensional arrays. Your adaptation will be called UArray2 and should define the type UArray2_T. Your adaptation should include the following changes: As in Hanson's code, a reference out of bounds should result in a checked run-time error.

For part A, the problem you are to solve is define an interface and build an implementation for UArray2.
My solution to this problem takes about 100 lines of C code.

Hints:

Unpleasantness to watch out for:

Your life will be much easier if you follow the programming idioms for storing values into Hanson's arrays, which deal with most of these issues.

Part B: Two-Dimensional Arrays of Bits

In some cases, particularly for documents scanned at high resolution, it can be useful to represent an image as an array of bits. Each bit is either black (1) or white (0). To save space, it is useful to have a packed representation of such images. For this part of the lab, you'll design Bit2: an interface to support two-dimensional arrays of bits.

Hints:

For part B, the problem you are to solve is define an interface and build an implementation for Bit2.
My solution to this problem takes about 110 lines of C code.

Part C: Using the UArray2 abstraction to identify Sudoku solutions

Write the test program sudoku. It takes as input a single portable graymap file, which may be named on the command line or may be given on standard input. Your program must not print anything, but if the graymap file represents a solved sudoku puzzle, your program should call exit(0); otherwise it should call exit(1). A solved sudoku puzzle is a nine-by-nine graymap with these properties: Here's an example (which you can also view as an image):
P2
9 9
# portable graymap representing a sudoku solution
9 
1 2 3   4 5 6   7 8 9
4 5 6   7 8 9   1 2 3
7 8 9   1 2 3   4 5 6

2 3 4   5 6 7   8 9 1 
5 6 7   8 9 1   2 3 4 
8 9 1   2 3 4   5 6 7 

3 4 5   6 7 8   9 1 2
6 7 8   9 1 2   3 4 5
9 1 2   3 4 5   6 7 8
My solution to this problem takes about 120 lines of C code. There is a significant opportunity for abstraction; a Very Good solution will identify such opportunities and use them to avoid repeating code.

If sudoku is used in a way that violates its specification, it should terminate with a checked run-time error (any one will do). Read the specification carefully!

Part D: Using the Bit2 abstraction to remove black edges

Write the test program unblackedges, which removes black edges from a scanned image. Example:
===========>
Before After

The program unblackedges takes at most one argument:

Program unblackedges should print, on standard output, a portable bitmap file which is identical to the original file except that all black edge pixels should changed to white. You can find some sample images in /comp/40/images/bitonal; try, for example,
  pngtopnm /comp/40/images/bitonal/hyphen.png | ./unblackedges | display -

For a bitmap of size w by h, a black edge pixel is defined inductively as follows:

My solution to this problem takes about 110 lines of C code for the main problem, plus about 40 lines of code that I can reuse for other problems. John Dias suggested an even simpler solution that requires less than 70 lines of code for the main part, and John's solution runs 30% faster than mine.

Hints:

If unblackedges is used in a way that violates its specification, it should terminate with a checked run-time error (any one will do).

Part E: Programming technique

Meet with your partner and identify one programming technique that meets either of these two criteria: Your assignment is to describe the technique in enough detail that a student halfway through COMP 15 could put it into practice. Use at most one page.

You should submit your description in a plain text file called TECHNIQUE, or if you prefer to use a word processor, a PDF file called technique.pdf.

Expectations for your solutions

Your course instructor has thought of several ways to solve parts A and B. Representation is the essence of programming! Your major design decision will be how to represent a UArray2_T and a Bit2_T. Several obvious alternatives, all of which acceptable, are What is not acceptable is to clone and modify Hanson's implementation. Your new code should be a client of Hanson's existing code, and you should rely on Hanson to do the heavy lifting. Reuse his code as much as possible.

Another significant design decision is the type (prototype) of the apply function you'll use in your row-major and column-major traversals.

If you're concerned about performance, don't worry—we'll make a careful study of it throughout the term, and you'll have a chance to revisit and improve your implementation.

Common mistakes

Avoid these common mistakes:

Organizing and submitting your solutions