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> int main() { // Declare the double precision vector V: RWMathVec<double> V; // Read the vector from standard input: std::cin >> V; /* * Output the length of the vector. * This illustrates the use of an RWMathVec<double> * member function. */ std::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); std::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); std::cout << "The maximum value of the vector is:\t" << V(maxV) << "\n"; // Output the variance of the vector: std::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)); std::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 © Rogue Wave Software, Inc. All Rights Reserved.
The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.