CS202 - Assignment Three

Due: 2018-10-03 9:00a

Math solutions

rot13 solution

counting solution

Objectives

  • Get more practice with number systems and conversions
  • Play around with strings some more
  • Continue to build your C programming muscles

[15 points] Some more math

A. [5 points] Martin 17

B. [5 points] Martin 18

C. [5 points] Martin 20

[15 points] Rot13

Rot13 is an extremely simple encryption method. It is used for low stakes obfuscation, like making it harder to read comments with spoilers in them. Follow the link to the Wikipedia article for a more in depth discussion, but the basic principle is that a character is encoded by shifting it 13 places. If the new location is past 'z', the character wraps around and starts at the beginning of the alphabet again. A nice feature of the scheme is that it is reversible simply by performing the same encryption process again (e.g., 'a' -> 'n' -> 'a'). Rot13 also leaves capitalization intact and doesn't touch spaces, numbers or special characters.

Write a function void rot13(int numlines, char * text[]) that performs in-place rot13 encoding on an array of strings. For the actual computations and comparisons, recall that you can make use of the fact that in C, char is a numeric type. In other words, 'a' and the value 97 (the ASCII value of 'a') are interchangeable. So, given some char variable c, you could compare it to 'a' (c > 'a'), or do math with it (c + 5).

To test this function, I would like you to pass the remainder of the arguments to your program after the name of your program. Use pointer arithmetic to pass just part of argv. After the strings have been encrypted, print them out all on the same line and separated by spaces.

Put this in a file called hw3_rot13.c

[15 points] Counting in binary

Write a program in C that counts in decimal and in binary from 0 up to an input number. You should have enough tools at this point to just read the bits off of an integer, but I want you to handle this problem a little less directly. Create a new string capable of holding all of the bits of the target number. Then, for each number you should update this string to be the binary representation of the number.

To calculate the binary representation, you are welcome to either perform the count mechanically (flipping between 1s and 0s, propagating the flip when a 1 becomes a 0), or computationally (using repeated division on an integer counter). In either case, your output should look like this:

                               
$ ./hw3_binary                                                
Usage: ./hw3_binary <target number>                                             
$ ./hw3_binary tardis                                         
tardis is not a valid integer                                                   
$ ./hw3_binary 7                                              
0  000                                                                          
1  001                                                                          
2  010                                                                          
3  011                                                                          
4  100                                                                          
5  101                                                                          
6  110                                                                          
7  111                                                                          
$ ./hw3_binary 24                                             
 0  00000                                                                       
 1  00001                                                                       
 2  00010                                                                       
 3  00011                                                                       
 4  00100                                                                       
 5  00101                                                                       
 6  00110                                                                       
 7  00111                                                                       
 8  01000                                                                       
 9  01001                                                                       
10  01010                                                                       
11  01011                                                                       
12  01100                                                                       
13  01101                                                                       
14  01110                                                                       
15  01111                                                                       
16  10000                                                                       
17  10001                                                                       
18  10010                                                                       
19  10011                                                                       
20  10100                                                                       
21  10101                                                                       
22  10110                                                                       
23  10111                                                                       
24  11000          

Note that the values are all right aligned, and use only enough room for the largest values, and that the binary numbers use only enough bits for the largest value as well.

Put this in a file called hw3_binary.c


[5 points] Follow the submission directions

  • If the first section is turned on paper, it must be legible and have your name and section on the top
  • If the first section is turned in online, it must be a PDF or RTF file (no .doc or .docx files)
  • Each file must be named according to the instructions above.
  • Make sure that the assignment runs on Linux before you submit it. Do not assume that it will -- check it.
  • Each function should be commented to describe its function
  • At the very top of each file, you will place a block comment that follows this format exactly:
/*
Assignment 3
<name of the program as appropriate>

Name:
Email: 
Date:
Collaborators:

To compile:
To run:
*/

The name, date, and email fields should be self explanatory. In the collaborators field, you should list everyone who helped you with this assignment including classmates, tutors, and myself. In the to compile and to run fields, provide the commands required to compile and execute your code (i.e., someone should be able to copy these commands directly onto a command line and run your code). The to run should include usage instructions (what arguments need to be provided and what they do).

Additional instructions

We had questions last time if it was necessary to "harden" your code, or if it was acceptable for the code to crash when given invalid inputs. From here on in, assume that it is never acceptable for a program to crash. This usually is not that hard -- you just need to validate the inputs.

Turning in your work

Submit the completed assignment on Canvas.

Last Updated: 10/8/2018, 4:28:07 PM