You must work ALONE for this and all other labs and homeworks. Failure to adhere to this rule will result in an Academic Integrity Violation. You will only be able to work in groups for projects. Updates to this assignment will be posted on the course web page.
We recommend that you work on CSUG machines for this lab.
For Linux and Mac users, simply type: 'ssh netid@csugXX.csuglab.cornell.edu' into the
terminal. (Replace XX with a number between 01-14 and netid with your netID. Do not include quotes.)
For Windows users, download an SSH client.
The recommended SSH client is PuTTY.
To connect to CSUG machines using PuTTY:
1.) Open PuTTY
2.) Under Host Name enter: 'netid@csugXX.csuglab.cornell.edu'
(Replace XX with a number between 01-14 and netid with your netID. Do not include quotes.)
3.) Press Open
4.) A window labeled 'PuTTY Security Alert' should pop-up which provides
a warning about the host key. Click Yes.
5.) You are connected.
In this lab we will implement 3 functions in the file arraylist.c for an arraylist of ints: arraylist_add, arraylist_insert, and arraylist_free.
References. Searching the Internet will generally find an answer to nearly any conceivable question about the C programming language. However, information on the Web is not always accurate or complete. We recommend reference books over web sites. In particular, C: A Reference Manual (5th Edition) by Samuel P. Harbison and Guy L. Steele Jr. is a required textbook for this class and should be your first source for reliable information on C.
Download the file arraylist.c.
typedef struct arraylist { // update if necessary int *buffer; // pointer to allocated memory int buffer_size; // the max number of integers the buffer can hold int length; // the number of integers stored in the list } arraylist;
A struct is an aggregate type that has accessible fields called members (see the slides for more information).
You will be implementing three functions in the file arraylist.c.
void arraylist_add(arraylist *a, int x) { // your code goes here }
void arraylist_insert(arraylist *a, int index, int x) { // your code goes here }
void arraylist_free(arraylist *a) { // your code goes here }
When you are done implementing the functions, compile your program using gcc and run it.
$ gcc -g -Wall -std=c99 -o arraylist arraylist.c $ ./arraylist
If you have implemented everything correctly, the output should be:
[0, 1, 2] Insert position 0: [100, 0, 1, 2] Insert position 1: [0, 100, 1, 2] Insert position 2: [0, 1, 100, 2] Insert position 3: [0, 1, 2, 100] Clean: [0, 1, 2]
You should use valgrind to check your code for memory leaks.
$ valgrind -q --leak-check=full ./arraylist