Write a program to find the maximum scalar product of two vectors.
Vectors are represented by arrays of the same size
Example:
Input: arr1[]={2,4,3}, arr2[]={4,6,2}
Output: Maximum scalar product is: 40
C Program
#include <stdio.h>//function to sort the array in ascending//ordervoid sortArrayAsc(int arr[], int n){for (int i = 0; i < n; i++){for (int j = 0; j < n - i - 1; j++){if (arr[j] > arr[j + 1]){int temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;}}}}int main(){int arr1[] = {2, 4, 3};int arr2[] = {4, 6, 2};int n = sizeof(arr1) / sizeof(arr1[0]);sortArrayAsc(arr1, n);sortArrayAsc(arr2, n);int maximum = 0;for (int i = 0; i < n; i++){maximum = maximum + arr1[i] * arr2[i];}printf("Maximum scalar product is: %d", maximum);return 0;}
No comments:
Post a Comment