Question

in c++ Write a program which can find the dot product of two vectors of the...

in c++ Write a program which can find the dot product of two vectors of the same length ?. The user will
enter the length ?. Use the length to control how many times you loop. The result is a scalar value
and not a vector. If the dot product is zero, then the two vectors are perpendicular.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

int main()
{
//declare variable
int length;
float A[10], B[10], dotProduct = 0.0;
  
//display message
printf("Vector Dot Product Calculator.");
printf("\n\nPlease enter the length of the vectors (max 10): ");
scanf("%d", &length);
  
//display message
printf("\nEnter vector A: ");
for(int i=0; i<length; i++)
scanf("%f", &A[i]);
  
//display message
printf("\nEnter vector A: ");
for(int i=0; i<length; i++)
scanf("%f", &B[i]);
  
for(int i=0; i<length; i++)
{
dotProduct = dotProduct + A[i] * B[i];
}
  
printf("\nA = [ ");
for(int i=0; i<length; i++)
{
printf("%.1f ", A[i]);
}
printf("]");
  
printf("\nB = [ ");
for(int i=0; i<length; i++)
{
printf("%.1f ", B[i]);
}
printf("]");
  
//display dot product
printf("\n\nDot Product = %.1f", dotProduct);
  
if(dotProduct==0.0)
{
//display message
printf("\n\nThese vectors are perpendicular.");
}
else
{
//display message
printf("\n\nThese vectors are not perpendicular.");
}
  
return 0;
}

Add a comment
Know the answer?
Add Answer to:
in c++ Write a program which can find the dot product of two vectors of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT