# C++: Classes: Example: floatVectorTest.cpp

This is a main program to show how the floatVector class, which is a subset of the capabilities of the STL `vector`, can be used. To be a real test program, it should have many more tests -- the easy way is to use `assert`. See Example: floatVector for links to the header and implementation files.

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
    // floatVectorTest.cpp Test program for floatVector class.
    //  Fred Swartz 2001-11-13, 2002-09-11 Needs much more asserts.

    #include <iostream>
    #include <cassert>
    using namespace std;

    #include "floatVector.h"

    //============================================================= main
    int main() {

      floatVector v;

      assert(v.size()==0);
      v.push_back(1.0);   v.push_back(2.0);   v.push_back(3.0);
      v.push_back(4.0);   v.push_back(5.0);   v.push_back(6.0);
      v.push_back(7.0);   v.push_back(8.0);   v.push_back(9.0);
      v.push_back(10.0);  v.push_back(11.0);  v.push_back(12.0);
      assert(v.capacity() == 20);
      assert(v.size()==12);
      assert(v.at(3)==4.0);

      int n = v.size();
      if (n > 0) {  // only do this if there was some input.
          float sum = 0.0 ;
          for (int i=0; i<n; i++) {
             sum += v.at(i);
          }

          // Compute average (mean)
          float average = sum / n;

          // Make a new vector (no need, just to show constructor)
          floatVector bigger(n / 2);  // half is estimate, but can expand.

          // Find and copy all element larger than the average
          for (int j=0; j<n; j++) {
              float val = v.at(j);
              if (val > average) {
                  bigger.push_back(val);
              }
          }

          // Print all elements larger than the average
          cout << "Larger than average (" << average << ")" << endl;
          for (int k=0; k<bigger.size(); k++) {
              cout << "bigger[" << k << "] = " << bigger.at(k) << endl;
          }
      }
      return 0;
    }//end main