Due at the beginning of your lab section

Goals for this Pre-lab

  • Familiarize yourself with the lab so you are ready to go
  • Lean about test-driven development

Remember to create a new directory in your repository for this lab.

$  cd CNET-cs152-win-19
$  mkdir hw2
$ svn add hw2
$  cd hw2
  • To prepare for lab, you need to read about Test-Driven Development and answer the questions in lab2questions.html. You will implement the functions in the warmup during lab. Print out the questions and turn them in within the first 5 minutes of your lab session.

Step 1: Test-Driven Development

Create a new file in your hw2 directory named lab2questions.txt and add it to the repository. A quick way to do this is to use the unix command touch. This will create and empty file and give it a name. Then you can add it to the repository. This way, when you commit later, you don't need to remember to add it!

$ touch lab2questions.txt
$ svn add lab2questions.txt
$ vi lab2questions.txt

Now copy and paste the following questions into the file. Fill in the answers as you read the wikipedia entry on Test-driven Development.

Step 2: Skeleton code

During the warmup, you are going to implement several functions that exercise control, iteration, and recursion.

In order to spend your time efficiently in lab, you need to start with a skeleton that compiles. Then you can start on development right away with the TAs there to help you. Starting next week, you will be expected to write skeleton code prior to lab. This week, however, I have given it to you. You need to get used to the new three-file format for writing programs. Read through this section to familiarize yourself with the code and answer the questions in lab2questions.txt in preparation for lab.

From this week on, all code will be split into three files. The exact names of the files depend on the assignment. Many assignments will have two sets of files: warmup# and hw#.

File #1: assign_main.c It contains:

  • main function
  • All tests helper functions
File #2: assign.c It contains:
  • All functions in the assignment.
  • Any helper functions you write that are called by functions in the file
File #3: assign.h It contains:
  • The prototypes of all assigned functions in functions.c
At the top of assign_main.c and assign.c, immediately after any system file include statements, you will write:
#include "assign.h"

This time, I have given you the functional skeletons of the three files so you can see what goes into each file. Scan them and answer the questions in lab2questions.txt. warmup2_main.c, warmup2.h, and warmup2.c.

Exercises

Exercise 1

void print_asterisk_letter(char letter);

Given a character, print the corresponding upper-case letter of the alphabet using asterisks. The letter can be 'N', 'O', 'P', or 'R'. The set of letters are here.



Exercise 2

Draw a sideways wedge recursively. The width is the number of asterisks in the first and last rows. The number of asterisks in a row always increases by one each row until it reaches the middle. Then, at the bottom, it also decreases by one each row until there is the width. You must use a recursive solution. You may write more than one function if you wish.

void draw_sideways_wedge_rec(unsigned int width, unsigned int height);
draw_sideways_wedge_rec(3,10) results in:

***
****
*****
******
*******
*******
******
*****
****
***

Exercise 3

Draw a sideways trapezoid iteratively. The functionality is identical to draw_sideways_wedge_rec, but you may not have any function calls (other than printf) within the function.

void draw_sideways_wedge_iter(unsigned int width, unsigned int height);
draw_sideways_wedge_iter(3,10) results in:

***
****
*****
******
*******
*******
******
*****
****
***

Exercise 4

Draw a set of rows of asterisks, with each row having half as many asterisks as the row above it. The last row drawn has 1 asterisk in it. This function must be implemented recursively.

void draw_halves_down(unsigned int width);
draw_halves_down(20) results in rows of 20, 10, 5, 2, and 1:

********************
**********
*****
**
*


Exercise 5

Draw a set of rows of asterisks, with each row having half as many asterisks as the row below it. The first row drawn has 1 asterisk in it. This function must be implemented recursively.

void draw_halves_up(unsigned int width);
draw_halves_up(20) results in rows of 1, 2, 5, 10, and 20

*
**
*****
**********
********************
It returns the number 5.

Step 3: Compile your skeleton

Create your Makefile to compile your code.

You need a name of the target, the files on which it depends, and the exact compile line. We will name our target warmup2.

warmup2: warmup2.c warmup2_main.c warmup2.h
	clang warmup2_main.c warmup2.c  -o warmup2

Make sure that it is a tab before the word clang, not just spaces.

If this is the only thing in your Makefile or if it is the first target listed, then you compile with:

$  make

Otherwise, compile with:

$  make warmup2