【发布时间】:2020-12-16 13:53:02
【问题描述】:
- 我必须创建一个包含 x、y 和 z 的结构 vector3d
- 然后我必须创建 struct vector 3d 类型的两个变量并在其中存储两个向量
- 接下来,我必须编写一个函数来计算这两个向量的点积和叉积。需要哪种返回类型?
这就是我到目前为止所拥有的。也许有人可以帮助我。
#include <stdio.h>
#include <stdlib.h>
int n = 3;
struct vector3d
{
int x, y, z;
};
int dot_product (int v1[], int v2[], int n)
{
int dproduct = 0;
n = 3;
for(int i = 0; i < n; i++)
dproduct += v1[i] * v2[i];
return dproduct;
}
void cross_product (int v1[], int v2[], int crossproduct[])
{
crossproduct[0] = v1[1] * v2[2] - v1[2] * v2[1];
crossproduct[1] = v1[0] * v2[2] - v1[2] * v2[0];
crossproduct[2] = v1[0] * v2[1] - v1[1] * v2[0];
}
int main()
{
struct vector3d v1 = {0,0,0};
struct vector3d v2 = {0,0,0};
printf("Vector 1 - Enter value for x: ");
scanf("%d", &v1.x);
printf("Vector 1 - Enter value for y: ");
scanf("%d", &v1.y);
printf("Vector 1 - Enter value for z: ");
scanf("%d", &v1.z);
printf("Vector 2 - Enter value for x: ");
scanf("%d", &v2.x);
printf("Vector 2 - Enter value for y: ");
scanf("%d", &v2.y);
printf("Vector 2 - Enter value for z: ");
scanf("%d", &v2.z);
}
【问题讨论】:
-
您的函数对
int的数组进行操作,但您希望它们对您的vector3d结构进行操作。
标签: c vector struct dot-product cross-product