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.
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
@@coding::1@@
/* 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