CMSC 15200 - Summer 2016

Homework #2: Due: Wednesday August 3rd, 2016 @ 11:59pm

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. Again - do not commit a.out to your repository.

A portion of your grade will count toward reusing functions you write in this homework assignment.

Make sure you understand the the circleArea.c and power.c files on the lecture page.

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, given the two parts of a fraction (i.e., two positive integers: numerator and denominator), reduces the fraction to lowest terms and prints out the reduced form. For example, if you call reduce(10,4) it should print 5/2 to the console. If the numerator or denominator are negative or the denominator is zero, the function should print "Invalid argument value".

Problem #1-C: Max,Min,Average Fraction

Write a function get_stats that finds the largest, smallest, and average in a series of fractions entered by the user. The function must prompt the user to enter in a fraction. After accepting and processing a fraction, the user will be asked if he wants to enter another fraction. If the user answers "Y", then the user is asked for another fraction. If the user answers "N", the program will proceed to write the maximum, minimum, and average of all the fractions. The minimum and maximum fractions must be in simplest form, and the average should be in decimal form. For example:
Enter number #1: 4/8

Do you want to enter another fraction? Y

Enter number #2: 4/6

Do you want to enter another fraction? Y

Enter number #3: 11/33

Do you want to enter another fraction? N

Min: 1/3
Max: 2/3
The average is 0.5              
                  

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 again whether the user would like to enter another fraction.


Problem #2: Get the Digits of a Number

Write a function get_digit that returns the i-th digit in a number. The function takes in two unsigned integers, the first parameter being the number and the second is the position of the digit the function will return. If the function cannot find the i-th digit then it should return -1. If the position is zero then the function should also return -1. For example:
printf("%d\n", get_digit(421, 0)); // -1
printf("%d\n", get_digit(421, 1)); // 1
printf("%d\n", get_digit(421, 2)); // 2
printf("%d\n", get_digit(421, 3)); // 4
printf("%d\n", get_digit(421, 4)); // -1
                  

Problem #3: Sum of the Digits

Write a function sum_of_digits that produces the sum of all the digits in an unsigned integer and returns an unsigned integer representing that sum.
printf("%d\n", sum_of_digits(421)); // 7
printf("%d\n", sum_of_digits(324)); // 9        
                  

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 approach 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 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 the lecturer. We'll get you set up with a repository in time to make the deadline.