Maximum scalar product of two vectors

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
//order
void sortArrayAsc(int arr[]int n)
{
    for (int i = 0i < ni++)
    {
        for (int j = 0j < n - i - 1j++)
        {
            if (arr[j] > arr[j + 1])
            {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

int main()
{
    int arr1[] = {243};
    int arr2[] = {462};

    int n = sizeof(arr1) / sizeof(arr1[0]);
    sortArrayAsc(arr1n);

    sortArrayAsc(arr2n);

    int maximum = 0;
    for (int i = 0i < ni++)
    {
        maximum = maximum + arr1[i] * arr2[i];
    }

    printf("Maximum scalar product is: %d"maximum);
    return 0;
}


No comments:

Post a Comment