CSC161 2010F Imperative Problem Solving

Exam 3: Structures and Sorting

Assigned: Wednesday, 24 November 2010

Due: 11 p.m., Monday, 6 December 2010
Due (DB): 11 p.m., Friday, 3 December 2010


Exam Format

This is a take-home examination. You may use any time or times you deem appropriate to complete the exam, provided you return it to me by the due date.

There are seven problems on this examination. You must answer six of them. The problem are not necessarily of equal difficulty. Each problem may include subproblems. Six correct or mostly-correct solutions will earn you an A. Five correct or mostly-correct solutions will earn you a B. Four or three correct or mostly-correct solutions will earn you a C. Two or one correct or mostly-correct solutions will earn you a D. Zero correct or mostly-correct solutions will earn you an F. Failure to attempt the exam will earn you a 0. Partially-correct solutions may or may not earn you a partial grade, at the discretion of the grader.

Read the entire exam before you begin.

I expect that someone who has mastered the material and works at a moderate rate should have little trouble completing the exam in a reasonable amount of time. In particular, this exam is likely to take you about five hours, depending on how well you've learned the topics and how fast you work. You should not work more than eight hours on this exam. Eight hours of work, documented attempts on all problems, and a signed statement that There's more to life than computer science will earn you at least a C on this exam.

I would also appreciate it if you would write down the amount of time each problem takes.

Academic Honesty

This examination is open book, open notes, open mind, open computer, open Web. However, it is closed person. That means you should not talk to other people about the exam. Other than as restricted by that limitation, you should feel free to use all reasonable resources available to you.

As always, you are expected to turn in your own work. If you find ideas in a book or on the Web, be sure to cite them appropriately. If you use code that you wrote for a previous lab or homework, cite that lab or homework and the other members of your group. If you use code that you found on the course Web site, be sure to cite that code. You need not cite the code provided in the body of the examination.

Although you may use the Web for this exam, you may not post your answers to this examination on the Web. And, in case it's not clear, you may not ask others (in person, via email, via IM, by posting a please help message, or in any other way) to put answers on the Web.

Because different students may be taking the exam at different times, you are not permitted to discuss the exam with anyone until after I have returned it. If you must say something about the exam, you are allowed to say This is among the hardest exams I have ever taken. If you don't start it early, you will have no chance of finishing the exam. You may also summarize these policies. You may not tell other students which problems you've finished. You may not tell other students how long you've spent on the exam.

You must include both of the following statements on the cover sheet of the examination.

  1. I have neither received nor given inappropriate assistance on this examination.
  2. I am not aware of any other students who have given or received inappropriate assistance on this examination.

