Program to find the maximum scalar product of two vectors | faceprep

Program to find the maximum scalar product of two vectors | faceprep

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

Program to find the maximum scalar product of two vectors


Sample Input 1:

3 (Number of elements of the array)

1 2 3 Array 1 elements)

4 5 6 (Array 2 elements)

Sample Output 1:

32

Calculation:

Maximum scalar product = 1 * 4 + 2 * 5 + 3 * 6

= 4 + 10 + 18

= 32


faceprep pro ad bannerClick here to learn more about FACE Prep PRO


Algorithm to find the maximum 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 descending 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 maximum scalar product of two vectors (dot product)

@@coding::1@@


face prep pro ad bannerClick here to learn more about FACE Prep PRO


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

/* C Program to find the maximum scalar product of two vectors (dot product) USING POINTERS*/n#include<stdio.h>n#include<stdlib.h>nint* createarray(int);nint getelements(int*,int);nint ascending(int*,int);nint maxscalar(int*,int*,int);nint i,j;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);nmaxscalar(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{nint temp;nfor(i=0;i<n;i++)n{nfor(j=0;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 maxscalar(int *a,int *b,int n)n{nint p,q,sum=0;nfor(i=0;i<n;i++)n{np=*(a+i);nq=*(b+i);nsum=sum+(p*q);n}nprintf(“%d”,sum);nreturn 0;n}n


Recommended Programs


Program to find the maximum scalar product of two vectors

c