> Vectors in c programming?

Vectors in c programming?

Posted at: 2014-12-18 
Are you trying C or C++?

Structures in C contain variables only, not functions or function calls.

The updated of the Vector3D struct definition version is better, and will work. I'd choose double instead of float for most uses, but maybe this is going to be stored in large arrays and precision doesn't count. However, your functions still need work.

In your function headers and in the prototypes, you need the argument types. You can omit the argument names in the prototypes, but I think that is poor style. Here's a complete example for the addVector3D function:

/* Prototype -- put this in the header file, after the struct definition, not inside. */

Vector3D addVector3D( Vector3D v1, Vector3D v2 ); /* returns v1+v2 */

/* Function definition: */



Vector3D addVector3D( Vector3D v1, Vector3D v2 ) /* returns v1+v2 */

{

.... Vector3D result;

.... result.a = v1.a + v2.a;

.... result.b = v1.b + v2.b;

.... result.c = v1.c + v2.c;

.... return result;

}

That describes a function that takes two Vector3D arguments and returns a Vector3D result.

The function body (a) defines result variable, computes each field of the result from the argument vector fields, then returns that complete struct value as a function result. (The ... tokens are for visible indentation only. Don't type them in...pretend they're spaces.)

Obviously, some functions will have other result or argument types. Your magVector3d() function takes one vector argument and produces a double (or float if you prefer) result.

double magVector3D( Vector3D v ); /* returns ||v|| */

The function will return sqrt(v.a*v.a + v.b*v.b + v.c+v.c). The function implements a vector magnitude, so you need to include all components in the calculation. Your sqrt(c*c) won't work for a vector c...the * operator isn't defined for structs, and there's no overload mechanism (like you have in C++ or C#) to define a meaning for your struct type. In C you have to define a function for each operation you want to implement.

Define a struct of type struct Vector3D and with its typ synonym name Vector3D(typedef). Write down the answer in a header file, eg ("vector3D.h")

Implement functions to set x,y,z coordinates, addition, substraction and to calculate the magnitude of |v|. You could use the function double sqrt(double x) from math.h. The functions should be declarated in a header file and implemented in a source code file("vector3D.c")

This is the source file, which is given by the task.

#include

#include

#include "vector3D.h"

int main(void){

Vector3D a,b,c;

double magnitude;

setVector3D(&a, 1.0, 3.5, 12.4);

setVector3D(&b, 3.0, 6.0, 22.0);

c = addVector3D (a, b);

c = subVector3D(b, a);

magnitude = magVector3d (c);

return 0;

}

Here is my header file that i wrote:

struct Vector3D{

setVector3D(&a,1.0,3.5,12.4);

setVector3D(&b,1.0,6.0,22.0);

int addVector(a, b);

int subVector(b, a);

int magVector3D (c);

};

typedef double Vector3D;

What is that i'm doing wrong here ?