C++ Multiply Vector by Scalar

In this article, we will discuss how to Multiply vector by a Scalar in C++.

Multiplying Vector by a Scalar value means multiplying each element of the vector by the same constant value.

Example,

Input
Vector = { 1 , 3 , 4 , 5 }
Scalar = 4

Output
Vector = { 4 , 12 , 16 , 20 } // Multiplying each element by Scalar

We can perform vector scalar multiplication in many ways. We will discuss some of them.

Method To Multiply Vector by Scalar

For this tutorial, assume v be the vector and k be the scalar value we want to multiply.

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 store/return 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 multiplies the vector element by k and returns the result.
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

void MultiplyVectorByScalar(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 = {1 , 3 , 4 , 5};
	int k = 4;
	cout<<"Scalar : "<<k<<endl;
	cout<<"Vector : ";
	printVector(v);
	
	MultiplyVectorByScalar(v , k);
	
	cout<<"Vector After Scaler Multiplication by "<<k<<" : ";
	printVector(v);

}

Output

Multiply Vector by Scalar using std::transform

Method 2: Using for_each

for_each(v.begin(), v.end(), [k](int &c){ c *= k; });
  1. std::for_each applies the lambda function to each element in the range [ v.begin(), v.end() ).
  2. We pass k as an argument to the lambda function. The lambda function multiplies each element of the vector by k.
  3. Since we are passing each element of the vector by reference to the lambda function, the result is reflected back in the original vector.
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

void MultiplyVectorByScalar(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 = {2 , 7 , 4 , 5};
	int k = 3;
	
	cout<<"Scalar : "<<k<<endl;
	cout<<"Vector : ";
	printVector(v);
	
	MultiplyVectorByScalar(v , k);
	
	cout<<"Vector After Scaler Multiplication by "<<k<<" : ";
	printVector(v);

}

Output

Multiply Vector by Scalar using std::for_each

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 MultiplyVectorByScalar(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 = {2 , 1 , 3 , 10};
	int k = 5;
	
	cout<<"Scalar : "<<k<<endl;
	cout<<"Vector : ";
	printVector(v);
	
	MultiplyVectorByScalar(v , k);
	
	cout<<"Vector After Scalar Multiplication by "<<k<<" : ";
	printVector(v);

}

Output

c++ vector scalar multiplication

What to Study Next?

  1. Concatenate Vectors in C++
  2. C++ Merge Sort using Vector
  3. Initialize Array with all 0 in Javascript
  4. Initialize Array with all 0 in C#

Leave a Comment

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