negative subscripts for 2D array of floats

L

lloyd

Hi,
I would like to set up a 2-D array of floats representing a function
of integer coordinates from the half-plane above (and including) the x-
axis. To be specific, I will calculate a function for each of the
integer points inside (and on the boundary of) the rectangle formed by
(-100,0) to (100,0) to (100,100) to (100,-100). This is a 101x201
rectangle which I would declare as follows:

double A[201][101];

I think it's possible to define a pointer p referring to the middle of
this array, so that I can use intuitive coordinates like p[-43][21]
inside my program to write to and access the array relative to the
pointer. Could someone tell me how to figure out what to define p as?
If I only had a one-dimensional array I think I could put

int p=A+100;

(or would I need to multiply 100 by the size of a float because it's
an array of floats, not ints?)

Thanks. I always use arrays instead of pointers and so there are some
gaps in my knowledge...
 
E

Eric Sosman

Hi,
I would like to set up a 2-D array of floats representing a function
of integer coordinates from the half-plane above (and including) the x-
axis. To be specific, I will calculate a function for each of the
integer points inside (and on the boundary of) the rectangle formed by
(-100,0) to (100,0) to (100,100) to (100,-100).

Are you sure about that final coordinate? I'll assume it is
supposed to be (-100,100); if that's not right I've misunderstood
your intent and you should probably stop reading.
This is a 101x201
rectangle which I would declare as follows:

double A[201][101];

I think it's possible to define a pointer p referring to the middle of
this array, so that I can use intuitive coordinates like p[-43][21]
inside my program to write to and access the array relative to the
pointer. Could someone tell me how to figure out what to define p as?
If I only had a one-dimensional array I think I could put

int p=A+100;

(or would I need to multiply 100 by the size of a float because it's
an array of floats, not ints?)

What you've shown is just a garble, not to be made meaningful
but anything as simple as a multiplication.
Thanks. I always use arrays instead of pointers and so there are some
gaps in my knowledge...

Here's a way to build a solution to your problem. Begin by
defining a name for the type representing "one column of values
along the Y dimension:"

typedef double Column[101];

Now your two-dimensional grid can be thought of as a one-dimensional
array of Columns:

Column A[201];

Then you can point to the midmost column:

Column *origin = &A[100];

.... and now you can write `origin[-43][21]'. It's possible to
dispense with the typedef and collapse everything into one
declaration, but doing it in small steps may avoid confusion,
particularly if you're leery of pointers.
 
L

lloyd

     Are you sure about that final coordinate?  I'll assume it is
supposed to be (-100,100); if that's not right I've misunderstood
your intent and you should probably stop reading.

You're right, sorry for that slip.
     Here's a way to build a solution to your problem. [snip]
... and now you can write `origin[-43][21]'.

That's fantastic, Eric, just what I needed. Thank you.

Lloyd.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top