CS 4448 - Spring 1998
Object-Oriented Programming and Design
Homework #8
Due on Thursday, April 9 at the start of class.
You must print out your code and the output from running the program.
You DO NOT need to mail me the code. The various tests can made into
several separate programs or put into one. I leave that up to you.
Note: on nag the stl include files can be found at /tools/cs/gcc/include/g++
Code re-use via Templates
This assignment uses the STL. Putting the command "using namespace
std;" in your program should save on typing and be useful.
Using iterator.h
Use two simple iterators. Get 2 numbers from the user and then
output the result. istream_iterator must be used for getting input and
ostream_iterator for the output.
The program output should be as follows.
Enter two integers: 12 25
The sum is: 37
Use an appropriate template from the STL
Make a class that contains a list of doubles. Doubles can be added to
the front or back of the list. They can also be removed from the
front or back of the list. First you will add 2.2 to the front then
3.5 to the front and finally 1.1 to the back. Then print the contents
using a for loop. Then remove the value from the front and print the
new contents. Finally change the value in position [1] to 5.4.
The program output should be as follows.
values contains: 3.5 2.2 1.1
After popping the front values contains: 2.2 1.1
After values[1] = 5.4 values contains: 2.2 5.4
Use the vector class and numeric.h and algorithm.h
Create a vector of integers. Initialize the vector to
{100, 2, 8, 1, 50, 3, 8, 8 ,9, 10}. First output the vector using the copy
function. Then perform a sort and print the output. Then
random_shuffle on the vector and then print out the vector again. Then
use count_if, min_element, max_element, and accumulate to generate the
output that follows.
Some code you can use for the last two parts of the output
You must write outputSquare.
for_each(v.begin(),v.end(), outputSquare);
.....
You must write calculateCube.
transform(v.begin(),v.end(), cubes.begin,calculateCube);
Then do output of cubes.
The program output should be as follows.
Vector v: 100 2 8 1 50 3 8 8 9 10
Vector v after sort: 1 2 3 8 8 8 9 10 50 100
Vector v after random_shuffle: 1 8 9 10 50 100 2 3 8 8
Number of elements matching 8: 3
Number of elements greater than 9: 3
Minimum element in Vector v is: 1
Maximum element in Vector v is: 100
The total of the elements in Vector v is: 199
The square of every integer in Vector v is:
The cube of every integer in Vector v is:
Adam Jonathan Griff,
computer@griffmonster.com
Copyright © University of Colorado. All rights reserved.
Revised: April 2, 1998