FILENAME: example1.cpp
Program:
/*
* Example program using class RWMathVec<double> * to do simple statistics with double precision vectors. */ // Include the RWMathVec<double> class header file. #include <rw/math/mathvec.h>
#include <iostream.h>
main() { // Declare the double precision vector V: RWMathVec<double> V; // Read the vector from standard input: cin >> V;
/* * Output the length of the vector. * This illustrates the use of an RWMathVec<double> * member function. */
cout << "The length of the vector is:\t" << V.length() << "\n"; /* * Calculate the mean of the vector. * This illustrates the use * of the global function mean(). */ double avg = mean(V); cout << "The mean of the vector is:\t" << avg << "\n"; /* * Find the index of the maximum element of * the vector and output the value. */ int maxV = maxIndex(V); cout << "The maximum value of the vector is:\t" << V(maxV) << "\n"; // Output the variance of the vector:
cout << "The variance of the vector is:\t" << variance(V) << "\n";
/* * Here is how to demean and normalize a * vector and print out the result. */ V -= mean(V); // Remove the mean maxV = maxIndex(V); // Max element if ( V(maxV) != 0) { V /= fabs(V(maxV)); cout << "The normalized demeaned vector is:\n" << V; } }
Sample Input:
[ 1.33 -23.7 -.23 16.46 9.5 3.14 11.3 -1. 24.23. 12.3 ]
Sample Output:
The length of the vector is: 10 The mean of the vector is: 5.333 The maximum value of the vector is: 24.23 The variance of the vector is: 151.721701 The normalized demeaned vector is: [ -0.211833 -1.536381 -0.294385 0.588824 0.220511 -0.11605 0.315764 -0.335133 1 0.3686831 ]>
©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.