/ Published in: C
Output:
Vector A:
x: 1.000000
y: 2.000000
z: 3.000000
Vector B:
x: 2.000000
y: 3.000000
z: 4.000000
Adding A and B gives the vector:
x: 3.000000
y: 5.000000
z: 7.000000
Dot product of A and B is 20.000000.
Cross product of A and B gives the vector:
x: -1.000000
y: 2.000000
z: -1.000000
Multiplying A by the scalar quantity 5.0 gives the vector:
x: 5.000000
y: 10.000000
z: 15.000000
Vector A:
x: 1.000000
y: 2.000000
z: 3.000000
Vector B:
x: 2.000000
y: 3.000000
z: 4.000000
Adding A and B gives the vector:
x: 3.000000
y: 5.000000
z: 7.000000
Dot product of A and B is 20.000000.
Cross product of A and B gives the vector:
x: -1.000000
y: 2.000000
z: -1.000000
Multiplying A by the scalar quantity 5.0 gives the vector:
x: 5.000000
y: 10.000000
z: 15.000000
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#include <stdio.h> struct vector { float x; float y; float z; }; void printVec(struct vector vec){ } void addVec(struct vector op1, struct vector op2, struct vector *result){ result->x = op1.x + op2.x; result->y = op1.y + op2.y; result->z = op1.z + op2.z; } void initVec(float x, float y, float z, struct vector *vec){ vec->x = x; vec->y = y; vec->z = z; } void multVec(float scalar, struct vector *vec){ vec->x *= scalar; vec->y *= scalar; vec->z *= scalar; } float dotVec(struct vector A, struct vector B){ return ((A.x * B.x) + (A.y * B.y) + (A.z * B.z)); } void crossVec(struct vector A, struct vector B, struct vector *C){ C->x = ((A.y * B.z) - (A.z * B.y)); C->y = ((A.z * B.x) - (A.x * B.z)); C->z = ((A.x * B.y) - (A.y * B.x)); } void main(){ struct vector A; struct vector B; struct vector C; initVec(1.0, 2.0, 3.0, &A); initVec(2.0, 3.0, 4.0, &B); printVec(A); printVec(B); addVec(A, B, &C); printVec(C); crossVec(A, B, &C); printVec(C); multVec(5.0, &A); printVec(A); }