Please sign and date each statement. Note that the statements must be true; if you are unable to sign either statement, please talk to me at your earliest convenience. You need not reveal the particulars of the dishonesty, simply that it happened. Note also that inappropriate assistance is assistance from (or to) anyone other than Professor Rebelsky (that's me).

Presenting Your Work

You must present your exam to me in two forms: both physically and electronically. That is, you must write all of your answers using the computer, print them out, number the pages, put your name on the top of every page, and hand me the printed copy. You must also email me a copy of your exam. You should create the emailed version by making a tarball of any relevant code and attaching that tarball to the message. Failure to name and number the printed pages will lead to a penalty. Failure to turn in both versions may lead to a much worse penalty. Failure to turn in a legible version of the exam is also likely to lead to a penalty.

If your answer relies on provided files but you have not modified the files, you should include them in the tarball but should not include them in the printed copy of the exam.

In many problems, I ask you to write code. Unless I specify otherwise in a problem, you should write working code and include examples that show that you've tested the code.

Unless I explicitly tell you to document a procedure, you need not document that procedure. If I do tell you to document a procedure, you should document it with six-P style documentation. If do not tell you to document a procedure, you may still document that procedure and may use whatever form you find most appropriate.

Just as you should be careful and precise when you write code and documentation, so should you be careful and precise when you write prose. Please check your spelling and grammar. Since I should be equally careful, the whole class will receive one point of extra credit for each error in spelling or grammar you identify on this exam. I will limit that form of extra credit to five points.

I may give partial credit for partially correct answers. I am best able to give such partial credit if you include a clear set of work that shows how you derived your answer. You ensure the best possible grade for yourself by clearly indicating what part of your answer is work and what part is your final answer.

Getting Help

I may not be available at the time you take the exam. If you feel that a question is badly worded or impossible to answer, note the problem you have observed and attempt to reword the question in such a way that it is answerable. If it's a reasonable hour (before 10 p.m. and after 8 a.m.), feel free to try to call me in the office (269-4410) or at home (236-7445).

I will also reserve time at the start of each class before the exam is due to discuss any general questions you have on the exam.


Problem 01: Quicksort, Revisited

Topics: Sorting, Pointers

Consider the following version of Quicksort, based on code from Kernighan and Ritchie.

 * kr_qsort: sort v[left]...v[right] into increasing alphabetical order.
 *   Note: Renamed to avoid conflict with qsort in stlib.h.
kr_qsort (char *v[], int left, int right)
  int i, last;

  /* If the array contains fewer than two elements, do nothing. */
  if (left >= right) 
  swap(v, left, (left + right)/2);
  last = left; 
  for (i = left+1; i <= right; i++) 
    if (strcmp (v[i], v[left]) < 0)
      swap(v, ++last, i); 
  swap(v, left, last);
  kr_qsort (v, left, last-1); 
  kr_qsort (v, last+1, right);
} // kr_qsort

Traditionally, we've written sort with only two parameters, the array of strings to sort and the size of the array (or, more precisely, the size of the portion to sort).

 * Procedure:
 *   sort
 * Parameters:
 *   strings, an array of strings
 *   size, a non-negative integer
 * Purpose:
 *   Sorts the first size elements of strings.
 * Produces:
 *   [Nothing; Called for the side effect.]
 * Preconditions:
 *   size <= number of elements in strings
 * Postconditions:
 *   strcmp (strings[i-1], strings[i]) <= 0 for 0 < i < size.
 *   Elements have neither been added to nor removed from strings.

Rewrite kr_qsort to match this specification. You may not create additional helper functions.

Problem 02: Splitting Strings

Topics: Strings, Substrings.

In homework 8, you may have developed some code that found portions of a string. Let's generalize that code somewhat. Implement the following procedure that splits a string into different parts.

 * Procedure:
 *   split
 * Parameters:
 *   str, a string
 *   sep, a string
 *   parts[], an array
 *   n, a non-negative integer
 * Purpose:
 *   Split str into up to n separate parts.
 * Produces:
 *   [Nothing; Called for the side effect]
 * Preconditions:
 *   n <= size of parts
 *   str != NULL
 *   sep != NULL
 * Postconditions:
 *   parts[0] = a copy of the characters of str from character 0 to the
 *              last character before the first instance of sep
 *   parts[1] = a copy of the characters of str from the first character
 *              after the first instance of sep to the last character
 *              before the second instance of sep (or the end of the string,
 *              if there is no second instance).
 *   parts[i] = a copy of the characters of str from the first character
 *              after the ith instance of sep (or the '\0' at the end of the
 *              string if there are fewer than ith instances) to the 
 *              last character before the i+1st instance of sep (or the '\0'
 *              at the end of the string).
 * Practica:
 *   split ("a,b,c", ",", strings, 3)
 *     strings[0] = "a"
 *     strings[1] = "b"
 *     strings[2] = "c"
 *   split ("a", ",", strings, 3)
 *     strings[0] = "a"
 *     strings[1] = ""
 *     strings[2] = ""
 *   split ("a,b,c,d", ",", strings, 3)
 *     strings[0] = "a"
 *     strings[1] = "b"
 *     strings[2] = "c"
 *   split ("a,b,c,d", " ", strings, 3)
 *     strings[0] = "a,b,c,d"
 *     strings[1] = ""
 *     strings[2] = ""
 *   split ("lions and tigers and bears", " and ", animals, 3)
 *     animals[0] = "lions"
 *     animals[1] = "tigers"
 *     animals[2] = "bears"
