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