C program to find the minimum scalar product (dot product) of given two vectors. A scalar product or dot product is the product of the given two equal length vectors. To find the scalar product, we require the number of elements or sizes of the two arrays representing the vectors and the data values of the arrays (or vectors).
The resultant output is an integer denoting the minimum possible scalar product value for the given two vectors (or arrays).
The first step after understanding the problem statement is always, gathering the necessary inputs. To read our required inputs at runtime, we can make use of predefined scanf() function in C. This reads input of any primitive datatype at runtime directly from the keyboard.
Based on the format specifier we mention in the function, the datatype of the input is determined. Here, we require all integer inputs only therefore, we use the ‘%d’ format specifier. We first read the size of the two arrays (n) and create two arrays (vector1, vector2) of the same size. We then individually read data values of each array by iterating in loop from beginning to end.
printf(“\nEnter number of elements in two vectors:”);
scanf(“%d”,&n);
int vector1[n], vector2[n];
printf(“\nEnter %d elements in 1st vector:\n”,n);
for(i = 0; i < n ; i++) {
scanf(“%d”,&vector1[i]);
}
printf(“\nEnter %d elements in 2nd vector:\n”,n);
for(i = 0; i < n ; i++) {
scanf(“%d”,&vector2[i]);
}
If we have to find the minimum product then, the way is to multiply the maximum number with minimum number. Only this possibility will result in finding the minimum product. For this, we will have to sort one array (vector1) in ascending order while, we sort the other (vector2) in descending order.
For ascending order, we’ll call the user-defined function (sort_asc) and pass the input array vector1 along with its size as parameters and for descending order we’ll call the user-defied function (sort_des) and pass the other input array vector2 and the size as parameters.
sort_asc(vector1, n);
sort_des(vector2, n);
In the sort_asc() function, we traverse from 0th index (i=0) to (n-1)th index. For every index value, we’ll check whether there is a value between 0th index (j=0) to (n-i-1)th index that, if there is jth index greater than (j+1)th index.
If there exists such value then, we swap the two using temporary variable (temp). We first assign a[j] value to temp then, update a[j] value to a[j+1]. Later, we update a[j] value with temp (originally a[j] value). This way both the values at jth and (j+1)th positions are swapped. This way at the end of first iteration, the largest element value is at the end. At the end of second iteration, the second largest value is at second from last position and so on. By the completion of all iterations, we’ll have the array sorted in ascending order.
void sort_asc(int a[], int n) {
int i,j,temp;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (a[j] > a[j+1]) {
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#include<stdio.h> void sort_asc(int[], int); void sort_des(int[], int); int main() { int n,i,sum=0; printf("\nEnter number of elements in two vectors:"); scanf("%d",&n); int vector1[n], vector2[n]; printf("\nEnter %d elements in 1st vector:\n",n); for(i = 0; i < n ; i++) { scanf("%d",&vector1[i]); } printf("\nEnter %d elements in 2nd vector:\n",n); for(i = 0; i < n ; i++) { scanf("%d",&vector2[i]); } sort_asc(vector1, n); sort_des(vector2, n); printf("\nThe minimum scalar product of two vectors is : "); for(i = 0; i < n ; i++) { sum = sum + (vector1[i] * vector2[i]); } printf("%d",sum); return 0; } void sort_asc(int a[], int n) { int i,j,temp; for (i = 0; i < n-1; i++) for (j = 0; j < n-i-1; j++) if (a[j] > a[j+1]) { int temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } void sort_des(int a[], int n) { int i,j,temp; for (i = 0; i < n-1; ++i) { for (j = i + 1; j < n; ++j) { if (a[i] < a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Enter number of elements in two vectors:3 Enter 3 elements in 1st vector: 1 2 3 Enter 3 elements in 2nd vector: 1 2 3 The minimum scalar product of two vectors is : 10 |