Raise all values in a vector by a given percentage : vector subscript indexer « Vector « C++






Raise all values in a vector by a given percentage

  
#include <iostream>
#include <vector>

using namespace std;

void raise_by_percent(vector<double>& v, double p)
{  
   for (int i = 0; i < v.size(); i++)
      v[i] = v[i] * (1 + p / 100);
}

int main()
{  
   vector<double> salaries(5);
   salaries[0] = 3.0;
   salaries[1] = 6.0;
   salaries[2] = 4.0;
   salaries[3] = 7.0;
   salaries[4] = 5.0;

   raise_by_percent(salaries, 4.5);

   for (int i = 0; i < salaries.size(); i++)
      cout << salaries[i] << "\n";

   return 0;
}
  
    
  








Related examples in the same category

1.Use indexer to add elements to a vector
2.Use indexer to reference elements in a vector
3.Loop thourgh all elements in a vector using <
4.Loop through all elements in a vector using operator [] instead of operator *
5.Create another vector that contains a subrange of vector.
6.Read double from keyboard, save it to a vector and find the max value