Summer 2003: Algorithm Library Design

Homework 4: Tuesday, 27.05.03

Exercise due Thursday , 05.06.03, 4pm (before class)
Submit homeworks to Roman Dementiev <dementiev@mpi-sb.mpg.de>. Source code only, no executables!

Project update due Tuesday, 10.06.03
Submit to the supervisor of your project.


Exercise 4 (10 Pts)

The following call should find the first element in container c that is greater than 3:
std::find(c.begin(), c.end(), _1 > 3);
To make this work, the expression _1 > 3 must create a Predicate that returns true if its argument is greater than 3. This is achieved by defining the placeholder variable _1 (a placeholder for the argument of the predicate) and its operator> appropriately.

Implement the placeholder variables _1 and _2 to make the following test program (in placeholder.C) to work. Note that the expression _1 > _2 creates a Binary Predicate.

int main() {
  const int size = 6;
  int a[size] = { 2, 5, 1, 3, 6, 4 };
  float b[size] = { 4.0, 6.5, 4.5, 1.0, 5.0, 2.5 };
  std::ostream_iterator<int> int_out_iter(std::cout, " ");
  std::ostream_iterator<float> float_out_iter(std::cout, " ");

  std::cout << "Part (a): ";
  std::remove_copy_if(a, a+size, int_out_iter, _1 > 3);
  std::cout << "       (should be: 2 1 3)" << std::endl;

  std::cout << "Part (b): ";
  std::remove_copy_if(b, b+size, float_out_iter, _1 > 4.0);
  std::cout << "     (should be: 4 1 2.5)" << std::endl;

  std::cout << "Part (c): ";
  std::remove_copy_if(b, b+size, float_out_iter, _1 > 4);
  std::cout << "     (should be: 4 1 2.5)" << std::endl;

  std::cout << "Part (d): ";
  std::sort(a, a+size, _1 > _2);
  std::copy(a, a+size, int_out_iter);
  std::cout << " (should be: 6 5 4 3 2 1)" << std::endl;
}
The placeholders should work with any type, not just with float and int.

Project update

due Tuesday, 10.06.03

Update your annotated feature diagram according to the feedback from your presentation. Scope your feature diagram for implementation, that is, decide what you are going to implement and what not. Return a report with the annotated feature diagram including the scoping information to your project supervisor. You may also include other additional design details.


Lutz Kettner (<surname>@mpi-inf.mpg.de). Last modified on Monday, 09-Jun-2003 00:55:15 MEST.