Latest :

C Program : Minimum Scalar Product of Two Vectors | C Programs

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;

}

}

Output:

x

Check Also

C Program To Check A Number Is Negative, Positive Or Zero | C Programs

C program to check whether a number is negative, positive or zero – In this ...