CSc 352, Fall 2002: Assignment 2: Simple C Programming

Out: Thu Sept 12

Due: 12:00 Noon Thu Sept 19


You are to create a number of files, as directed below, and then submit them electronically on host lectura.cs.arizona.edu using the command

turnin cs352_hw2 files

Please follow the directions below carefully: submissions that don't follow directions will be penalized heavily.

Note:

  1. Each of the programs below should exit with a status of 0 if execution proceeds normally, and a status of -1 if any sort of error occurs.

    Under csh and tcsh you can check the exit status of a command or program cmd by typing the command echo $status immediately after the execution of cmd. A program can exit with status n by executing the system call exit(n), or -- in the function main() only -- executing the statement return n.

  2. Your programs should compile with no errors or warnings when compiled with gcc -Wall.


  1. Write a program, in a file triangle.c, that behaves as follows:

    1. Reads in three integer values, given one per line, from stdin. These represent the lengths of three straight line segments.

    2. If it is possible to form a right-angled triangle using these three lines, the program prints is a right triangle. Otherwise, if it is possible to form a triangle using these lines, the program prints is a triangle. Otherwise, the program prints is not a triangle.

    Recall that for a right-angled triangle, the square on the hypotenuse equals the sum of the squares on the other two sides; and that for triangles in general, the sum of the lengths of each pair of sides is greater than the length of the third side.

    You should make sure you check that the values entered make sense (i.e., are non-negative): if they don't, your program should print an appropriate error message and exit with status -1.

  2. A (positive integer) number is said to be perfect if it is the sum of its positive divisors. (see this page for more information). For example, the first three perfect numbers are

    Write a program, in a file perfect.c, that reads in integer values, one per line, from stdin, until an end-of-file is encountered; and for each value N so read, prints out `N is perfect' if N is a perfect number, and `N is not perfect' if it is not. You may assume that the input contains only positive numbers (i.e., not anything like abc or -2).

    For example, given the input sequence

    5
    6
    7
    the program should print out
    5 is not perfect
    6 is perfect
    7 is not perfect
    For each number read in, the output indicating whether or not it is perfect should be printed before reading in the next number.

  3. Goldbach's Conjecture states that any even number greater than 2 can be written as the sum of two primes. This statement has been verified for all values upto fairly large bounds, but has not yet been formally proved. The purpose of this assignment is to verify Goldbach's Conjecture for all even numbers upto a given input number.

    Write a C program, in a file goldbach.c, that reads in a single number N from stdin and verifies Goldbach's Conjecture for all even numbers upto (and including) N. For each even number k between 4 and N (inclusive), your program should print out three numbers using the statement

    printf( "%d %d %d\n", k, a, b ); /* numbers separated by a single space */
    where a and b are prime numbers such that a+b = k.

    To simplify grading, we will assume the following restrictions on the format of your output:

    1. When printing the two prime numbers a and b that add up to an even number, always make sure that the smaller prime is printed first. E.g., print
      12 5 7
      and not
      12 7 5.
    2. There are some numbers that can be expressed as the sum of two primes in more than one way, e.g.:
      24 = 5 + 19 = 7 + 17.
      In this case, print only the pair of primes where the first (smaller) value is the smallest. Thus, in the example above, since 5 < 7, you should print
      24 5 19
      and not
      24 7 17.
    If N >= 4, your program should exit with status 0; otherwise it should print out nothing but exit with status -1.