C++ program to find minimum scalar of two vectors

Minimum scalar product of two vector

In this section we learn about the minimum scalar product of two array.

There are we have a two array That is array 1 and array 2  so here we need to find the dot product of the two array. Dot product is also known as the scalar product of two vector.

For finding the minimum scalar product of arrays we need to multiply the minimum value of first array to the maximum value of the second array and add these corresponding products to result.

Algorithm:-

  1. Initialize two array.
  2. Take the element for both array,
  3. Sort the both array by calling sort method.
  4. print the minimum scalar product by calling MScalarProduct().
  5. MScalarProduct(a1,a2,n)
    1. initialize sum variable.
    2. initialize for loop from 0 to size of an array.
    3. multiply array same index element and add this with sum and store it in same variable.
    4. return the sum;
  6. sort(a1,a2,n)
    1. sort the array1 in ascending order.
    2. sort the array2 in descending order.

C++ Code:-


     #include<iostream>
     using namespace std;
     int MScalarProduct(int arr1[],int arr2[],int n)
     {
          int sum=0
          for(int i=0;i<n;i++)
          {
               sum=sum+arr1[i]*arr2[i];
          }
          return sum;  

     }
     void sort(int arr1[],int arr2[],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;
                    }

               }
               for(int j=0;j<n;j++)
               {                  
                    if(arr2[i]>arr2[j])
                    {

                         temp=arr2[i];

                         arr2[i]=arr2[j];

                         arr2[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″;
     for(int i=0;i<size;i++)
     {
          cin>>a2[i];
          }
          sort(a1,a2,size);

     cout<<\nthe minimum scalar product of two vector is “<<MScalarProduct(a1,a2,size);
     }


    
    output
    Enter size of array 1
    4
    Enter elements of array 1
    2
    3
    5
    7
    Enter size of array 2
    4
    Enter element of array 2
    5
    2
    1
    1
    Minimum scalar of product of two vectors
    28