ArrayLists (team assignment)

Overview

Python, Java, and many other languages have the capability for an array that automatically expands itself as necessary. In Python it is called a "list"; in Java, it is called an "ArrayList". (I'll refer to this idea throughout the rest of this assignment as an ArrayList, since that's more precise; it conveys the idea that behind the list is an array of fixed size in memory.) C has no such built-in ArrayList capability, though it does have fixed-size arrays. For this assignment, you will implement ArrayList-like functionality in C.

Grab these two files:

Your assignment is to create the file arraylist.c, which contains the implementations for all of the functions prototyped in arraylist.h.

Part 0

As a warmup to get your C skills flowing, write a standalone program to create an array of 10 random integers, and sort it using selection sort. Print out your results to the screen. Call this file sortarray.c when you submit it.

Remember the C tutorial that is linked on the course web page: it contains lots of information on how to write C programs and how to use arrays.

You may find the function random() useful, which will generate a random integer in the range fom 0 to (2**31)-1. Here's how you can use the system clock to seed it:

#include <stdio.h>
#include <time.h>
int main()
{
    srandom(time(NULL));
    printf("%i\n",random());
}

When debugging your code, however, seed the random number generator with a fixed seed; that way, your program runs the same way every time.

Part 1

Implement the functions init, print, and insert. Don't bother yet with making the array automatically double. To make sure that your code runs, modify arraytester.c so that the initial array size is large enough for the rest of the program (setting it to 100 initially will do it.)

Here's how to get your code to compile: type

gcc -o tester arraylist.c arraytester.c

which will create an executable file called tester.

Part 2

Implement get and delete, and implement auto-doubling as well.

Valgrind

Make sure that your program doesn't have any memory leaks: you should free up all garbage appropriately. We'll be testing this on the department Macs using "valgrind", which is a wonderful tool for detecting memory bugs in C. Valgrind is open source tool that is available for OS X and for Linux; unfortunately, it isn't available for Windows. The only tools I know of that work similarly for Windows are very expensive. If you are doing your programming on your own Windows machine, you'll have to do memory testing on one of the department machines.

To use valgrind, first compile your code with debugging info enabled:

gcc -g -o tester arraylist.c arraytester.c

You then run your executable through valgrind as follows:

valgrind tester