CS 4448 - Spring 1998
Object-Oriented Programming and Design
Homework #8 - A Solution

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.
A possible code solution is:

// Code using the input and output STL iterators.
#include <iostream.h>
#include <iterator>

int main() {
  int number1, number2;

  cout << "Enter two integers: ";

  istream_iterator<int> inputInt(cin);
  number1 = *inputInt; // read first int from standard in
  ++inputInt;  //move iterator to the next input value
  number2 = *inputInt; // get next

  ostream_iterator<int> outputInt(cout);
  cout << "The sum is: ";
  *outputInt = number1+number2; // output result to standard out
  cout << endl;
  return 0;
}

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.

A possible code solution is:

//Code using deque from the STL
#include <iostream.h>
#include <deque>
#include <algorithm>

int main() {
  deque<double> values;
  ostream_iterator<double> output(cout," ");

  values.push_front(2.2);
  values.push_front(3.5);
  values.push_back(1.1);

  cout << "values contains: ";

  for ( int i=0; i < values.size(); i++)
    cout << values[i] << ' ';

  values.pop_front();
  cout << endl << "After pop_front values contains: ";
  copy(values.begin(), values.end(), output);
  
  values[1] = 5.4;
  cout << endl << "After values[1] = 5.4 values contains: ";
  copy (values.begin(), values.end(), output);
  cout << endl;
  return 0;
}

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.

A possible code solution is:

// Code using Vector, accumulate,  and some algorithms
#include <iostream.h>
#include <algorithm>
#include <numeric>
#include <vector>

bool greater9(int value) {return value<9; }
void outputSquare(int value) { cout << value*value << ' '; }
int calculateCube(int value) { return value*value*value; }

int main() {
  const int SIZE = 10;
  int a1[] = {100, 2, 8, 1, 50, 3, 8, 8, 9, 10};
  vector<int> v(a1,a1+SIZE);
  ostream_iterator<int> output(cout, " ");

  cout << "Vector v: ";
  copy(v.begin(),v.end(),output);

  sort(v.begin(),v.end());
  cout << endl << "Vector v after sort: ";
  copy(v.begin(),v.end(),output);

  random_shuffle(v.begin(),v.end());
  cout << endl << "Vector v after random_shuffle: ";
  copy(v.begin(),v.end(),output);

  int result = 0;
  count(v.begin(), v.end(), 8, result);
  cout << endl << "Number of elements matching 8: " << result;

  count_if(v.begin(), v.end(), greater9, result);
  cout << endl << "Number of elements greater than 9: " << result;

  cout << endl << "Minimum element in Vector v is: "
       << *(min_element(v.begin(), v.end()));

  cout << endl << "Maximum element in Vector v is: "
       << *(max_element(v.begin(), v.end()));

  cout << endl << "The total of the elements in Vector v is: "
       << accumulate(v.begin(), v.end(), 0);

  cout << endl << "The square of every integer in Vector v is: " << endl;
  for_each(v.begin(), v.end(), outputSquare);

  vector<int> cubes(SIZE);
  transform(v.begin(), v.end(), cubes.begin(), calculateCube);
  cout << endl << "The cube of every integer in Vector v is: " << endl;
  copy(cubes.begin(), cubes.end(), output);

  cout << endl;
  return 0;
}

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: 9 50 100 3 2 8 8 10 1 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:
81 2500 10000 9 4 64 64 100 1 64 
The cube of every integer in Vector v is:
729 125000 1000000 27 8 512 512 1000 1 512

Adam Jonathan Griff, computer@griffmonster.com
Copyright © University of Colorado. All rights reserved.
Revised: April 2, 1998