Huge Sell on Popular Electronics
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <iostream>
#include <valarray>
using namespace std;
// print valarray line-by-line
template<class T>
void printValarray (const valarray<T>& va, int num)
{
for (int i=0; i<va.size()/num; ++i) {
for (int j=0; j<num; ++j) {
cout << va[i*num+j] << ' ';
}
cout << endl;
}
cout << endl;
}
int main()
{
/* valarray with 12 elements
* - four rows
* - three columns
*/
valarray<double> va(12);
// fill valarray with values
for (int i=0; i<12; i++) {
va[i] = i;
}
printValarray (va, 3);
// assign 77 to all values that are less than 5
va[va<5.0] = 77.0;
// add 100 to all values that are greater than 5 and less than 9
va[va>5.0 && va<9.0] = valarray<double>(va[va>5.0 && va<9.0]) + 100.0;
printValarray (va, 3);
}
/*
0 1 2
3 4 5
6 7 8
9 10 11
77 77 77
77 77 5
106 107 108
9 10 11
*/
Huge Sell on Popular Electronics
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <iostream>
#include <valarray>
using namespace std;
// print valarray
template <class T>
void printValarray (const valarray<T>& va)
{
for (int i=0; i<va.size(); i++) {
cout << va[i] << ' ';
}
cout << endl;
}
int main()
{
// define two valarrays with ten elements
valarray<double> va1(10), va2(10);
// assign values 0.0, 1.1, up to 9.9 to the first valarray
for (int i=0; i<10; i++) {
va1[i] = i * 1.1;
}
// assign -1 to all elements of the second valarray
va2 = -1;
// print both valarrays
printValarray(va1);
printValarray(va2);
// print minimum, maximum, and sum of the first valarray
cout << "min(): " << va1.min() << endl;
cout << "max(): " << va1.max() << endl;
cout << "sum(): " << va1.sum() << endl;
// assign values of the first to the second valarray
va2 = va1;
// remove all elements of the first valarray
va1.resize(0);
// print both valarrays again
printValarray(va1);
printValarray(va2);
}
/*
0 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
min(): 0
max(): 9.9
sum(): 49.5
0 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9
*/
Machine Learning, Big Data, Data Science, Analytics, Cloud, Security, AI, Robotics, Database, BI, Development: Software, Web, Mobile