CSE 333 Exercise 4
Due: Friday, January 13 by 11 AM
Rating: 2 (note)
Each exercise this quarter is rated on a integer scale of 1 – 5, inclusive, with 1 being the "least time-consuming" and 5 being the "most time-consuming".
This difficulty scale is meant as a rough guide for you in predicting the amount of time to set aside for each exercise as you balance the work required for 333 with your other obligations. However, it is necessarily imperfect as everyone's set of circumstances and experiences with the exercises differ. If your experience with an exercise does not align with its rating, that is not a reflection of you or your abilities.
Goals
- Design and implement a small C module with a clear public interface.
- Use header files to separate interface from implementation.
- Practice compiling and linking multi-file C programs.
- Reason about information hiding and code organization in C for a clean interface for users.
Problem Description
Your job is to write a multi-file C program that does the following:
- Defines a function called
NthPrimethat accepts a singleint16_targument and returns anint64_t. The functionNthPrimeshould return the nth prime number. For example,NthPrime(1)returns2,NthPrime(2)returns3, andNthPrime(3)returns5. The behavior ofNthPrime(n)is undefined forn <= 0, and implementations do not need to check for this case. - Is modularized across three files:
NthPrime.h,NthPrime.c, andex4.c.NthPrime.hshould contain the function prototype forNthPrime, along with comments documenting how to use the function.NthPrime.cshould contain the implementation ofNthPrime. You may define helper functions here if you find them useful.ex4.cshould contain amainfunction that testsNthPrimeby printing the input and output of the function for at least two different, non-trivial arguments.
Implementation Notes
Interfaces and Modularity
This exercise is intended to give you practice designing and using a
small C module.
Think carefully about what belongs in the public interface
(NthPrime.h) versus the implementation
(NthPrime.c).
Client code should be able to use NthPrime without knowing
how it is implemented.
In particular, make sure that NthPrime.h contains only the
function declaration and documentation needed by users of the function,
and that all implementation details remain in
NthPrime.c.
Testing
Your ex4.c file should act as client code that uses the
NthPrime module.
It is sufficient to test the function with a small number of
representative, non-trivial inputs.
When a test fails, your program should print an informative error
message and return a non-zero exit status.
The main function in ex4.c is considered
testing code.
Testing code is the only place where the use of assert()
is allowed, though you are not required to use it.
Style Focus
General
Make sure that your function names and signatures match the specification exactly, including capitalization. Write clear comments explaining the purpose and behavior of each function you define. Be sure that your comments document any assumptions or constraints on function behavior.
Header files
Header files define the public interface to a module. Include only function declarations and documentation in header files, and avoid exposing implementation details. Client code should be able to use a module correctly by reading the header file alone.
Multiple files
This exercise is intentionally split across multiple files.
Place interface declarations in header files and implementations
in the corresponding .c files.
Use header guards where appropriate, and keep module boundaries
clear and well organized.
Submission
Submit the following file(s) by creating an ex4-submit tag in your exercise repo before the assignment deadline. The file(s) should be located in the exact directory listed below, including capitalization:
ex4/ex4.cex4/NthPrime.hex4/NthPrime.c
Your code must:
- Compile without errors or warnings on CSE Linux machines
(lab workstations,
attu, or CSE home VM). - Have no runtime errors, memory leaks, or memory errors
(
gccandvalgrind). - Be contained in the files listed above that compile into an
executable with the following commands:
$ gcc -Wall -g -std=c17 -c -o NthPrime.o NthPrime.c $ gcc -Wall -g -std=c17 -c -o ex4.o ex4.c $ gcc -Wall -g -std=c17 -o ex4 ex4.o NthPrime.o
- Have a comment at the top of each
.cfile with your name(s) and CSE or UW email address(es). - Be pretty: the formatting, variable and function names, commenting, and so on should be consistent with class style guidelines.
- Be robust: your code should deal with hard-to-handle/edge cases and bogus user input (if there are any) gracefully.