Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)
C Program : Maximum Scalar Product of Two Vectors
Latest :

C Program : Maximum Scalar Product of Two Vectors

Write a C program to find the maximum scalar product of the given two vectors. For this, we will represent vectors in the form of arrays and the inputs necessary are size of the two arrays and the data values or elements of the two arrays.

The resultant output is an integer representing the largest or maximum possible scalar product or dot product of the given two input arrays.

The first step to follow would be gathering the required inputs at runtime. To do so, in C language we have a predefined input function called scanf() which reads primitive datatype of any input at runtime from the keyboard.

Based on the format specifier we use, the datatype of the input will be decided. Here, all the inputs are of integer datatype so, we’ll use the ‘%d’ format specifier. We will first read the number or elements or size of the array (n). Then, we’ll create two arrays of the same size and read the data values of the two arrays by iterating and reading them individually.

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]);

}

To find the maximum scalar product in C, we’ll have to multiply the minimum value with the corresponding minimum value whereas maximum value with the maximum value. To do so, we’ll have to sort the two arrays first so, we call a user-defined function (sort) and pass the array along with its size as parameters. This function will sort the array passed as parameter in ascending order.

sort(vector1, n);

sort(vector2, n);

In the sort() function, we iteration from beginning (i=0) to the last but one index (n-2) and for every iteration we again iterate from starting (j=0) to the (n-i-1) index and check whether there exists a jth index such that a[j]>a[j+1].

If yes then, we swap the two data values using temporary variable (temp). We first assign the a[j] value to temp and then, assign the a[j+1] to a[j]. Then, we assign the original value of a[j] i.e., temp to the a[j+1]. This way the two values are swapped.

Output:

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)
x

Check Also

C Program To Print Number Of Days In A Month | Java Tutoring

C program to input the month number and print the number of days in that ...