INTERNATIONAL:Eval:C
Sommaire
Introduction
The purpose of this micro-project is to evaluate basic knowledge in C. You must respect the presentation and organization rules (directory and file names, format of the output … )
Requirements
All the required files must be in the a single directory named after your login of the form: login-eval20150127. The content of the directory must follow this layout:
- login-eval20150127
- Makefile
- main.c
- readfile.c
- readfile.h
- sort.c
- sort.h
The directory must contains theses files and only these files.
All the code must compile correctly (see next section) without warnings and must obey to the C99 standard.
In order to provided your files, you'll build a compressed archive using tar and the bzip2 compression algorithm (option j in tar.) The archive name must have the form: login-eval20150127.tar.bz2.
Evaluation
The main evaluation will be automatic, your archive and your code must strictly obey to the form described in this document, otherwise it won't be evaluated.
In order to evaluated, your program must successfully compile and run without failure.
Building your Code: Makefile
Your code will be build using the command make that take its form from the file Makefile. This file must provide the following rules:
- all: build the main program called main
- main: same as all
- clean: remove all files produced by the compilation process (including main)
You must use the compiler named gcc with the following options (for C files) : -Wall -Wextra -std=c99 -O2.
Here is a template Makefile to help you start:
# Makefile CC=gcc CFLAGS= -Wall -Wextra -std=c99 -O2 LDFLAGS= LDLIBS= all: main # FIX The following rules main: clean: # END
The main goal
This program is pretty simple, it reads a file containing integer (in ASCII) stores them in an array, sorts them and print them on the standard output.
The following sections describe each sub-parts, files by files.
Reading the files
- file: readfile.c
You must provide a function that take a file name and a pointer to an array (thus a pointer on a pointer), reads the file, allocates correctly an array to store the integers from the file and returns the number of integers stored in the array.
The file has the simplest possible format: one integer (in ASCII) per line.
readfile.c must provide at least the following function:
size_t read_file(char *name, int **tab);
Your code should follow this schema:
- open the file name, quit the program with an error message if opening fails
- allocate an array
- read the file line by line, and for each line:
- get the integer
- store the integer in the array, extending it if needed
- store the array address in *tab
- returns the number of integers stored in the array
Of course, the file readfile.h will exposed read_file.
Sorting the array
- file: sort.c
This file must provide a sort function for an array of integers. The function has the following prototype:
void sort_int(int tab[], size_t len);
- tab is the array
- len is the len of the array (number of elements)
The array must be sorted in increasing order w.r.t. the natural integer order.
This part must not use any library functions, including the function qsort. A validation procedure will be run on the code to detect any usage of these functions.
You can implement any sorting algorithm, but your code will be test against big set of data (100MB or more) and must run in reasonable time.
Of course, the file sort.h will exposed sort_int.
Main program
- file: main.c
This the main program, it will:
- Check that at least one argument has been provided
- if no arguments are provided, the program quit with an error message
- Call read_file with the first argument
- Call sort_int with the array built with read_file
- Display the sorted array on standard output, one integer per line
- Free the memory allocated to the array
- Exit
Some general remarks about the program:
- When leaving with an error, the status of the program must be greater than 0 and less than 127.
- All error messages must be printed on the standard error output stream (stderr)
- The only permitted output is the final result (content of the sorted array.)
- Any memory allocation must be freed before leaving