Program to find the minimum scalar product of two vectors (dot product) | faceprep

Program to find the minimum scalar product of two vectors (dot product) | faceprep

The program to find the minimum scalar product of two vectors (dot product) is discussed here. Given two arrays, find the minimum scalar product of all permutations of the two given arrays.

Program to find the minimum scalar product of two vectors (dot product)


Sample Input 1:

3 (Number of elements of the array)

1 3 5 (Array 1 elements)

2 4 1 (Array 2 elements)

Sample Output 1:

15

Calculation:

Minimum scalar product = 1 * 4 + 3 * 2 + 5 * 1

= 4 + 6 + 5

= 15

Sample Input 2:

5

1 2 3 4 5

1 0 1 0 1

Sample Output 2:

6

Calculation:

Minimum scalar product = 5 * 0 + 4 * 0 + 3 * 1 + 2 * 1 + 1 * 1

= 0 + 0 + 3 + 2 + 1

= 6


program to find the minimum scalar productsClick here to learn more about FACE Prep PRO


Algorithm to find the minimum scalar product of two vectors

  • Input the number of elements of the arrays.
  • Input the array 1 and array 2 elements.
  • Initialize sum = 0.
  • Sort the array 1 in ascending order.
  • Sort the array 2 in descending order.
  • Repeat from i = 1 to n
  • sum = sum + (arr1[i] * arr2[i])
  • Return sum.

Program to find the minimum scalar product of two vectors (dot product)

@@coding::1@@


face prep pro adClick here to learn more about FACE Prep PRO


Program to find the minimum scalar product of two vectors (dot product) using pointers

// C Program to find the minimum scalar product of two vectors (dot product) USING POINTERSn#include<stdio.h>n#include<stdlib.h>nint i,j,temp;nint* createarray(int);nint getelements(int*,int);nint ascending(int*,int);nint minscalar(int*,int*,int);nint main()n{nint n,*a,*b;nscanf(“%d”,&n);na=createarray(n);ngetelements(a,n);nb=createarray(n);ngetelements(b,n);nascending(a,n);nascending(b,n);nminscalar(a,b,n);nreturn 0;n}nint* createarray(int n)n{nint *a;na=(int*)malloc(n*sizeof(int));nreturn a;n}nint getelements(int *a,int n)n{nfor(i=0;i<n;i++)nscanf(“%d”,a+i);nreturn 0;n}nint ascending(int *a,int n)n{nfor(i=0;i<n;i++)n{nfor(j=i+1;j<n;j++)n{nif(*(a+i)>*(a+j))n{ntemp=*(a+i);n*(a+i)=*(a+j);n*(a+j)=temp;n}n}n}nreturn 0;n}nint minscalar(int *a,int *b,int n)n{nint sum=0,p,q;nfor(i=0;i<n;i++)n{np=*(a+i);nq=*(b+n-1-i);nsum=sum+(p*q);n}nnprintf(“%d”,sum);nreturn 0;n}n


Recommended Programs


Program to find the minimum scalar product of two vectors (dot product)