[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# Calculating sequential index of FORTRAN (column-based) multi-dimensional array

Posted on 2006-05-01
Medium Priority
776 Views
I have some C code that I use to calculate the sequential index in memory of a multi-dimensional arrays. It can be used for an arbitrary number of dimensions. Works in row-major order (C language style). Below is a sample snippet of an array of size 2,3:

C row-major order:
index 0 = 0,0
index 1 = 0,1
index 2 = 0,2
index 3 = 1,0
index 4 = 1,1
index 5 = 1,2

// Calculate and store the factors on startup (ahead of time, in constructor)
// This code created with help from:
//   http://www.brpreiss.com/books/opus5/html/page90.html
int nDimensions = 2;
int nDimSize[2] = {2, 3};
int nFactors[2];
int nProduct = 1;

for (int i=nDimensions-1; i>=0; --i) {
nFactors[i] = nProduct;
nProduct *= nDimSize[i];
}

// When array value is specified later, calculate its index and retrieve it
// example: array(1,0) - zero-based
int nIndices[2] = {1, 0};
int nIndex;

for (int i=0; i<nDimensions; ++i) {
nIndex += nFactors[i] * nIndices[i];
}

// Resulting nIndex is 3 (zero-based)

Now my problem is, I have to figure out a similiar way to calculate the index if the array is specified in column-major order. Our application interfaces with FORTRAN code which uses column-major order. So if a multidmensional array is declared and set in FORTRAN, our C app must correctly display those values. Right now, it is not doing so.

Fortran column-major order:
index 0 = 0,0
index 1 = 1,0
index 2 = 0,1
index 3 = 1,1
index 4 = 0,2
index 5 = 1,2

I can't seem to figure out the right function to calculate the 'nFactors' variable, and the 'nIndex' value from that. You'd think it would be just flipping around a couple of things, but I just can't get it to work.

I'd appreciate any help.
0
Question by:gunn

LVL 11

Accepted Solution

WelkinMaze earned 1200 total points
ID: 16581241
Hi,
You have just to reverse the order of indexing here:

for (int i=nDimensions-1; i>=0; --i) {
nFactors[i] = nProduct;
nProduct *= nDimSize[i];
}

instead from nDimensions-1 to 0 make it from 0 to nDimensions-1. So it becomes:
for (int i=0; i<=nDimensions-1; ++i)

The rest of the entire code is just the same.
0

Author Comment

ID: 16751166
You were right. Sorry about not responding to this sooner. I actually posted the question, then figured it out on my own just a short time later and never revisited. Again, sorry.

So now my code looks like this:

for (i=0; i<= nDimensions-1; i++) {
nFactors[i] = nProduct;
nProduct *= nDimSize[i];
}

Thanks again!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This article will show, step by step, how to integrate R code into a R Sweave document
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This video teaches viewers about errors in exception handling.
###### Suggested Courses
Course of the Month18 days, 16 hours left to enroll