For this assignment, you will write code to be used by a program. You
will not turn in a main()
, though you will be expected to write
one for testing. You will turn in only one file: numfacts.c
.
                
This module uses dynamic memory to store numbers and compute simple statistics.                 
Here is the numfacts.h
that you must use, and that we will use to
compile your program. Don’t turn in numfacts.h
.
                
#include <math.h> // for NAN #include <stdbool.h> // for bool, true, false // This will be called first: void initialize(); // This will be called last: void terminate(); // Read a file of doubles. Return how many doubles were read from // this file for success, a negative number for failure. // If called more than once, the values accumulate. int readfile(const char *filename); // Return the number of duplicated entries. // For example, if the numbers are 6,7,8,6,8,6, then // there are three sixes and two eights, so the dup_count() is 5. int dup_count(); // Return true if all values are at least this far apart. bool separation(double min_distance); // Returns values, in sorted order. // If n ≥ 0, return the corresponding value. // n==0 retrieves the lowest value. // If n < 0, count from the end. // n==-1 retrieves the highest value. // n==-2 retrieves the next highest value. // For an invalid n, return NAN double nth(int n);
You must implement all six of those functions in numfacts.c
.
                
NAN
is a special value, which stands for Not A Number.
It has the peculiar property that NAN is unequal to anything,
including NAN itself:
                
double d = NAN; if (d != d) printf("d=%f, which is not a number.\n", d);
d=nan, which is not a number.
Here is one way that we could test your code:                 
#include "numfacts.h" #include <stdio.h> int main() { initialize(); int n = readfile("test.data"); printf("There are %d entries: %d duplicates.\n", n, dup_count()); printf("There are %d values.\n", n); printf("Smallest: %f\n", nth(0)); printf("Largest: %f\n", nth(-1)); printf("Separated by five: %s\n", separation(5) ? "true" : "false"); terminate(); return 0; }
We will test your program with the numfacts.h
listed above, and
with various test programs of our choice. With a test program in
testomatic.c
, we would compile and link your code as follows:
                
c11 -Wall -c testomatic.c c11 -Wall -c numfacts.c c11 -Wall testomatic.o numfacts.o -o testomatic
If you encounter “STACK FRAME LINK OVERFLOW”, then try this:
export STACK_FRAME_LINK_OVERRIDE=ffff-ad921d60486366258809553a3db49a4a
main
in numfacts.c
.
main
in numfacts.c
.
readfile()
, dup_count()
,
separation()
, or nth()
functions may be called.
dup_count()
, separation()
, or nth()
before readfile()
is called. You simply have no data, so far.
main
. They must be declared static
.
main
in numfacts.c
.
static
.
main
in numfacts.c
.
Use web checkin, or:                 
~cs157/bin/checkin HW4 numfacts.c
User: Guest