CSCI 1120 (Low-Level Computing), Fall 2017:
Homework 3

10 points.


Be sure you have read, or at least skimmed, the assigned readings for classes through 9/06.

Honor Code Statement

Please include with each part of the assignment the Honor Code pledge or just the word ``pledged'', plus one or more of the following about collaboration and help (as many as apply).1Text in italics is explanatory or something for you to fill in. For written assignments, it should go right after your name and the assignment number; for programming assignments, it should go in comments at the start of your program(s).

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 with each file as an attachment. Please use a subject line that mentions the course and the assignment (e.g., ``csci 1120 hw 3'' or ``LL hw 3''). 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.

Note about error checking: Starting with this assignment, I do want you to do at least minimal checking that input from standard input is sensible. As mentioned in class, doing a really great job of parsing and validating input is not especially easy, but for our purposes it's enough to check that scanf was able to get value(s) of the requested type(s) and that the values meet whatever other criteria the problem states (e.g., not negative for the second problem).

  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. (Note that you will not get full credit unless you use recursion.)

    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$ .


... apply).1
Credit where credit is due: I based the wording of this list on a posting to a SIGCSE mailing list. SIGCSE is the ACM's Special Interest Group on CS Education.

Berna Massingill