CS 14 - Lab


CS 14 Homepage

First Task - Lab Practical

Complete the lab practical as assigned by the TAs. You will have 2 hours to complete the practical. You will turn in your lab using the electronic turnin. WATCH THE TIME! The electronic turnin will be gone 2 hours after the scheduled start time of your lab section and ASBOLUTELY NO LATE LAB PRACTICALS WILL BE ACCEPTED NO MATTER WHAT THE REASON EVEN IF YOU ARE ONLY 1 SECOND LATE. DO NOT TRUST THE CLOCK ON YOUR COMPUTER!!! IT IS NOT THE SAME AS THE CLOCK ON THE TURNIN SERVER. MAKE SURE YOU OPEN A TURNIN PAGE AND REFRESH IT IF YOU WOULD LIKE TO KNOW THE CURRENT TIME. REMEMBER THAT LAB PRACTICALS ARE 8% OF YOUR GRADE. DO NOT PUSH THE TURNIN TIME BECAUSE IF YOU MISS THE TURNIN, YOU ARE OUT OF LUCK. I suggest that you implement the functions in the order specified above and turn in a copy of your code each time you get a new function working so that you can maximize your points. You may not use ANY reference material at all and, should I even need to specify that this includes man/info pages, web, IM, chat, email, book, previous code written by you, code written by anyone, etc. You may not look at ANY reference while doing this lab practical. You may only look at this web page containing the lab practical, electrontic turnin, your code that you are currently writing for the practical, and gdb or ddd.

Second Task - Advanced STL - Associative Containers and Algorithms

In this lab, you will be learning about a few advanced STL topics including associative containers and algorithms. You will do some short exercises to help introduce you to these topics. For this lab you will be working independently. You may want to look at the STL reference guide for more information.

Associative Containers - Maps

The sequential containers you have used thus far (list, vector, and deque) are very useful but they provide you with code that you could easily reproduce in a few hours. However, associative containers provide implementations for data types that you could not so easily duplicate in a few hours of coding. Associative containers (aka associative arrays) store items based on keys. In a compiler, a symbol table can be used to store memory addresses of variables and functions within the code. If you were to write a compiler and you used an associative array for your symbol table, you can easily accesses the memory addresses using syntax such as this:

This is a very intuitive way to code, but C++ does not have built in support for associative arrays (some languages such as Perl have built in support for associative arrays). Luckily, with STL, associative arrays are very simple to use and you can use anything that you want as the index: built-in types, user-defined structures, classes, etc. Associative containers are implemented as red-black trees for efficiency.

Today you will be learning about the map associative container. The map container comes in the form of map<Key, T, Compare> where Key is the key you will use to do lookups, T is the data to store at that location, and Compare is a function object that compares two key values. For maps using built-in types for keys, you can use the less< > function object template:
If the key values are user defined types, you will need to overload operator <().

Part 1 - Learning Maps

For part 1 of your assignment, you will be doing a very simple program involving maps and the number of days in each month. Key will be the month name and T will be the number of days in that month (see above for the syntax used to declare a map). Use the following procedure to complete part 1:

Algorithms

STL also provides you with various templatized algorithms that are very simple to use. Algorithms are simply templatized functions. There are many types of algorithms but I will only cover 2: sort and accumulate.

sort - The sorting algorithm allows you to sort any type of data structure whether it is a built in type of a user defined type. Sort works by using the < operator for the objects being sorted. If you are sorting built-in types the < operator is already defined for you. If you are sorting user defined types, you will need to overload the < for the type. Sort can be used in two different ways:
accumulate - Accumulate repeatedly applies a function to each member of the input sequence and returns the result. For instance, if you want to add up all of the values in an integer array, you can use accumulate to do this. However, accumulate is flexible so that you can specify what function you want to apply to each member of the input (for instance you could muliply all of them together). Accumulating is such a common operation, that many computer architectures have accumulation registers (you will learn more about this in cs61 and cs161). The STL accumulate comes in 2 different offerings:

Part 2 - Learning algorithms

For part 2 of your lab, you will use the sort and the accumulate algorithms. You will create a list of integers and an array of integers and apply these algorithms to both. Use the following procedure to complete part2:

Point Breakdown For Lab Assignment

You may demo your program in lab or it will be graded from your electronic submission (due 24 hours after the end of your lab section). Please show the output from part1 and part2 to the TA. The TA will also look at your code for correctness when checking out. Remember, to receive credit for this lab, you MUST turn the code in online. If you do not turn in your code online, you will lose points for your lab. All code must be turned in online for archival purposes.

Remeber to compile your code using the flags "-g -Wall -W -Werror -pedantic". For this lab you do not have to use a makefile since you are creating 2 standalone programs each in a single file.

© 2003 UC Riverside Department of Computer Science & Engineering. All rights reserved.