Changelog:

  • 26 Jan 2023: rephrase to allow invoking pprime as integers are read in
  • 26 Jan 2023: note that collaboration is exception to normal policy

1 Your task

  1. Write the following C files:

    main.c
    contains a main function. If there are command-line arguments, convert each to an integer and invoke pprime with each. If there are no command line arguments, read integers from standard input (provided in base-10, one number per line) until the end of standard input is reached. Invoke pprime with each of the integers you read (either immediately after reading each line or after reading all the integers). main should not print anything itself.
    primes.c

    contains (at least) three functions:

    isprime
    Accepts a single integer and returns 1 if it is prime, 0 otherwise. You are welcome to brute-force this or use the primality checking from COA1’s smallc program.
    nextprime
    See CSO1’s smallfunc.c for a description.
    pprime

    Given an integer, print a single line to standard out containing either

    (number) is prime

    or

    (number) is not prime, but (bigger number) is

    where (number) is the argument integer and (bigger number) is the result of invoking nextprime on the argument

    Do not include the parentheses in your actual output; correct formatting is

    3 is prime
    4 is not prime, but 5 is
    primes.h
    contains the header for pprime, isprime, and nextprime.
  2. Create a makefile named Makefile such that typing

    make

    will rebuild (if necessary) primes.o and main.o and link them together into an executable primes.

  3. Make sure your .c and .h file will not produce any warnings when compiled with the options -std=c11 -Wall -pedantic (assuming GCC or Clang) (though it is okay if your Makefile uses different options).

  4. Submit your files to the submission site (linked above).

2 Hints

Refer to introduction to makefiles for detailed examples of how Make works. You my also refer to our make’s online documentation including this example from that docuemntation.

You can find a reference on the C standard library and language here; in particular the string functions and math functions might be useful.

If your code uses any function in math.h (such as sqrt, pow, log, etc.), make sure you add -lm to the LDFLAGS of your Makefile or it will not compile on the department servers.

3 Collaboration

This assignment is intended to be exploratory and get your feet wet; as an exception to the normal policy, you are welcome to help one another with it as much as you wish. However, you are responsible for both (a) citing help and (b) understanding what you submit.