Goals for this Warmup

  • Practice programming with control, iteration, and recursion.

For this lab, you are welcome to get technical help from another student on how to use or install any of the tools involved. You may also get syntax help on C. On the warmup only, you may partner with one student (and document that collaboration on your warmup.c file) with whom you can discuss not only syntax but also the algorithmic portion. You may not, however, get help on the algorithmic portion of the exercise outside of your collaborative partner, office hours or piazza questions to TAs or instructors. The homework itself has no partner.

This lab is broken down into several steps:

Set up

You should have already created a hw2 directory and completed the pre-lab (having created, added, and submitted warmup2.h warmup2.c warmup_main.c and Makefile).

You are being asked to write four functions. The functions were also described in the prelab.

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, do two things. 1) print the corresponding capital letter of the alphabet. 2) return the character. The number can be anything from 0 to 25. 0 prints out and returns 'A', 1 -> 'B', 2 -> 'C', etc.

If it receives a number out of range (e.g. 153), it prints: ERROR: print_letter: Received input 153, between 0 and 25 expected. In this case, it returns ' ' - the space character.

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 'R', 'S', 'T', or 'U'. The set of letters are here.

Note: Think carefully about the proper control structure to use. 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 R and U expected. It does not print out anything.
void print_asterisk_letter(char letter);

Problem 3

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 (based on the number of rows printed, not any target width). Then, at the bottom, it also decreases by one each row until there is the width. You must use a recursive design process. You may write more than one function if you wish - whatever design emerges from the recursive design process. There may be absolutely no loops.

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

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

Problem 4

Draw a sideways trapezoid iteratively. 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 an iterative solution, and it must contain no function calls.

void draw_sideways_wedge_iter(unsigned int width, unsigned int height);
draw_sideways_wedge_iter(3,10) 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.
  • $ svn add warmup2.h warmup2.c warmup2_main.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.