# CS157 HW4: The last CS157 assignment!

## Description

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.

// 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

`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() {
initialize();
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;
}
```

## Testing

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
```

## Debugging

If you encounter “STACK FRAME LINK OVERFLOW”, then try this:

```    export STACK_FRAME_LINK_OVERRIDE=ffff-ad921d60486366258809553a3db49a4a
```

## Requirements

• Do not put `main` in `numfacts.c`.
• There is no limit to the size of the data file, or to the number of data files. None!
• The data file is in no particular order.
• The data file may contain any number of doubles per line, or none.
• It might not have any data at all, and that’s ok.
• Do not put `main` in `numfacts.c`.
• There is no limit to how many times the `readfile()`, `dup_count()`, `separation()`, or `nth()` functions may be called.
• It’s ok to call `dup_count()`, `separation()`, or `nth()` before `readfile()` is called. You simply have no data, so far.
• Put comments at the very very beginning of the file with your name, date and purpose of the program.
• You may define whatever additional functions that you wish, except for `main`. They must be declared `static`.
• Do not put `main` in `numfacts.c`.
• Close any file that you open.
• Free any memory that you allocate.
• You may use global variables, only if they’re `static`.
• Do not put `main` in `numfacts.c`.

## 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

Check: HTML CSS
Edit History Source

Modified: 2018-05-01T06:53

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