Elementary Question: how to modify an array of points?

Started by
5 comments, last by jamesH 24 years, 1 month ago
I am trying to write basic operation of 3d vectors represented by arrays. float pointa[3]={2.0f, 1.5f, 3.0f}; float pointb[3]={2.4f, 2.9f, -0.5f }; float pointc[3]; Given pointa and pointb, I want to make pointc to be the sum of pinta and pointb by using a function with pointers or reference (not by return value). What I want is like: void Add(float pointp[3], float pointq[3], float* out) { out[0] = pointp[0] + pointq[0]; out[1] = pointp[1] + pointq[1]; out[2] = pointp[2] + pointq[2]; } But even if I write Add(pointa, pointb, pointc); it seems pointc does not become the desired sum; What's wrong with this? If I write void AddVersion2(float pointp[3], float pointq[3], float & out0, float & out1, float & out2) { out0 = pointp[0] + pointq[0]; out1 = pointp[1] + pointq[1]; out2 = pointp[2] + pointq[2]; } then AddVersion2(pointa, pointb, pointc[0], pointc[1], pointc[2]) should work, but I want to avoid the long parameters. But how can I write simpler function using array? If I use a struct for definition of a 3d-vector (x, y, z components) and if I write a simple function like Add(pointa, pointb, pointc) by reference, then it works. Should I avoid array representation of 3-d points? James Edited by - jamesh on 2/23/00 1:19:58 PM
Advertisement
Uh, the code worked fine under my compiler....
Really? I will try again. There might be something wrong with my codes other than Add.

Thanks for quick reply.

the code looks fine to me.


try it with

void Add(float* pointp, float* pointq, float* out)

just for kicks

Carl "trixter"[email=carl@trixoft.com]carl@trixoft.com[/email]http://www.trixoft.com
Sorry for my first post. Now I recognised my problem. My problem is how I can modify multi-dimensional array by passing it into function. Please let me restate the problem:

Float VectArrayA[100][3]; //array of vectors
Float VectArrayB[100][3]; //array of vectors
Float VectArrayC[100][3]; //array of vectors

Suppose VectArrayA and VectArrayB are already assigned values. Then I want to assign values for VectArrayC as sum of VectArrayA and VectArrayB, i.e.,

I want to write a code for a function so that, for each k and j,

VectArrayC[k][j] = VectArrayA[k][j]+ VectArrayB[k][j]

by passing VectArrayC in to the function (not a return value of the function). I am very confused about this problem.

James

Edited by - jamesH on 2/25/00 11:47:53 AM
Write it like this

void Add(float *out, float *vecA, float *vecB, int length){  for( int n = 0; n < length*3; n++ )  {    out[n] = vecA[n] + vecB[n];  }}


Of course this requires that each element in the array is three floats long.
Thank you. That is the way to go.
James

Edited by - JamesH on 2/27/00 10:05:32 AM

This topic is closed to new replies.

Advertisement