Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   negative subscripts for 2D array of floats (http://www.velocityreviews.com/forums/t805589-negative-subscripts-for-2d-array-of-floats.html)

lloyd 11-05-2011 07:14 PM

negative subscripts for 2D array of floats
 
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...

Eric Sosman 11-05-2011 08:00 PM

Re: negative subscripts for 2D array of floats
 
On 11/5/2011 3:14 PM, lloyd wrote:
> 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.

--
Eric Sosman
esosman@ieee-dot-org.invalid

lloyd 11-05-2011 08:15 PM

Re: negative subscripts for 2D array of floats
 
On Nov 5, 4:00*pm, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
> * * *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.



All times are GMT. The time now is 02:05 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.