# C++ program to find maximum scalar product of two vectors

## Maximum scalar product of two vectors

In this section we will learn about the maximum product of two vectors in an array.

There we have two arrays array 1 and array 2 so here we need to find dot products of two arrays. Dot product is also known as the scalar product of two vectors.

For finding maximum scalar product of two vectors in an array. We have to multiply minimum value of array1 to the minimum value of array 2 and add all the multiplied values.

### Example

Array 1- 1 2 3 4

Array 2- 2 3 4 5

scalar product= 1*2+2*3+3*4+4*5 = 2+6+12+20 = 40

## Algorithm:-

1. Input the number of elements of the arrays.
2. Input the array 1 and array 2 elements.
3. Initialize sum = 0.
4. Sort the array 1 in ascending order.
5. Sort the array 2 in ascending order.
6. Repeat from i = 1 to n
7. sum = sum + (arr1[i] * arr2[j])
8. Return sum.

## C++ Code:-

#include<iostream>
using namespace std;
void sort(int arr1[],int n)
{
int temp=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(arr1[i]<arr1[j])
{
temp=arr1[i];

arr1[i]=arr1[j];

arr1[j]=temp;
}

}
}
}

void main(String[] args
{
int*a1=new int[50];

int*a2=new int[50];

cout<<\nenter size of an array”;
int size;
cin>>size;
cout<<\nenter elements of an array1″;
for(int i=0;i<size;i++)
cin>>a1[i];
cout<<\nenter elements of an array2″;
cin>>a2[i];
sort(a1,size);
sort(a2,size);
int sum = 0;
for(i = 0; i < size ; i++)
sum = sum + (a1[i] * a2[i]);
cout<<\nthe maximum scalar product of two vector is “<<sum;
}

OUTPUT
Enter size of array 1
4
Enter elements of array 1
2
3
5
7
Enter size of array
4
Enter elements of array 2
1
2
2
5
maximum scalar product of two vectors in arraya
53