Calculates the dot product of two vectors. - C Array

C examples for Array:Array Value

Description

Calculates the dot product of two vectors.

Demo Code

#include <stdio.h>

int main(void)
{
   int i;/* w  w w. j a  v a 2  s  . com*/
   const int length = 2;
   int dotProduct;

   int vector1[length];

   printf("Enter values for vector 1: ");

   for (i = 0; i < length; ++i)
      scanf("%d", &vector1[i]);

   int vector2[length];

   printf("Enter values for vector 2: ");

   for (i = 0; i < length; ++i)
      scanf("%d", &vector2[i]);

   dotProduct = 0;

   for (i = 0; i < length; i++)
      dotProduct += vector1[i] * vector2[i];

   printf("The dot product of the two vectors is %d", dotProduct);

   return 0;
}

Result


Related Tutorials