void split (char *str, char *sep, char *parts[], int n);

Note: You will find the strstr procedure quite helpful.

Note: You may not use strtok in your solution.

Note: In case you could not tell it from the documentation, you should allocate new space for the strings you put into parts.

Problem 03: Iterating Hash Tables

Topics: Hash tables, Macros, Iteration

Here is a procedure that prints all the elements in a hash table.

print_table (struct hashtable *table)
  int i;
  struct element *tmp;

  for (i = 0; i < table->size; i++)
      tmp = table->contents[i];
      while (tmp != NULL)
          printf ("%s -> %s\n", tmp->key, tmp->value);
          tmp = tmp->next;
        } // while
    } // for
} // print_table

Here is a procedure that finds the first key in the table with value val.

char *
find_key (struct hashtable *table, char *value)
  int i;
  struct element *elt;

  for (i = 0; i < table->size; i++)
      elt = table->contents[i];
      while (elt != NULL)
          if (strcmp (elt->value, value) == 0)
            return elt->key;
          elt = elt->next;
        } // while
    } // for

  return NULL;
} // find_key

Remarkably similar, aren't they? And, as you know by now, when you start to write similar code, you need to factor out the similar parts. In this case, the similar part is iterating through the elements of the hash table.

a. Write a macro, ITERATE_TABLE(TABLE, ELT, CODE), that iterates through all of the elements of the hash table TABLE, setting ELT to each element in turn and then executing CODE.

For example, once you've written that macro, you should be able to rewrite the programs above as

print_table (struct hashtable *table)
  struct element *tmp;

  ITERATE_TABLE (table, tmp, 
                 printf ("%s -> %s\n", tmp->key, tmp->value));
} // print_table

char *
find_key (struct hashtable *table, char *value)
  struct element *elt;

  ITERATE_TABLE (table, elt, 
                 if (strcmp (elt->value, value) == 0) return elt->key);

  return NULL;
} // find_key

b. Using ITERATE_TABLE, write a function, int count_values (struct hashtable *table, char *value), that counts how many times value appears in table.

Problem 04: Inserting Into Lists

Topics: Linked lists, Pointers

Consider the following type declarations.

struct string_node {
  char *data;
  struct string_node *next;

struct string_list {
  struct string_node *head; // The first element in the list
  struct string_node *tail; // The last element in the list

Write the following procedure.

 * Procedure:
 *   slist_insert_before
 * Parameters:
 *   lst, a list of strings
 *   target, a string
 *   newvalue, a string
 * Purpose:
 *   Insert newvalue into lst immediately before the first
 *     instance of target.
 * Produces:
 *   [Nothing; Called for the side effect.]
 * Preconditions:
 *   target appears at least once in lst.
 * Postconditions:
 *   The length of the list has increased by 1.
 *   Another copy of newvalue has been added to the list.
 *   That copy of newvalue immediately precedes target in lst.
 *   No other elements have been added.
 *   No elements have been deleted.
void slist_insert_before (struct string_list *lst, char *target, char *newvalue);

Warning! Don't forget to consider the special cases.

Problem 05: Cyclic Lists

Topics: Lists, Pointers, Cycles

As we noticed in our exploration of the simplest linked lists, it is possible to set up linked structures that loop. For example, one can set the cdr of a node to itself. There are, of course, other loops. For example, one can set the cdr of the cdr of a node to the original node.

Document and write a procedure, struct node *np_loop(struct node *np), that finds and returns the first duplicate node in the linked list pointed to by np. If there are no duplicate nodes in the list, return NULL.

Problem 06: Transforming Lists

Topics: Lists, Function Pointers

Using the definitions from an earlier problem, write a procedure, struct string_list *slist_transform (struct string_list *list, void (*fun)(char *str)) that transforms a list by applying fun to each element.

For example, consider the following definitions.

star_first (char *str)
  if (str != NULL)
    str[0] = '*';
} // star_first

We can use that definition to set the first letter of each string to an asterisk.

