Write a program that calculates the scalar product of two integer vectors U and V (of the same dimension).
\((3\ \ 2\ -4) \times (2\ -3\ \ 5) = 3 \times 2 + 2\times(-3) + (-4)\times 5 = -20 \)
Difficulty level

Video recording
This exercise is mostly suitable for students
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define SIZE 10
void main()
{
int A1[SIZE], A2[SIZE];
int dim; // same dimension for both vectors
int i, j;
int sum = 0;
do {
printf("Enter the dimension: ");
scanf("%d", &dim);
} while (dim <= 0 || dim > SIZE);
printf("**Reading first vector**\n");
for (i = 0; i<dim; i++)
{
printf("Enter A1[%d]=", i);
scanf("%d", &A1[i]);
}
printf("**Reading second vector**\n");
for (i = 0; i<dim; i++)
{
printf("Enter A2[%d]=", i);
scanf("%d", &A2[i]);
}
printf("\n\n(");
for (i = 0; i<dim; i++)
{
printf("%d ", A1[i]);
}
printf(")*(");
for (i = 0; i<dim; i++)
{
printf("%d ", A2[i]);
}
printf(")=");
for (i = 0; i < dim; i++)
sum += A1[i] * A2[i];
printf("%d\n", sum);
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
