Appfs Exercise 1: Number storage and sorting subroutines.
More...
Go to the source code of this file.
- Author
- Thorsten Koch
- Date
- 24Oct2014
Definition in file nums.h.
Definition at line 9 of file nums.h.
void nums_free |
( |
NUMS * |
nsp | ) |
|
Definition at line 141 of file nums.c.
145 if (NULL != nsp->
num)
static int is_valid(const NUMS *nsp)
Checks if data structure is ok.
int * num
storage for numbers
void nums_print |
( |
const NUMS * |
nsp | ) |
|
Definition at line 166 of file nums.c.
174 for(
int i = 0; i < nsp->
used; i++)
176 if (nsp->
num[i] != prev)
180 printf(
"%d\n", prev);
static int is_valid(const NUMS *nsp)
Checks if data structure is ok.
static int cmp_num(const void *a, const void *b)
Compare two ints.
int * num
storage for numbers
size_t used
next free num[]
NUMS* nums_read |
( |
const char * |
filename | ) |
|
Problem: How to return number of items
Alternatives: int* read_nums(const char* filename, size_t* nums_used) int read_nums(const char* filename, int** num)
Functions should have one exit initalize at declaration goto is bad
Definition at line 52 of file nums.c.
54 assert(NULL != filename);
55 assert(0 < strlen(filename));
64 if (NULL == (nsp = calloc(1,
sizeof(*nsp))))
72 if (strcmp(filename,
"-"))
74 if (NULL == (fp = fopen(filename,
"r")))
86 while(fread(&n,
sizeof(n), 1, fp))
98 if (NULL == (nsp->
num = realloc(nsp->
num, new_size *
sizeof(nsp->
num[0]))))
103 nsp->
size = new_size;
116 if (strcmp(filename,
"-"))
131 if (nsp->
num != NULL)
Data structure to store numbers.
static int is_valid(const NUMS *nsp)
Checks if data structure is ok.
int * num
storage for numbers
size_t used
next free num[]
size_t size
max number of entries off num
size_t nums_used |
( |
const NUMS * |
nsp | ) |
|
Definition at line 187 of file nums.c.
static int is_valid(const NUMS *nsp)
Checks if data structure is ok.
size_t used
next free num[]