  slist_transform (strings, star_first);

Problem 07: Reading Lists

The other day, I was trying to write a function to read in a list of strings from a file that I knew contained no lines of more than eighty characters. Here's what I came up with.

#define MAXLINE 80
  char str[MAXLINE+1];
  struct string_list *strings = new_string_list ();
  while (fgets (str, MAXLINE+1, stdin) != NULL)
    sl_append (strings, str);

However, when I printed the list, I did not get the output I expected.

Figure out what's going wrong with that code and fix it.


Here you will find answers to questions of general interest. Please check here before emailing your questions!

Problem 1

Did you know that your code for kr_qsort is borken (or even broken)?
Will you write us a test suite for sort?
Yes. It can be found in Examples/Exam3/Problem1.
Will you give us the code for swap?
Yes. It's in the directory mentioned above. I'm repeating it here.
swap (char *v[], int i, int j)
  char *tmp = v[i];
  v[i] = v[j];
  v[j] = tmp;
} // swap
Any hints?
Look at how we wrote merge sort.

Problem 4

Do we have to use string_list? Can we just call them list and node? Please? Or at least can we have size in our struct?
No. Yes. Yes. Yes.

Problem 5

Can you give some examples of looped lists?
Here's the simplest one.
  struct node *n1 = cons("1", NULL);
  set_cdr (n1, n1);
Here's that one with a little bit of leader.
  struct node *n2 = cons ("2", n1);
  struct node *n3 = cons ("3", n2);
Here's a longer loop.
  struct node *n4 = cons ("4", NULL);
  struct node *n5 = cons ("5", n4);
  struct node *n6 = cons ("6", n5);
  set_cdr (n4, n6);
And a bit of leader for that loop.
  struct node *n7 = cons("7", n4);
And a list that starts at a different point in that loop.
  struct node *n8 = cons("8", n5);

Problem 7

What do the ellipses represent?
Elided code. Basically, the #define happens near the top of the program and the loop happens somewhere in the middle of some procedure (say, main).
What do you expect the output to be?
A list of the strings that the user entered.


Do you actually run the code you give us?
Do you read these? I answered the question last time.
Why did you give us seven questions instead of the normal five?
These are smaller.
Can we get extra credit for errors in the Q&A section or the errata section?
Certainly not.
What happens if we submit answers to all seven problems?
I grade all of them and credit the best of them.


Here you will find errors of spelling, grammar, and design that students have noted. Remember, each error found corresponds to a point of extra credit for everyone. We usually limit such extra credit to five points. However, if I make an astoundingly large number of errors, then I will provide more extra credit.

Super-duper extra credit



Tuesday, 23 November 2010 [Samuel A. Rebelsky]

  • Wrote problems.

Wednesday, 24 November 2010 [Samuel A. Rebelsky]

  • Formatted.
  • Added two more problems.
  • Released.
  • Improved after corrections from class.

Monday, 29 November 2010 [Samuel A. Rebelsky]

  • Corrected error.

Thursday, 2 December 2010 [Samuel A. Rebelsky]

  • Corrected error.
  • Added sample code for testing sorting algorithm.

Friday, 3 December 2010 [Samuel A. Rebelsky]

  • Added Q&A on problems 4 and 7.

Sunday, 5 December 2010 [Samuel A. Rebelsky]

  • Added lots of junk.

Monday, 6 December 2010 [Samuel A. Rebelsky]

  • A hell week bonus for the students.


Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Mon Dec 6 11:11:32 2010.
The source to the document was last modified on Mon Dec 6 11:11:30 2010.
This document may be found at

Samuel A. Rebelsky,