# CSE 532S Generic Algorithms and Threads Studio

Please complete the required studio exercises listed below, along with any of the (optional) enrichment exercises that interest you.

Please make sure that the name of each person who worked on these exercises is listed in the first answer, and that you number your answers so they are easy for us to match up with the appropriate exercise.

### Required Exercises

1. As the answer to the first exercise, list the names of the people who worked together on this studio.

2. Open up Visual Studio 2013, make sure your settings are for C++, and create a new project for this studio (for example named something like generics_and_threads).

In the main C++ source code file for the project (which should be named something like generics_and_threads.cpp) please modify the main function signature so that it looks like the standard (i.e., portable between Windows and Linux) main function entry point for C++: int main (int, char * []) and make sure the code compiles and that you can run the program from a windows terminal shell (hint: you can run cmd from the main start window to get a terminal shell).

As the answer to this exercise, please give the full path to the directory where the executable program was created.

3. One useful feature of the STL algorithms is that they are designed to work as well (or even better) with native pointers as with other iterator types declared for the various STL containers. In your main function, declare a (plain-old-C++-style) array of integers, initialized with an odd number of unsorted values in which each value appears one or more times. For example:

`int arr[] = {-2, 19, 80, -47, 80, 80, -2};`

Declare a variable of type `int *` that is initialized to point just past the end of the array (hint: use pointer arithmetic with the name of the array, and the number of positions in the array which you can calculate by using the `sizeof()` operator twice). Use the STL `copy` algorithm to print out the contents of the array by passing it the starting address of the array, the pointer that is just past the end of the array, and a variable of type `ostream_iterator<int>` (an output iterator for printing integers using an ostream) that is initialized with `cout` (optionally you can also pass the iterator's constructor a string like `" "` or `"\t"` to print between each of the integer values in the array). Build your program and give its output as the answer to this exercise.

4. Modify the previous exercise so that instead of a one-dimensional array of int values it uses a two-dimensional array of integers. Initialize the array with different values and then divide it into discrete ranges so that (except for the last range) the position just past the end of one range is the beginning position of the next range. Write or use a function (or STL algorithm) that can take a pointer to the beginning of a range and a pointer just past the end of a range, add up the integers in that range, and give that sum as its result. Pass each of the ranges in the array to that function, and print out the result given by the function for each of those ranges. Show the output of your program (which should show those sums) as the answer to this exercise.

5. Modify the previous exercise so that after the input array is initialized, each run of the function is performed in a separate C++11 thread. Construct a separate array where each thread will place the result of the function, and give each thread its own location in that array (e.g., by passing it a pointer). The main thread should join with each of the other threads, and then should print out the array of results from the threads. As the answer to this exercise please show the code you wrote to do this.

### Enrichment Exercises (Optional)

6. Instead of initializing the array in the last exercise from the main thread, have the individual threads load their ranges of the array from separate input files. Show the code you wrote to do this as the answer to this exercise.

7. Modify either or both of the previous two exercises so that the joins are performed in a guard object (to prevent possible issues due to exceptions etc. from causing a join to be missed). Show the code you wrote to do this as the answer to this exercise.