CMSC 15200 - Summer 2013

Homework #2 (Long): Due: Wednesday August 7th, 2013 @ 1:30pm

This homework will give you practice with variables, expressions, selection statements, loops, and functions. You should submit one file hw2/hw2.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-A: GCD

Write a function gcd that, given two integers N and M, finds their greatest common divisor.

Using a naïve algorithm (trying all possible divisors for each of the two numbers, from 1 to N-1, and from 1 to M-1, and keeping track of what the largest common divisor is) will only earn you partial credit. For full credit, you will need to use an algorithm that doesn't require working throught every single divisor.

Hint: Don't try to come up with the algorithm yourself. There is a well-known classic algorithm to find the greatest common divisor of two numbers (this algorithm, in fact, is much easier to code than the naïve algorithm). Find out what that algorithm is and don't forget to cite your sources.

Problem #1-B: Reduce

Write a function reduce that asks the user to enter in a fraction, then reduces the fraction to lowest terms and prints out the reduced form.

Enter in a fraction: 10/4
Reduced form: 5/2
Hint : Use the gcd function you wrote in the earlier problem..


Problem #2: Max,Min,Average Number

Write a function getStats that finds the largest, smallest, and average in a series of numbers entered by the user. The function must prompt the user to enter in a number. After accepting and processing a number, the user will be asked if he wants to enter another number. If the user answers “Yes”, then the user is asked for another number. If the user answers “No”, the program will proceed to write the maximum, minimum, and average of all the numbers.
Enter number #1: 42

Do you want to enter another number? Y

Enter number #2: 23

Do you want to enter another number? Y

Enter number #3: 37

Do you want to enter another number? N

The smallest number is 23, and the largest number is 42

The average is 34              
                  

For full credit, when asking if the user wants to enter another number, check that the character is 'Y', 'y', 'N', or 'n' (without using an if statement). If the user introduces any other character, show an error message and ask him again whether he would like to enter another number.


Problem #3: Sum of the Digits

Write a function sumOfDigits that produces the sum of all the digits in an unisgned integer and returns an unsigned integer representing that sum.
Enter in a number: 421

Sum of the Digits: 7            
                  

Problem #4: 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 quasi-boolean int indicating the validity of the given account number.

Hint: Use the sumOfDigits function from above and it might be beneficial to use some parts of the sumOfDigits code in this function as well.
Hint: Look at the code we produced in class for getting the number digits in an integer.

Your File

In order to compile your file, 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 four 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 2013 */
              /* Homework 2 */
              
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/hw2/hw2.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 Adam. We'll get you set up with a repository in time to make the deadline.