C++ Divide vector by Scalar

In this article, we will discuss how to divide vector by a scalar quantity in C++.

Example,

Input
Vector = { 4 , 4 , 20 , 80 }
Scalar = 4

Output
Vector = { 1 , 1 , 5 , 20 } // dividing each element by the scalar

For this tutorial, assume v be a vector of integers and k be a scalar value.

Method 1: Using std::transform

transform(v.begin(), v.end(), v.begin(), [k](int &c){ return c/k; });
  1. We use std::transform to perform an operation on each element of the vector.
  2. The first 2 parameters, v.begin() and v.end() tells std::transform that we need to perform operations on each elements in range [ v.begin(), v.end() ).
  3. The third parameter is the initial iterator where we want to store the values after modification.
  4. The last parameter is the lambda function. The functions accept scalar k as a parameter. The function runs for each element of the vector. The function divides the vector element by k and returns the result.
  #include<iostream>
  #include<vector>
  #include<algorithm>
  using namespace std;
   
  void DivideVectorByScalar(vector<int> &v, int k){
     transform(v.begin(), v.end(), v.begin(), [k](int &c){ return c/k; });
  }
   
  void printVector(vector<int> v){
     for(int i=0;i<v.size();++i)
         cout<<v[i]<<' ';
     cout<<endl<<endl;
  }
   
  int main() {
   
     vector<int> v = {4 , 4 , 20 , 80};
     int k = 4;
     cout<<"Scalar : "<<k<<endl;
     cout<<"Vector : ";
     printVector(v);
     
     DivideVectorByScalar(v , k);
     
     cout<<"Vector After Division by "<<k<<" : ";
     printVector(v);
   
  } 

Output

Scalar : 4
Vector : 4 4 20 80
Vector After Division by 4 : 1 1 5 20

Method 2: Using for_each

for_each(v.begin(), v.end(), [k](int &c){ c /= k; });
  1. std::for_each calls the lambda function for each element in the range [ v.begin(), v.end() ).
  2. We pass k as an argument to the lambda function. The lambda function divides each element of the vector by k.
  3. Since we are passing the reference of each element of the vector to the lambda function, the change is reflected back in the original vector.
 #include<iostream>
 #include<vector>
 #include<algorithm>
 using namespace std;
  
 void DivideVectorByScalar(vector<int> &v, int k){
     for_each(v.begin(), v.end(), [k](int &c){ c /= k; });
 }
  
 void printVector(vector<int> v){
    for(int i=0;i<v.size();++i)
        cout<<v[i]<<' ';
    cout<<endl<<endl;
 }
  
 int main() {
  
    vector<int> v = {40 , 8 , 20 , 80};
    int k = 4;
    cout<<"Scalar : "<<k<<endl;
    cout<<"Vector : ";
    printVector(v);
    
    DivideVectorByScalar(v , k);
    
    cout<<"Vector After Division by "<<k<<" : ";
    printVector(v);
  
 } 

Output

Scalar : 4
Vector : 40 8 20 80
Vector After Division by 4 : 10 2 5 20

Method 3: Loop

for(int i=0;i<v.size();++i)
	v[i] = v[i] / k;

This is the most basic way to multiply vector by a scalar.

 #include<iostream>
 #include<vector>
 #include<algorithm>
 using namespace std;
  
 void DivideVectorByScalar(vector<int> &v, int k){
    for(int i=0;i<v.size();++i)
        v[i] = v[i] / k;
 }
  
 void printVector(vector<int> v){
    for(int i=0;i<v.size();++i)
        cout<<v[i]<<' ';
    cout<<endl<<endl;
 }
  
 int main() {
  
    vector<int> v = {40 , 8 , 12 , 20};
    int k = 4;
    cout<<"Scalar : "<<k<<endl;
    cout<<"Vector : ";
    printVector(v);
    
    DivideVectorByScalar(v , k);
    
    cout<<"Vector After Division by "<<k<<" : ";
    printVector(v);
  
 } 

Output

Scalar : 4
Vector : 40 8 12 20
Vector After Division by 4 : 10 2 3 5

What to Study Next?

  1. Concatenate Vectors in C++
  2. C++ Merge Sort using Vector

Leave a Comment

Your email address will not be published. Required fields are marked *