CS157: Intro to C, Part II

Spring 2018

HW 4

CS157 HW4: The last CS157 assignment!                


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.                 

Provided code                

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.

Test Program                

Here is one way that we could test your code:                 

    #include "numfacts.h"
    #include <stdio.h>

    int main() {
        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");
        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


How to submit your homework:                

Use web checkin, or:                 

    ~cs157/bin/checkin HW4 numfacts.c

How to receive negative points:                

Turn in someone else’s work.

User: Guest                 

Edit History Source

Modified: 2018-05-01T06:53                 

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building