DM510 Required Assignment 0: C-programming
Exercise suggested by Arun Vadiveal.
In this assignment, your task is to solve a series of small
programming tasks in C. For each task a file with method stubs is
available. These files will help you get started, but they also serve
to define the input and output of functions. The files can be downloaded here. You can unpack them with the command tar -xvf assignment0.tar
. A bash
script run
is included to compile and run the files for
each task. You may need to make the script executable with chmod
u+x run
. The script also includes a simple test case for each
task, to help avoid misunderstandings concerning the input/output
format. As a minimum your implementation must succeed in each of the
simple tests. Your implementation will be tested more thoroughly with
additional tests during the evaluation, thus make sure to test
additional border line scenarios yourself.
Task 0: Hello World
For this task, you are given the file "helloworld.c", and need to add the necessary lines of code to make it print "Hello World" to standard out. Try compiling the file using gcc
. You can type man gcc
in a console to read about how to use it.
Task 1: Repeat
For this task, you are given the file "repeat.c". Your implementation must read the parameters passed to the program and write them out in the same order seperated by a single space. For instance ./repeat These are arguments.
should result in the following being written to standard out: These are arguments.
.
Task 2: Linked List
For this task, you are given the file "llist.c". In this task you will need to know a little about pointers and structs. If you feel lost, make sure to take a look at the Online C Programming course, especially the sections: "Pointers in C" and "Structures in C".
You need to implement two functions. The first is to insert an element into a linked list: int insert( char *buffer, int index )
. The parameters are a pointer to a buffer containing a message and an index for the position to insert the element in (0 results in the element being the new head, 1 means after the head, etc.). The global variable head
serves as an entrypoint to the linked list. Initially the head
is NULL
, and the first insert is made by allocating memory for the structure and making head
point to this memory location. As the content of the buffer may be changed by the caller, it is important to allocate memory for the message, copy the contents of the buffer and store the pointer to the allocated memory, instead of just using the pointer given as input. Also in C allocated memory can contain random data, so make sure to explicitly set the next
-pointer to NULL
. Additional inserts are done similarly, but remember to update the appropriate next
-pointers such that elements are not lost.
The second function, llelem_t* extract_head( )
, is a simple function that returns the current head
of the linked list, while removing it from the list. Removing the head
-element is done by simply referring to the next
element as the new head.
Task 3: Linked List Extension
For this task, you will need a working implementation of Task 2. The purpose is to get more familiar with string manipulation in C. So if you haven't already read about strings in C, you should take a look at the Online C Programming course and also try typing man string
in a console. In addition to the existing method for inserting single elements, you need to implement the function: void list_from_string( char* );
. The function will insert a list of elements based on the string given as parameter. The string is considered a comma separeted list of elements, such that list_from_string( "Yoda,am,I" )
would result in the linked list with the elements: "I" -> "am" -> "Yoda". By using the already implemented method for insertion, the task is to repeatedly extract the substring up until the first comma or the '\0' character, and perform an insert( substring, 0 )
.
Submitting your solution
For submitting the sources proceed as follows: Create the following directory structure
assignment0/
Put the sources and also the bash script run
in that
directory.
Before you submit your solution, make sure your output of the run
script is identical to the following output:
Test 0: Compile succeeded without warnings output: 'Hello World' is equal to expected: 'Hello World' Test: Success Test 1: Compile succeeded without warnings output: 'Please repeat this.' is equal to expected: 'Please repeat this.' Test: Success Test 2: Compile succeeded without warnings output: 'This is a small message.' is equal to expected: 'This is a small message.' Test: Success Test 3: Compile succeeded without warnings output: 'Testing list from command line arguments.' is equal to expected: 'Testing list from command line arguments.' Test: Success
Change to the directory assignment0
and type aflever DM510
.
24.02.2011: Based on the problems with the file system in the IMADA terminal room, the deadline for the 0th mandatory assignment is moved from February 25th, 11:00 to March, 4st, 11am.
Frequently Asked Questions (FAQ)
- Are we allowed to work in groups?
Note that you will find FAQ for User Mode Linux here.
No, this assignment has to be solved individually. However feel free to discuss and problems with the TA or with other students. Note, that we will perform a plagiarism check.