Today's lecture and studio focus on how different kinds of iterators (including plain-old pointers) can be used in different algorithms from the C++ standard library, and how callable types can be used to extend the behaviors of some of those algorithms further. Those features in turn depend significantly on details of C++ templates, traits, and other advanced generic programming topics that we have considered in recent lectures and studios.
Please complete the following required exercises. I encourage you to please work in groups of 2 or 3 people on each studio (and the groups are allowed to change from studio to studio) though if you would prefer to complete any studio by yourself that is also allowed.
As you work through these exercises, please record your answers, and when
you finish them please log into Canvas, select this course in this semester, and
then upload a .txt
file containing your answers on the Canvas page
for this studio assignment. Only one submission per team, please, and if
you need to re-submit it the person who originally submitted the studio should
please be the one to do that.
Make sure that the name of each person who worked on these exercises is listed in the first answer, and make sure you number each of your responses so it is easy to match your responses with each exercise.
First, ssh into shell.cec.wustl.edu
using your WUSTL
Key id and password and then use qlogin
to log into one
of the Linux Lab machines and then confirm that the version of g++
there is correct (as you did in Studio 0). Then
cd
into your directory for this course this semester, create a new
subdirectory for this studio, and cd
into it.
Copy over the Makefile
from one of your previous studios into your
directory for this studio, and as you work on this studio update it so that it
builds an executable binary file named studio18
from the source and
header files you develop.
Add a new source file and in it define a main function that declares a (plain-old C-style) array of integers, initialized with an odd number of unsorted values in which at least a few of of the values appear more than once, e.g.,
int arr[] = {-2, 19, 80, -47, 80, 80, -2, 19, 19};
Declare pointer variables that are initialized to point to the first element
in the array, and just past the end of the array, respectively; then use the
copy
algorithm to print out the contents of the array by passing it
those pointers and a variable of type ostream_iterator<int>
that is initialized with cout
and a string like " "
to print between each of the integer values in the array.
Compile and run your program and as the answer to this exercise show the code you wrote and the output your program produced.
In your main function, declare a container variable of type
vector<int>
and an iterator variable of type
back_insert_iterator<vector<int> >
and pass
the pointers from the previous exercise and that iterator into a call to the
copy
so that it will copy the contents of the array into the vector,
and then use other appropriate iterators to print out the contents of the vector
to the standard output stream (again with spaces between the elements as in
the previous exercise).
Compile and run your program and as the answer to this exercise show the code you wrote for this exercise and the output produced by that code.
Use the sort
algorithm to sort the integers in your array from the
previous exercises into non-decreasing order (smallest to largest), and then again
use the copy
algorithm to have your program print out the contents of
the sorted array.
Compile and run your program and as the answer to this exercise show the code you wrote for this exercise and the output produced by the code you added.
Use the adjacent_find
algorithm to locate and print out each range
of repeated elements in the sorted array from the previous exercise on a separate
line, as in:
-2 -280 80 80
etc.
Compile and run your program and as the answer to this exercise show the code you wrote for this exercise and the output produced by the code you added.
Sort the contents of the vector into non-increasing order (largest to smallest)
by passing an object of type greater<int>
as a third parameter
to the sort
algorithm and then again use the copy
algorithm to print out the contents of the sorted vector.
Compile and run your program and as the answer to this exercise show the code you wrote for this exercise and the output produced by the code you added.
Use the count
and accumulate
algorithms to compute the
median (middle value), mean (average), and mode (most frequent value) over the
integers in the sorted vector from the previous exercise.
Hints: with an odd number of positions the median is easy to
calculate, using pointer arithmetic; to avoid round-off errors use a
float
initialized with the result of running the
accumulate
algorithm to calculate the mean; one way to calculate
the mode is to iterate through one of the sorted containers and each time a new
value is found call the count algorithm on the remaining sorted range to see how
many of that value there are and then skip over that many repeated elements
(using pointer arithmetic).
Compile and run your program and as the answer to this exercise show the code you wrote for this exercise and the output produced by the code you added.
For this studio, please turn in the following:
Page posted Saturday November 12, 2022, by Chris Gill.