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

Credit:
10 points.

Reading

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

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) one of two ways:

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 want you to do at least minimal checking that input from standard input is sensible. Doing a really great job of parsing and validating input is not especially easy, but for our purposes I say 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 has been presented in video lectures so far -- in particular I'd rather you not use arrays.) Note that the numbers do not have to be distinct -- for example, the user could enter three 0s.

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

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

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

Essay and pledge

Include with your assignment the following information.

For programming assignments, please put it a separate file. (I strongly prefer plain text, but if you insist you can put it in a PDF -- just no word-processor documents or Google Drive links please.) For written assignments, please put it in your main document.

Pledge

This should include the Honor Code pledge, or just the word “pledged”, plus at least one of the following about collaboration and help (as many as apply). Text in italics is explanatory or something for you to fill in; you don't need to repeat it!

Essay

This should be a brief essay (a sentence or two is fine, though you can write as much as you like) telling me what if anything you think you learned from the assignment, and what if anything you found interesting, difficult, or otherwise noteworthy.




2021-09-28