COMP 411: Lab 4: Arrays and Pointers in C

Due on Wednesday, February 7, 2018, 11:59 PM


In this lab, we will focus on bubble sort of character strings. You are to implement two different versions of the bubble sort algorithm. Please read the descriptions of each carefully, and implement each using the starter files provided.

Relevant readings:

Declaring strings in C: Unlike other high-level languages that explicitly provide support for strings (e.g., the String class in Java), there is no built-in type for strings in C. Instead, strings are handled as arrays of characters. Here are some ways of declaring strings in C:


        char String1[10];               // an uninitialized string of up to 10 characters

        char String2[10] = "Hello";     // a string with space for 10 characters, initialized to "Hello"

        char String3[10] = { 'H', 'e', 'l', 'l', 'o', '\0'}
                                        // same as previous one, but note the '\0' terminator

        char String4[]   = { 'H', 'e', 'l', 'l', 'o', '\0'}
                                        // a strong with space for 6 characters; compiler counts for you

String length and termination: Unlike Java, the length of the string is not stored within the data structure. The entire data structure is simply the array of characters, and information about its size is typically not encoded by the compiler into the executable. Thus, at runtime, the actual length of each string is typically unknown. Instead a different approach is used.

Each string is terminated using a special character called the "NULL" character, which is ASCII 0, and can be represented as '\0'. Hence, in the examples above, String3 and String4 were initialized as character arrays, and the string terminator '\0' had to be explicitly used to terminate the strings. There is a standard C string library (string.h) that provides many useful string processing functions, all of which expect NULL-terminated strings. You access those functions by using the #include<string.h> directive at the top of the main program file.

The NULL character is automatically appended by the compiler when the following declaration is used:


        char String2[10] = "Hello";     // a string with space for 10 characters, initialized to "Hello"

With this type of declaration, the use of double quotes ("Hello") explicitly indicates that a string is intended and, therefore, the compiler automatically appends a NULL character, making it equivalent to the character array: { 'H', 'e', 'l', 'l', 'o', '\0'}

If you forget to terminate the string, none of the C string functions will work properly. For instance, if String3 were not terminated using '\0', a subsequent call to printf(String3) could very well produce output like: HellojB%$@#($M. Even worse, printf() might go out of the bounds of space available to your program while looking for the string terminator, most likely causing a "segmentation fault" error.

When declaring strings, be careful to count the space that will be needed by the terminator when allocating space for the string variable. Thus, the string "Hello" needs 6 characters, not 5. Likewise, String1 above can store at most 9 real characters, plus its terminator. Similarly, String2 can have at most 9 real characters plus the terminator, even though it is initialized to a shorter string. Thus, String2's initial contents could very well be { 'H', 'e', 'l', 'l', 'o', '\0', '#', '@', '%', '$'}, where the last four characters are junk; these trailing characters are ignored by all C library functions because the string terminates at the '\0'.

All C library routines for reading and writing strings --- scanf(), printf(), strcpy, etc. --- always append the terminator at the end of any new string created or updated. However, if you are manually creating a new string and copying individual characters into it, it is your responsibility to append a terminator at the end. For example:


        char String2[10] = "Hello";     // a string with space for 10 characters, initialized to "Hello"
        ...
        ...
        String2[2] = '\0';              // puts a string terminator after the 'e'

The above code fragment turns String2 into { 'H', 'e', '\0', 'l', 'o', '\0', '#', '@', '%', '$'}, which is effectively the string "He".


Exercise 1

For the first version, you will implement the primary data structure as a 2-dimensional array Strings[NUM][LEN], where NUM is the number of strings, and LEN is the maximum length allowed for the string: the first index i gives you the i-th string, and the second index j gives you the j-th character within that string.

Using the starter file ex1.c, implement bubble sort of strings. Your code must strictly follow these specifications:

Compile and run your program on inputs of your choice, and also make sure it runs correctly on the sample inputs provided. Refer to the sample inputs and outputs provided on exactly how to format your I/O.


Exercise 2

Repeat the above exercise (copy ex1.c to ex2.c), but this time use C string library functions to simplify the code of the bubble sort. Name the file containing your code ex2.c. Compile and run your program on inputs of your choice, and also make sure it runs correctly on the sample inputs provided.

Test Inputs, Due Date and Submission Procedure

Reminder: Before beginning work, it is generally a good idea to run the following command so your terminal environment is set up properly:

% /home/porter/comp411/bin/comp411start

Sample inputs and corresponding outputs are provided under /home/porter/comp411/samples/lab4. You should try running your program on the sample input files provided, and make sure the program's output is identical to that in the sample output files.

Your assignment must be submitted electronically by 11:59pm on Wednesday, February 7.

You will submit your work on Exercises 1-2, specifically, the files ex1.c and ex2.c.

How to submit: Navigate to the folder where these files are stored (~/comp411lab/lab4), and run the following command for running a self-checking script (which does not submit the files):

 % cd ~/comp411lab/lab4
 % /home/porter/comp411/bin/selfchecklab4

If the self-check did not report any errors, you can submit your work as follows:

 % cd ~/comp411lab/lab4
 % /home/porter/comp411/bin/submitlab4

When you invoke the submit script, it submits your files, and once again performs a quick check on your source files (including compiling, running and comparing the output of your program with the expected output, for all sample input/output files), and provides a quick summary of any errors. Your work is submitted regardless of whether errors were found or not, but you are free to fix any errors and submit again before the due date.

In case of any problems, please contact the instructor or the TAs.


Revised: 25 September 2005, f.mokhtarian@surrey.ac.uk
Revised: 1 February 2017 and 21 September 2017, Montek Singh, montek@cs.unc.edu

Last updated: 2018-02-17 11:48:56 -0500 [validate xhtml]