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

Posted on 2006-05-01
Last Modified: 2013-11-08
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:
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.
Question by:gunn
    LVL 11

    Accepted Solution

    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.

    Author Comment

    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!

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Suggested Solutions

    There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
    If you haven’t already, I encourage you to read the first article ( in my series to gain a basic foundation of R and R Studio.  You will also find the …
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now