473,472 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Order of uknown array subscript

Hello all

I have a question that may or may not be simple - I need my three
dimensional array to be of the form:

array[jj][kk][ii]

where jj and kk range from 0 to some constant fixed at the very beginnning
of the program (currently 20) but [ii] goes from 0 to some boundary that
depends on calculations performed within the program. I know it is possible
to do this:

double (*somearray)[fixed][fixed]= new double[variable
expression][fixed][fixed]

but can I have the last subscript as variable? - this is not just a question
of hypothetics, but a need to have the data in this format as an external
procedure expects the data with the first and second dimensions to be one
type of data (which has a fixed size) and the third dimension to have a
variable size.

Many thanks for any help!

Currently I do this:

const int griddef=20;
int somefunction(unsigned int wldepth,other arguments...)
int status, ii, jj, kk;

double *array[griddef][wldepth]; /*<------This doesn't work as wldepth is a
variable, but it shows what I want to do!*/

long naxes[3] = { griddef, griddef,wldepth };

array[0][0] = (double *)malloc( naxes[0] * naxes[1] * naxes[2] * sizeof(
double ) );

for(jj=0;jj<griddef;jj++)

{

for(ii=0;ii<griddef;ii++)

{

array[jj][ii] = array[0][0] + griddef*(griddef*jj+ii);

}

}

for(kk=0;kk<griddef;kk++)

{

for (jj=0;jj<griddef;jj++)

{

for (ii=0;ii<wldepth;ii++)

{

array[jj][kk][ii] = some calculation based on ii,jj and kk;

}

}

}

return 0;

}
Jul 22 '05 #1
4 1537
"Tom Page" <t.******@durham.ac.uk> wrote...
Hello all

I have a question that may or may not be simple - I need my three
dimensional array to be of the form:

array[jj][kk][ii]

where jj and kk range from 0 to some constant fixed at the very beginnning
of the program (currently 20) but [ii] goes from 0 to some boundary that
depends on calculations performed within the program. I know it is possible to do this:

double (*somearray)[fixed][fixed]= new double[variable
expression][fixed][fixed]

but can I have the last subscript as variable? - this is not just a question of hypothetics, but a need to have the data in this format as an external
procedure expects the data with the first and second dimensions to be one
type of data (which has a fixed size) and the third dimension to have a
variable size.
There are several ways to skin your cat. You can have a two-dimensional
array of vector<double>. You can have an array of pointers each of which
is dynamically allocated, almost like you did below (I snipped it, but you
know what I am talking about), you just need to do

double *tda[fixed1][fixed2], **ptda = &tda[0][0];
for (int i = 0; i < fixed1 * fixed2; ++i)
ptda[i] = new double[variable];

thus allocating 'variable' doubles for each of tda elements. Now access
them using

tda[jj][kk][ii]
[...]


Also take a look at the FAQ, see "Dynamic multidimensional arrays".

Victor
Jul 22 '05 #2
Victor

Thanks so much for your response. I am having some troubles with it though.
I have posted my efforts on to a web site to avoid copying large amounts of
code into the message - it's not very long but I thought it would be
cleaner.

http://www.tompagenet.co.uk/fits.php

