cvectorstructdot-productcross-product# Dot Product and Cross Product in C using structures

- I have to create a struct vector3d that includes x,y, and z
- Then I have to create two variables of the type struct vector 3d and store two vectors in them
- Next, I have to write a function that calculates the dot and cross product of these two vectors. Which return type is necessary?

That's what I have until now. Maybe someone could help me.

```
#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);
}
```

Solution

You can't use `int[]`

in the place of `vector3d`

. You can pass your vector struct and use it to perform your tasks. I have written this code, you can modify it with your needs.

```
#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
```

In the future try to think of these small things by your self.

- Passing the function *cmp(const *void, const *void) as a parameter in C and using it to create a queue?
- Is it safe to use `strstr` to search for multibyte UTF-8 characters in a string?
- Why am I getting a heap-use-after-free error?
- Catch and compute overflow during multiplication of two large integers
- Access a global static variable from another file in C
- "this" pointer in C (not C++)
- Unknown Output of C code
- Yocto- gcc cannot find shared library
- Artifact in one of the triangles in basic OpenGL texture quadrilateral
- How to check if a pointer is valid?
- cJSON Key-Value Parsing
- hi, I am emplementing LZW here and it work great, the problem is that I cant scan and encode a string if it have a (space)
- pthread clean up int/array/struct type variables using pthread_cleanup_push
- Standard way of getting a bit pattern of all ones
- Why doesn't the use of strictly conforming programs and conforming implementations ensure absolute portability?
- Checking emptiness of X macros
- Is it possible to run WebAssembly code async?
- C suddenly messing up values in 2d array
- Socket opening macOS assembly
- GDB outputs arm mnemonics
- What kind of optimization does const offer in C/C++?
- wcstombs segmentation fault
- Fast implementation of complementary error function with 5-digit accuracy
- Ascii to binary calculation is flawed
- what stops GCC __restrict__ qualifier from working
- Why my sig_int() function can't prevent my function from exit in c?
- Allocating user space memory in Linux Kernel
- Determining realloc() behaviour before calling it
- Is it legal to cast a struct to an integer if it has no padding?
- C macro, something weird