Thursday, July 9, 2009

Vector addition in C++...?

I'm attempting to learn how to use C++ on my own in an independent studies course. I have compleated several different types of programs already on my own and am now attempting to create a vector addition program, but I'm stuck. I'm looking for a program which is already complete to use as a template to understand the programming methods better. Please don't think I am just asking for your help with homework, I'm really just trying to understand how to program better. Thanks in advance.

Vector addition in C++...?
// For any vector work I use structures. For a 3-D vector define a structure as:





typedef struct


{


double x;


double y;


double z;


} VECTOR;





// Write a simple main to exercise some vector functions





void main()


{


VECTOR VectorAdd(VECTOR A, VECTOR B);


double VectorDot(VECTOR A, VECTOR B);


VECTOR VectorCross(VECTOR A, VECTOR B);





VECTOR a, b, c;


double d;





a.x = 1.0;


a.y = 3.0;


a.z = 4.3;





b.x = -2.3;


b.y = 4.4;


b.z = 1.1;





c = VectorAdd(a, b);





printf("Added: x: %lf, y: %lf, z: %lf\n", c.x, c.y, c.z);





c = VectorCross(a, b);





printf("Crossed: x: %lf, y: %lf, z: %lf\n", c.x, c.y, c.z);





d = VectorDot(a, b);





printf("Dotted: %lf\n", d);





}





VECTOR VectorAdd(VECTOR A, VECTOR B)


{


VECTOR C;





C.x = A.x + B.x;


C.y = A.y + B.y;


C.z = A.z + B.z;





return C;


}





VECTOR VectorDot(VECTOR A, VECTOR B)


{


return A.x*B.x + A.y*B.y + A.z*B.z;


}





VECTOR VectorCross(VECTOR A, VECTOR B)


{


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;





return C;


}


No comments:

Post a Comment