【问题标题】:Dot Product and Cross Product in C using structuresC中使用结构的点积和叉积
【发布时间】: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


【解决方案1】:

您不能使用int[] 代替vector3d。您可以传递您的向量结构并使用它来执行您的任务。这段代码我已经写好了,你可以根据自己的需要修改。

#include <stdio.h>
#include <stdlib.h>

int n = 3;

typedef struct vector3d
{
    int x, y, z;
} vector3d;

int dot_product(vector3d v1, vector3d v2)
{
    int dproduct = 0;

    dproduct += v1.x * v2.x;
    dproduct += v1.y * v2.y;
    dproduct += v1.z * v2.z;
    return dproduct;
}

vector3d cross_product(vector3d v1, vector3d v2)
{
    vector3d crossproduct = {0, 0, 0};
    crossproduct.x = v1.y * v2.z - v1.z * v2.y;
    crossproduct.y = v1.x * v2.z - v1.z * v2.x;
    crossproduct.z = v1.x * v2.y - v1.y * v2.x;
    return crossproduct;
}

int main()
{
    vector3d v1 = {0, 0, 0};
    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);

    printf("Dotproduct: %d\n", dot_product(v1, v2));
    vector3d cp = cross_product(v1, v2);
    printf("Crossproduct: x:%d y:%d z:%d", cp.x, cp.y, cp.z);

    return 0;
}

//OUTPUT
Vector 1 - Enter value for x: 1
Vector 1 - Enter value for y: 2
Vector 1 - Enter value for z: 3
Vector 2 - Enter value for x: 3
Vector 2 - Enter value for y: 2
Vector 2 - Enter value for z: 1
Dotproduct: 10
Crossproduct: x:-4 y:-8 z:-4 

以后试着自己想想这些小事。

【讨论】:

    【解决方案2】:

    您使用typedef 创建结构的别名,并在矢量分析函数中使用该结构 (Passing struct to function)。要访问结构的字段,请使用 . 表示法。还有另一种可能将结构作为指向结构的指针传递给函数,在这种情况下,您使用-&gt; 表示法来访问字段(Passing pointers/references to structs into functionshttps://www.tutorialspoint.com/cprogramming/c_structures.htm

    typedef struct 
    {
        int x;
        int y;
        int z;
    } vector3d;
    
    int dot_product (vector3d v1, vector3d v2)
    {
       return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
    }
    

    在此链接中,使用复数而不是 3D 向量(近似为“2D 向量”),您可以修改:https://www.programiz.com/c-programming/c-structure-function

    【讨论】:

      猜你喜欢
      • 2012-07-30
      • 1970-01-01
      • 2018-07-15
      • 1970-01-01
      • 1970-01-01
      • 2015-03-31
      • 2021-05-18
      • 2014-12-06
      • 1970-01-01
      相关资源
      最近更新 更多