Demonstrate valarray: assign value, : Valarray « Data Structure « C++






Demonstrate valarray: assign value,

Demonstrate valarray: assign value,
#include <iostream>
#include <valarray>
#include <cmath>
using namespace std;

int main()
{
  valarray<int> valarrayObject(10);
  int i;

  for(i = 0; i <10; i++)
      valarrayObject[ i ] = i;

  cout << "Original contents: ";
  for(i = 0; i <10; i++)
    cout << valarrayObject[i] << " ";
  cout << endl;

  valarrayObject = valarrayObject.cshift(3);

  cout << "Shifted contents: ";
  for(i = 0; i <10; i++)
    cout << valarrayObject[i] << " ";
  cout << endl;

  valarray<bool> valarrayObject2 = valarrayObject < 5;
  cout << "Those elements less than 5: ";
  for(i = 0; i <10; i++)
    cout << valarrayObject2[i] << " ";
  cout << endl << endl;

  valarray<double> valarrayObject3(5);
  for(i = 0; i <5; i++) 
     valarrayObject3[i] = (double) i;

  cout << "Original contents: ";
  for(i = 0; i <5; i++)
    cout << valarrayObject3[i] << " ";
  cout << endl;

  valarrayObject3 = sqrt(valarrayObject3);

  cout << "Square roots: ";
  for(i = 0; i <5; i++)
    cout << valarrayObject3[i] << " ";
  cout << endl;

  valarrayObject3 = valarrayObject3 + valarrayObject3;
  cout << "Double the square roots: ";
  for(i = 0; i <5; i++)
    cout << valarrayObject3[i] << " ";
  cout << endl;

  valarrayObject3 = valarrayObject3 - 10.0;
  cout << "After subtracting 10 from each element:\n";
  for(i = 0; i <5; i++)
    cout << valarrayObject3[i] << " ";
  cout << endl;

  return 0;
}

           
       








Related examples in the same category

1.Demonstrate sliceDemonstrate slice
2.Demonstrate gslice() in valarrayDemonstrate gslice() in valarray