Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Multi-dimensional Arrays 5

Status
Not open for further replies.

ASingerMustDie

Programmer
Feb 1, 2001
17
GB
Hi All :)

Surely this has been asked before, I would search but that particular facility appears to be permanently out-of-order.

What I am hoping to do is declare a 2-dimensional array with bounds according to two variables, such as:

int x = 50; // could be anything
iny y = 100; // could be anything
double *imageArray;

imageArray = new double [x][y];
imageArray[25][50] = 2.5;

We surely all know that this isn't quite right, but how can it be done?

Thanks in advance, and thanks for helping with my previous post :)

Regards.
 
You can create a 1-D array, and then to access one of the elements:
q=25;
imageArray[q*100+50];

It will grab the same element as:
imageArray[q][50];
Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
aphrodia's trick is a good one, I'll expand a little...

You take a long linear array and implement it as a matrix. If it's a 50 column and 100 row, and you want the 2nd col, 5th row, the formula would be:

element[5000];
/* 5th row, 2nd col is */
n = 5 * 50 + 2;
/* This moves down 5 rows, and over 2 columns. */

if you treat all of your accesses like this, you can get away with treating a 1d array as 2d. Be careful at the boundry conditions, though.

MWB

As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Thanks everyone.

To be frank, I never expected that it wouldn't be supported or I'd have worked on other solutions myself.

Thanks for the tips :)
You've all been nominated for Tipster :-D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top