Vector Operations

/ 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

`#include <stdio.h> struct vector {	float x;	float y;	float z;}; void printVec(struct vector vec){	printf("x: %f\ny: %f\nz: %f\n\n", vec.x, vec.y, vec.z);} 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);	printf("Vector A:\n");	printVec(A);	printf("Vector B:\n");	printVec(B);	printf("Adding A and B gives the vector:\n");	addVec(A, B, &C);	printVec(C);        printf("Dot product of A and B is %f.\n\n", dotVec(A, B));	printf("Cross product of A and B gives the vector:\n");	crossVec(A, B, &C);	printVec(C);	printf("Multiplying A by the scalar quantity 5.0 gives the vector:\n");	multVec(5.0, &A);	printVec(A);}`