Goals for this Warmup

  • Introduce you to Duet Programming
  • Practice programming with control, iteration, and recursion.

For this lab, you are welcome to get technical help from another pair on how to use or install any of the tools involved. You may also get syntax help on C. You may not, however, get help on the algorithmic portion of the exercise outside of your partner, office hours, piazza questions to TAs or instructors.

This lab is broken down into several steps:

Set up

You should have already created a hw2 directory and completed the pre-lab.

Meet up with your duet programming partner for today. Log on to adjacent computers and bring up your individual accounts.

You are being asked to write four functions. Go through all steps of the Duet Programming protocol for each function, trading roles each function. You can see the function descriptions in the prelab. Decide now who is going to be the first Strategist and who is going to be the first Implementer.

Exercises

Follow the Duet Programming Protocol. At different times, you may want to look at the code the other person wrote. To do so, you will use scp, which allows you to copy a file from someone else's directory (as long as they enter their password).

For the Strategist to obtain the code from the Implementer:
scp Implementercnetid@linux.cs.uchicago.edu:~/152/hw2/warmup2.c .
Then the Implementer enters his/her password.
Likewise, for the Implementer to obtain the test code:
scp Strategistcnetid@linux.cs.uchicago.edu:~/152/hw2/warmup2_main.c .


Below are more details on the exercises.

Problem 1

char print_letter(unsigned int number);

Context: Every character has an ASCII value. This means that 'a' has one value, 'b' another, 'A', another, '0' another, and so on. The exact numbers can be found in an ASCII table, though we don't care about the actual values of any of them for this exercise. These mappings of character to number are not random, but laid out specially so that you can perform useful math on them. As you probably learned from the warmup, 'a' + 5 is 'f'. Likewise, 'A' + 5 is 'F', and '0' + 5 is '5'.

Given a number, print the corresponding lower-case letter of the alphabet. The number can be anything from 0 to 25. 0 prints out 'a', 1 prints out 'b', 2 prints out 'c', etc. It also returns the character. If it receives a number larger than 26 (e.g. 153), it prints: ERROR: print_letter: Received input 153, between 0 and 25 expected. Note: Think carefully about the proper control structure and calculations to use. This can be solved in very few lines if you embrace the numerical properties of characters.


print_letter(5) is 'f'
print_letter(25) is 'z'

Problem 2

Given a character, print the corresponding upper-case letter of the alphabet using asterisks. The letter can be anything from 'I' to 'L'. The set of letters are here.. You must make your design as if you are implementing all of the alphabet. However, because this course is about the beauty and joy of computing, not the drudgery of it, I am only asking you to complete 4 letters. If someone enters a letter outside of the range (e.g. 'A'), print out: ERROR: print_asterisk_letter: Received input A, between I and L expected. Note: Think carefully about the proper control structure to use. Your design needs to be efficient even if you were asked to implement all 26 letters.
void print_asterisk_letter(char letter);

Problem 3

Draw a sideways trapezoid. The width is the number of asterisks in the middle row. The number of asterisks in a row always increases by one each row until it reaches the specified width. Then, at the bottom, it also decreases by one each row until there are none. You must use a recursive solution.

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

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

Problem 4

Draw a trapezoid. The width is the number of asterisks in the bottom row. Each row has two fewer asterisks than the one below it. You must use a recursive solution.

void draw_trapezoid_rec(unsigned int width, unsigned int height);
draw_trapezoid_rec(5,2) results in:

 ***
*****

Problem 5

Draw a sideways trapezoid. The width is the number of asterisks in the middle row. The number of asterisks in a row always increases by one each row until it reaches the specified width. Then, at the bottom, it also decreases by one each row until there are none. You must use an iterative solution.

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

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

Problem 6

Draw a trapezoid. The width is the number of asterisks in the bottom row. Each row has two fewer asterisks than the one below it. You must use an iterative solution.

void draw_trapezoid_iter(unsigned int width, unsigned int height);
draw_trapezoid_iter(5,2) results in:

 ***
*****


Submit

At this point, you should have done the following:
  • Checked out your repository
  • Created a folder named hw2 in your repository and run svn add hw2
    $ svn add hw2
  • Completed the following four files: warmup2.h, warmup2.c, warmup2_main.c and Makefile inside your hw2 directory.
  • If you participated in Duet Programming, make sure you use scp to get the most up-to-date copies of any files your partner edited.
  • $ svn add warmup2.h warmup2.c test_warmup2.c Makefile 
  • Compiled your executable manually and with the Makefile
  • Executed your code to make sure it runs properly and inspected the results.
  • $ svn commit -m "hw2 warmup complete"
Now you're ready to move on to hw2!! Remember that the homework is completed individually.