CMSC 15200 - Summer 2016

Homework #3: Due: Friday August 5th, 2016 @ 11:59pm

This homework will give you more practice with variables, expressions, selection statements, loops, and functions. You should submit two files: hw3/luhn.c and hw3/drunkard.c in your subversion repository. Do not commit a.out to your repository; we will recompile your code on our own machines on our end (in fact, we must).

Problem #1: Luhn's Formula

The Luhn formula is used to verify account numbers, such as some credit card numbers. The last digit in a Luhn-style account number is a check digit whose value is determined by the previous digits. By way of explanation, say our account number is 18929. The check digit is 9; we would like to verify that it is correct. Double every alternate digit, working right-to-left from the next-to-last digit (in this case, 2). Compute the sum of the digits in the resulting numbers. For example 18929, this sum is 4 + 9 + 7 + 1 = 21. (4 is the sum of the digits in 4 = 2 * 2; 7 is the sum of the digits in 16 = 2 * 8.) The check digit is given by 10-u where u is the low digit of the sum. In this case u is 1, so the check digit is 10 - 1 = 9, so the account number is valid per the Luhn formula.

Implement a function luhn to consume an unsigned int and return a boolean int indicating the validity of the given account number.

Hint: Look at your homework #2 and see if you could reuse some of the functions you implemented.

Problem #2: Drunkard's walk.

A drunkard begins walking aimlessly, starting at a lamp post. At each time step, the drunkard can't remember where he or she currently is, and takes a random step, either north, east, south, or west, with probability 25%. You will write a program that will calculate how far the drunkard will be from the lamp post after N steps.

You will write this program in the a file called drunkard.c. The program will prompt the user to enter an integer N and simulate the motion of a random walker for N steps. After each step, print the location of the random walker, treating the lamp post as the origin (0, 0). Also, print the square of the final distance from the origin (i.e., the distance formula without the square root).

Random numbers can be generated with the rand() function. See random numbers. Make sure you initialize the random seed at the beginning of the main function as stated on the website . You will need to think of the best way to implement this program. Make sure to think about the functions, variables, etc., before you begin implementing the program.

Simulation of 10 steps:
Enter in the number of simulation steps: 10
Start Position: (0,0)
Going South: (0,-1)
Going West: (-1,-1)
Going East: (0,-1)
Going North: (0,0)
Going West: (-1,0)
Going North: (-1,1)
Going North: (-1,2)
Going West: (-2,2)
Going North: (-2,3)
squared distance = 13.000000                
  
Simulation of 30 steps:
Enter in the number of simulation steps: 30
Start Position: (0,0)
Going East: (1,0)
Going South: (1,-1)
Going North: (1,0)
Going East: (2,0)
Going West: (1,0)
Going South: (1,-1)
Going East: (2,-1)
Going West: (1,-1)
Going West: (0,-1)
Going West: (-1,-1)
Going West: (-2,-1)
Going South: (-2,-2)
Going East: (-1,-2)
Going South: (-1,-3)
Going East: (0,-3)
Going East: (1,-3)
Going West: (0,-3)
Going North: (0,-2)
Going West: (-1,-2)
Going West: (-2,-2)
Going South: (-2,-3)
Going South: (-2,-4)
Going South: (-2,-5)
Going East: (-1,-5)
Going West: (-2,-5)
Going South: (-2,-6)
Going South: (-2,-7)
Going South: (-2,-8)
Going South: (-2,-9)
squared distance = 85.000000           
  

Note: For those of you who are interested, this process is a discrete version of Brownian motion.


Your File

In order to compile your files, you will need to write a main function. We will replace it with our own main function when we grade it, so it doesn't matter what you write. However, the most sensible use is to use it to run a variety of tests, with expected results, on the functions you've written to solve the problems above.

Style

At the top of your C file, write a comment with your name, etc., in the following form:

              /* Jane Doe, jdoe */
              /* CS152, Summer 2016 */
              /* Homework 3 */
              
This information is not strictly necessary, since your files are already identified by their names and the repository they reside in. Nevertheless, the redundancy is a helpful convenience for us when we are browsing and/or grading your work.

Comments, where they occur, should be helpful and precise. Omit superfluous comments:

              int a = b + c; /* I'm adding b and c and naming it a! */
              
Yes, we can see that.

Your code should be no more than 80 columns wide.

Do not write more than one statement on a line.

Do not submit code that does not compile. If a function is broken, and makes compilation impossible, comment out that function and submit the rest. Non-compiling code will receive little to no credit.


Submitting Your Work

Save and commit your code in YOUR-REPOSITORY/hw3/luhn.c and /hw3/drunkard.c. Recall that you will need to add your work before you commit it. (Also, notice that in the -m message you include at commit time, -m is simply a command-line option.)

Commit your work early and often. We do not grade intermediate commits, only the work as it stands at the deadline. If you have any issues with subversion, not only your instructors but your classmates can help you. Most of the students in this class have at least one full quarter of experience running subversion.

If, for any reason, perhaps due to a late add, you do not have a CS repository, save your work somewhere you can easily get it and send mail to the lecturer. We'll get you set up with a repository in time to make the deadline.