You are expected to complete this assignment individually. If you need help, you are invited to come to office hours and/or ask questions on piazza. Clarification questions about the assignments may be asked publicly. Once you have specific bugs related to your code, make the posts private.
This homework has several exercises. We are also providing you with some resources on printf and error handling for this assignment.
You should submit five files for this assignment (hw2.h, hw2.c, hw2_main.c, and Makefile) in your subversion repository as directed below.
If you participated in duet programming, make sure you copy over your files from your duet svn account to this account!!
Because you are adding a second set of files to your directory, you need to add a second target to your Makefile. In your makefile, add another two lines (with a space between these and the ones already there).
hw2: hw2.h hw2.c hw2_main.c clang -Wall -o hw2 hw2.c hw2_main.c
Now you need to make the skeleton code so that your program will minimally execute.
double surface_area_cylinder(double height, double radius) { return 0.0; }
First get this compiling and running. It won't print out anything, but this will mean that your code will compile and execute with our infrastructure. This must work in order to get any points in this course. Do this first, not last.
Note: The array of exercises are chosen for several reasons. First, they build up in difficulty as they move forward. Second, they exercise all of the different skills you have learned so far. As simple as these functions seem, they have multiple ways they could be solved. You will be graded on choosing a solution that is efficient.
How do you decide, you ask? If you can solve it by calculation rather than by conditionals, then do so. Also, different conditionals have different efficiencies. A disconnected set of if's is slower than a coordinated set of if/else if/else if's, which is slower than a single switch statement. However, not all code can be implemented with the faster control structure. Therefore, it is your job to think about whether what you are writing could be structured in such a way as to use a switch or if/else if/else if or if. We cover proper usage, not just correctness of control structures in class, so make sure to pay attention. If we haven't covered it in lecture, then I do not expect you to apply it in your assignments.
Context: Communication over radio is notoriously noisy, making it hard to hear the difference between letters such as 'D', 'P', and 'B' when read out. In addition, pilots need to read out their airplane numbers and desination airport initials routinely. Therefore, they have created a pilot's alphabet that assigns a word to each letter (each word begins with that letter). The words are specifically chosen to not sound like each other. (dab and tab, for example, would be poor choices).
First write a function that takes a single char and prints out the word associated with that char. For letters 'a' to 'z', 'A' to 'Z' , use the pilot's alphabet. For '0' to '9', print out the written version of the number (e.g. "seven").
In addition, this function returns an integer which is the number of letters in the word it printed out.
If the user enters any char out of those ranges, print out an error "error (print_pilot_word): [description of error]". Remember, as described in hw1, to use fprintf rather than printf and send the output to stderr. Return -1 to indicate that an improper input was received.
Note: You may not use an array for this exercise. This is an exercise in choosing proper control structures, not choosing proper data structures. That will come later!The function header must be as follows:
Don't forget to complete this portion, test it, and commit it before moving on!int print_pilot_word(char letter);
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'.
Someone has written a function that encodes a word into a single integer. It works in the following way:
Your job is to write a function that prints out the word from its integer encoding.
(Hint: A good thing to do right now before going any further is to try a few small words, figure out their encodings, and then try to extract the word back again from the encoding.)
In the warm-up, you already wrote a function that prints out a single letter from its number.
char print_letter(unsigned int number);
Now write a function that prints out an entire word, one letter at a time:
void print_word(unsigned int number);
If the user enters a number greater than 26 for print_letter, print out an error "error (function_name): [description of error]".
Note: You may not use arrays or loops for this exercise. You must use recusion. Remember that recursion is a problem-solving technique, not just a solution. This is the key to this problem. If you can't figure it out, please come into to office hours! I'm happy to help you!You must clearly label your base case, smaller case, and general case in comments
Don't forget to complete this portion, test it, and commit it before moving on! If you get stuck, you may move on. Otherwise, make sure you go through all steps.* * ** * * **** ** ** *** ** * * * * * * ** * * * * * * * * * * **** ** * **** * ** * * * * * * * * ** * * * * * * * * ** ** * * **** ** * * ** **There is a single space between each letter (or column of spaces, to be more accurate).
void print_asterisk_word(char word[], unsigned int length);
You already wrote a print_asterisk_letter function in the warmup. You are now using an array, which you saw in class on Friday. I have provided skeleton code for you to test it. Think about whether or not you can use that function for this part. At its heart, this an exercise in function decomposition, not coding. The way you break down this exercise is going to greatly influence the readability of your solution. Think about how the computer is going to print it out, then think about giving that information in such a way that divides nicely into functions. You should have helper functions, but not necessarily the ones you think.
There are many ways to encode messages so they are slightly harder to read, including mappings from one letter to another letter or rotations from one letter to some other letter (A=C, B=D, C=E, ... Z=B for rot_factor 2).
Write a function encode_rotate that rotates each letter by a linearly increasing amount. You are now modifying an array, which you saw in class on Friday. You are only modifying the contents of the array - I have given you the syntax for doing so. I have also provided skeleton code for you to test it.
void encode_rotate(char msg[], int length, int rot_factor);
For every letter in the message that is a letter from 'a' to 'z' and 'A' to 'Z', rotate it by rot_factor*n letters, where n is the position in the message if we start numbering with 1. Do not modify punctuation or any other character that is in the message. For example, the word "apple" with rot_factor 1 will become "brspj", whereas with 2, it will become "ctvto".
You may modify the input array. For primitive types such as int, float, char, double, the input parameter is a copy of what was passed in. For arrays, because the system doesn't know their length, the array itself is not copied - only the location of the beginning of the array is copied. This means that if you modify the array within the function, the change is reflected in the calling code.
Don't forget to complete this portion, test it, and commit it before moving on!$ svn add hw2.h hw2.c hw2_main.c
$ svn commit -m "hw2 complete"