TDT4102, Spring 2009

Exercise 10

Deadline: 17.04.2009


The objective of this exercise:

General requirements:

Recommended reading:


Part 1: Template functions (15%)

a) Write a template function called shuffle for shuffling the elements of an array. Use the array to be shuffled as the first parameter and the size of the array as the second parameter. Shuffling an array implies reorganizing the elements in a random order.
You should be able to compile and run the following code that uses the template function:

    int a[] = {1,2,3,4,5,6,7};
    shuffle(a, 7);

    double b[] = {1.2, 2.2, 3.2, 4.2};
    shuffle(b, 4);

    string c[] = {"one", "two", "three", "four"};
    shuffle(c, 4);

b) Write a template function called maximum that takes two values of the same type as arguments and returns the highest value.

c) What is the restriction for using the above function (related to the way you have implemented it)? It will work for all basic data types (int, double, char), but if you try to call the function with arguments of a user-defined type such as a Person or a Circle class, you will probably discover that it will not compile. Make sure you understand why this problem occurs.


Part 2: Iterators (15%)

a) Create a vector for strings (vector<string>) and insert a number of strings into the vector (using the push_back() method).
Print out each string in the vector with a for-loop that uses iterators (and NOT the index operator).

b) Use a reverse iterator to print out the content of the vector in the reverse order.

Part 3: Lists (20%)

a) Create a class Person with member variables for last and first name. Include the constructors, member functions and overloaded operators that you find useful.

b) Create a list<Person> variable for persons and write a function for inserting Person objects in sorted order  (based on the alphabetical order of names).
   
    void insertOrdered(list<Person> &p, Person person);

HINT! strings can be compared with operators such as < or >.

c) Write a loop in the main method that prints out the name of the persons (to cout).

Part 4: Sets and maps (30%)

HINT! This assignment is rather straight forward to implement with the appropriate use of set and/or map!

a) Create a class PhonebookEntry with member variables for a person's name and phone numbers. A single person may have many different phonenumbers and each phonenumber should be associated with a unique label such as "home", "mobile", "work". By "unique" we mean that there should be only one number with the label "mobile" for the same person. It is up to you how you want to represent the name of the person (as separate values for first and last name, or a single string with both).

b) Create a class Phonebook that is used to store and update PhonebookEntries. Include a member function called add that takes a PhonebookEntry as argument. If the person name in the argument entry does not exist in the phonebook, the argument entry is simply added to the phonebook. If the person's name already exists in the phonebook, the argument entry is used to update the list of phonenumbers for this person: if a label exists the number is replaced, if a label does not exists the number is added, existing numbers that are not updated should be kept).

c) Write a loop in the main method that prints out the content of the Phonebook (to cout).

Part 5: Searching (20%)

a) Extend the Phonebook class in the previous part with a function called find for searching the phonebook.

    bool Phonebook::find(string name, string label, PhoneBookEntry& result);

The find function should be quite flexible and support searching by substrings. The first parameter is the name (or substring of a name), the second parameter is the label associated with a phone number (or substring of the label). The third argument is used to return the name and numbers found. If the label argument is an empty string (""), the returned entry should include all the phonenumbers for this person, if the label argument is a non-empty string, the numbers included should be only the one(s) that match the string in the label argument. A boolean return value is used to indicate if a matching entry was found or not. NB! The function returns the first matching entry.

Example:

PhonebookEntry result;
if (pb.find("Aalb", "mob", result){
    cout << result << endl; //assuming that the << operator is overloaded
}

Result:
Trond Aalberg
    mobile: 97855667

if (pb.find("Aalb", "", result){
    cout << result << endl; //assuming that the << operator is overloaded
}

Result
Trond Aalberg
    mobile: 97855667
    home: 76334455
    work: 73597952

HINT! You need to do substring searching using the find(string&)  function in the string class. The find() function searches for substrings and is case-sensitive. If you want a case-insensitive solution you will have to implement your own substring searching algorithm.