C语言编写一个函数vec_dot,将两个数组和向量的长度作为参数传入函数,返回两个向量点积的结果。注意两个向量需要用户输入
时间: 2024-05-09 22:17:51 浏览: 99
Sure, I can handle that! Here's an example C language code to accomplish that:
```
#include <stdio.h>
float vec_dot(float* vec1, float* vec2, int length) {
float dot_product = 0;
for(int i = 0; i < length; i++) {
dot_product += vec1[i] * vec2[i];
}
return dot_product;
}
int main() {
int length;
printf("Enter the length of vectors: ");
scanf("%d", &length);
float vec1[length], vec2[length];
printf("Enter the values for the first vector:\n");
for(int i = 0; i < length; i++) {
scanf("%f", &vec1[i]);
}
printf("Enter the values for the second vector:\n");
for(int i = 0; i < length; i++) {
scanf("%f", &vec2[i]);
}
printf("The dot product of the two vectors is: %f\n", vec_dot(vec1, vec2, length));
return 0;
}
```
Hope this helps you! Do you have any other questions?
阅读全文