there are links on the right to my original code (as hinted at in my first
post) and the code that I created after your suggestions. The new code fails
to run, almost certainly because of the:
fits_write_img(fptr, TDOUBLE, fpixel, nelements, array[0][0], &status)line
in the code. This calls an external procedure (one of the CFITSIO
procedures - http://heasarc.gsfc.nasa.gov/docs/so...io/fitsio.html
for those who are interested).The trouble is that this appears to expect an
array of the form array[0][0] (which is two dimensional, but then it knows
it is three dimensional and works out the third dimension by using the
nelements argument which explains the total number of elements and thus if
you know two dimensions it can calculate the third.Should it be of
importance, these are the argements both procedures take:* wldepth - the
size of the dynamic array dimension (the other two are of static size
griddef)* inarray[][griddef][griddef] - the inputed array (basically the
data to write out by the procedure seen above)* line - a number that simply
alters the filename (irrelevant)* type - ditto (irrelevant)Thanks for any
help with this, it is very much appreciated!Thanks, Tom
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:YPcYb.46506$_44.44246@attbi_s52...
"Tom Page" <t.******@durham.ac.uk> wrote...
Hello all

I have a question that may or may not be simple - I need my three
dimensional array to be of the form:

array[jj][kk][ii]

where jj and kk range from 0 to some constant fixed at the very
beginnning
of the program (currently 20) but [ii] goes from 0 to some boundary that
depends on calculations performed within the program. I know it is

possible
to do this:

double (*somearray)[fixed][fixed]= new double[variable
expression][fixed][fixed]

but can I have the last subscript as variable? - this is not just a

question
of hypothetics, but a need to have the data in this format as an external
procedure expects the data with the first and second dimensions to be one
type of data (which has a fixed size) and the third dimension to have a
variable size.


There are several ways to skin your cat. You can have a two-dimensional
array of vector<double>. You can have an array of pointers each of which
is dynamically allocated, almost like you did below (I snipped it, but you
know what I am talking about), you just need to do

double *tda[fixed1][fixed2], **ptda = &tda[0][0];
for (int i = 0; i < fixed1 * fixed2; ++i)
ptda[i] = new double[variable];

thus allocating 'variable' doubles for each of tda elements. Now access
them using

tda[jj][kk][ii]
[...]


Also take a look at the FAQ, see "Dynamic multidimensional arrays".

Victor

Jul 22 '05 #3
/*Sorry - the post should have read*/

Victor

Thanks so much for your response. I am having some troubles with it though.
I have posted my efforts on to a web site to avoid copying large amounts of
code into the message - it's not very long but I thought it would be
cleaner.

http://www.tompagenet.co.uk/fits.php

there are links on the right to my original code (as hinted at in my first
post) and the code that I created after your suggestions. The new code fails
to run, almost certainly because of the:
fits_write_img(fptr, TDOUBLE, fpixel, nelements, array[0][0], &status)

line in the code. This calls an external procedure (one of the CFITSIO
procedures - http://heasarc.gsfc.nasa.gov/docs/so...io/fitsio.html
for those who are interested).

The trouble is that this appears to expect an array of the form array[0][0]
(which is two dimensional, but then it knows it is three dimensional and
works out the third dimension by using the nelements argument which explains
the total number of elements and thus if you know two dimensions it can
calculate the third.

Should it be of importance, these are the argements both procedures take:
* wldepth - the size of the dynamic array dimension (the other two are of
static size griddef)
* inarray[][griddef][griddef] - the inputed array (basically the data to
write out by the procedure seen above)
* line - a number that simply alters the filename (irrelevant)
* type - ditto (irrelevant)

Thanks for any help with this, it is very much appreciated!

Thanks, Tom
Jul 22 '05 #4
"Tom Page" <t.******@durham.ac.uk> wrote...
/*Sorry - the post should have read*/

Victor

Thanks so much for your response. I am having some troubles with it though. [...]


My fault, probably.

This is what I meant to supply as an example:

const int griddef = 100;

void foo(double *inarray[griddef][griddef], int thirddim)
{
for (int kk = 0; kk < griddef; ++kk)
for (int jj = 0; jj < griddef; ++jj)
for (int ii = 0; ii < thirddim; ++ii)
inarray[kk][jj][ii] = 3.1415926;
}

int main()
{
double *myarray[griddef][griddef];

int third_dimension = 20;

for (int kk = 0; kk < griddef; ++kk)
for (int jj = 0; jj < griddef; ++jj)
myarray[kk][jj] = new double[third_dimension];

foo(myarray, third_dimension);

for (int kk = 0; kk < griddef; ++kk)
for (int jj = 0; jj < griddef; ++jj)
delete[] myarray[kk][jj];
}

Victor
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
9
by: copx | last post by:
I wonder if something like this would be save/portable: m = find_slot(); if (m != NOT_FOUND && list.state == OK) { ... The question is: Is it guaranteed that the "list.state == OK" check...
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
0
by: Mark Gibson | last post by:
Hi, I've been playing about with array's, and found the concat operator '||' quite useful, apart from the fact that prepending an element places it in a lower subscript. Is there a way of...
51
by: Pedro Graca | last post by:
I run into a strange warning (for me) today (I was trying to improve the score of the UVA #10018 Programming Challenge). $ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc...
5
by: kalki70 | last post by:
Hello, I've been loooking for info about this issue, but I still can't find. If I create an array of objects, are the constructors called in some predefined order, or it is compiler-dependent? ...
10
by: nachch | last post by:
Does the C specification define the order of evaluation of assignment statements? For example, what should be the output from the following: int foo1() { printf("foo1\n"); return 0; } int...
3
by: uche | last post by:
Please give me some feed back on this issue: Here is the complier error: hexdmp.cpp: In function `void output(unsigned char, int, bool&)': hexdmp.cpp:133: error: invalid types `unsigned char'...
15
by: Jeroen | last post by:
Hi all, I've got a very specific question about the evaluation order in C++. Assume some kind of custom array class, with an overloaded subscript operator. In the following code: { my_array...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.