CSCI 1120 (Low-Level Computing), Spring 2015:
Homework 2

Credit:
10 points.

Reading

Be sure you have read the assigned readings for classes through 1/28.

Programming Problems

Do the following programming problems. You will end up with at least one code file per problem. Submit your program source (and any other needed files) by sending mail to bmassing@cs.trinity.edu, with each file as an attachment. Please use a subject line that mentions the course number and the assignment (e.g., ``csci 1120 homework 2''). You can develop your programs on any system that provides the needed functionality, but I will test them on one of the department's Linux machines, so you should probably make sure they work in that environment before turning them in.

  1. (5 points) Write a C program that asks the user for three integers and prints them in order from smallest to largest, or an error message if what was entered is something other than three integers. (You may recognize this problem as a special case of one you probably spent some time on in CS1. Don't try to solve that problem in general for this assignment; just solve the ``three numbers'' problem using what we've discussed in class so far.) Clarification: Notice that the numbers do not have to be distinct -- for example, the user could enter three values of 0.

  2. (5 points) Write a C program that asks the user for two non-negative integers, call them $ a$ and $ b$ , not both zero, and computes and prints $ \gcd(a,b)$ , the greatest common divisor of $ a$ and $ b$ , using a recursive version of Euclid's algorithm. Print an error message if what was entered is not two integers, or either input is negative, or both are zero.

    Euclid's algorithm can be described recursively thus: For non-negative integers $ a$ and $ b$ , not both zero, with $ a \ge b$ ,

    $\displaystyle \gcd(a, b) = \begin{cases}a & \text{if } b = 0 \\ \gcd(b, a\bmod b) & \text{otherwise} \end{cases}$    

    where $ a\bmod b$ is the remainder when $ a$ is divided by $ b$ .



Berna Massingill
2015-02-04