Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 197365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:50:48+00:00 2026-05-11T16:50:48+00:00

A textbook I recently read discussed row major & column major arrays. The book

  • 0

A textbook I recently read discussed row major & column major arrays. The book primarily focused on 1 and 2 dimensional arrays but didn’t really discuss 3 dimensional arrays. I’m looking for some good examples to help solidify my understanding of addressing an element within a multi-dimensional array using row major & column major arrays.

           +--+--+--+  |
          /  /  /  /|  |
         +--+--+--+ +  |        +---+---+---+---+
        /  /  /  /|/|  |       /   /   /   /   /|
       +--+--+--+ + +  |      +---+---+---+---+ +
      /  /  /  /|/|/|  |     /   /   /   /   /|/|
     +--+--+--+ + + +  |    +---+---+---+---+ + +
    /  /  /  /|/|/|/|  |   /   /   /   /   /|/|/|
   +--+--+--+ + + + +  |  +---+---+---+---+ + + +
  /  /  /  /|/|/|/|/   |  |000|001|002|003|/|/|/|
 +--+--+--+ + + + +    |  +---+---+---+---+ + + +
 |00|01|02|/|/|/|/     |  |004|005|006|007|/|/|/|
 +--+--+--+ + + +      |  +---+---+---+---+ + + +
 |03|04|05|/|/|/       |  |008|009|00A|00B|/|/|/
 +--+--+--+ + +        |  +---+---+---+---+ + +
 |06|07|08|/|/         |  |00C|00D|00E|00F|/|/
 +--+--+--+ +          |  +---+---+---+---+ +
 |09|0A|0B|/           |  |010|011|012|013|/
 +--+--+--+            |  +---+---+---+---+
 arr[5][3][4]          |    arr[3][4][5]

NOTE: Original question incorrectly represented arr[3][4][5]. I have learned that the original subscript represents depth. The data has been corrected to reflect intended array representation.

Example hex data
 +---+---+---+---+  +---+---+---+---+  +---+---+---+---+    
 |000|001|002|003|  |100|101|102|103|  |200|201|202|203|  
 +---+---+---+---+  +---+---+---+---+  +---+---+---+---+  
 |004|005|006|007|  |104|105|106|107|  |204|205|206|207|   
 +---+---+---+---+  +---+---+---+---+  +---+---+---+---+   
 |008|009|00A|00B|  |108|109|10A|10B|  |208|209|20A|20B|   
 +---+---+---+---+  +---+---+---+---+  +---+---+---+---+   
 |00C|00D|00E|00F|  |10C|10D|10E|10F|  |20C|20D|20E|20F|
 +---+---+---+---+  +---+---+---+---+  +---+---+---+---+ 
 |010|011|012|013|  |110|111|112|113|  |210|211|212|213|
 +---+---+---+---+  +---+---+---+---+  +---+---+---+---+ 
      slice 0            slice 1            slice 2

 short Arr[3][4][5]; // assume array is filled with hex test data

 arr[1][2][3] = 0x10B use slice 1, row 2, col 3
 arr[2][3][4] = 0x210 use slice 2, row 3, col 4 
                       resolves to row 4, col 0

row major
{000,001,002,003,004,005,006,007,008,009,00A,00B,00C,00D,00E,00F,010,011,012,013,
100,101,102,103,104,105,106,107,108,109,10A,10B,10C,10D,10E,10F,110,111,112,113,
200,201,202,203,204,205,206,207,208,209,20A,20B,20C,20D,20E,20F,210,211,212,213}

column major
{000,004,008,00C,010,001,005,009,00D,011,002,006,00A,00E,012,003,007,00B,00F,013,
100,104,108,10C,110,101,105,109,10D,111,102,106,10A,10E,112,103,107,10B,10F,113,
200,204,208,20C,210,201,205,209,20D,211,202,206,20A,20E,212,203,207,20B,20F,213}

  Calculation offset for arr[1][2][3] using row major offset?
  Calculation offset for arr[1][2][3] using column major offset?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:50:48+00:00Added an answer on May 11, 2026 at 4:50 pm

    When I asked this question I was hoping to find some good 3 dimensional array examples. Especially code examples. Since I didn’t find anything that was understandable, I decided to create a little C program to help display the concept. It uses the same test data in a 3x4x5 array. It also includes test data for a 5x5x5 array. It creates a column major array from the row major array so the offset calculations can be verified.

    The array offset methods are:

  2. char *calc_RowMajor(char *Base, int elemSz, int depth_idx, int row_idx, int col_idx)
  3. char *calc_ColMajor(char *Base, int elemSz, int depth_idx, int col_idx, int row_idx)

    I added comments in the code where applicable to help clarify what the code is doing.

  4. 
    //
    // Arrays.cpp : 
    //     Purpose: Display rowMajor & colMajor data and calculations.
    //
    #include "stdafx.h"
    
    #define _show_Arrays 1  // 1=display rowMajor & colMajor arrays
    #define _square_array 0 // 1=use arr[5][5][5], 0=use arr[3][4][5]
    
    #if (_square_array == 1)
        const int depthSz = 5;
        const int rowSz = 5;
        const int colSz = 5;
        /*
        +---+---+---+---+---+
        |x00|x01|x02|x03|x04|
        +---+---+---+---+---+ 
        |x05|x06|x07|x08|x09|   
        +---+---+---+---+---+  
        |x0A|x0B|x0C|x0D|x0E|   
        +---+---+---+---+---+   
        |x0F|x10|x11|x12|x13|
        +---+---+---+---+---+ 
        |x14|x15|x16|x17|x18|
        +---+---+---+---+---+ 
              slice x          
        */
        short row_arr[depthSz][colSz][rowSz] = {
        { /* slice 0 */
          {0x000,0x001,0x002,0x003,0x004},
          {0x005,0x006,0x007,0x008,0x009},
          {0x00A,0x00B,0x00C,0x00D,0x00E},
          {0x00F,0x010,0x011,0x012,0x013},
          {0x014,0x015,0x016,0x017,0x018}},
        { /* slice 1 */
          {0x100,0x101,0x102,0x103,0x104},
          {0x105,0x106,0x107,0x108,0x109},
          {0x10A,0x10B,0x10C,0x10D,0x10E},
          {0x10F,0x110,0x111,0x112,0x113},
          {0x114,0x115,0x116,0x117,0x118}},
        { /* slice 2 */
          {0x200,0x201,0x202,0x203,0x204},
          {0x205,0x206,0x207,0x208,0x209},
          {0x20A,0x20B,0x20C,0x20D,0x20E},
          {0x20F,0x210,0x211,0x212,0x213},
          {0x214,0x215,0x216,0x217,0x218}},
        { /* slice 3 */
          {0x300,0x301,0x302,0x303,0x304},
          {0x305,0x306,0x307,0x308,0x309},
          {0x30A,0x30B,0x30C,0x30D,0x30E},
          {0x30F,0x310,0x311,0x312,0x313},
          {0x314,0x315,0x316,0x317,0x318}},
        { /* slice 4 */
          {0x400,0x401,0x402,0x403,0x404},
          {0x405,0x406,0x407,0x408,0x409},
          {0x40A,0x40B,0x40C,0x40D,0x40E},
          {0x40F,0x410,0x411,0x412,0x413},
          {0x414,0x415,0x416,0x417,0x418}}
        };
    
    #else
      const int depthSz = 3;
        const int rowSz = 4;
        const int colSz = 5;
        /*
        +---+---+---+---+
        |000|001|002|003|  
        +---+---+---+---+  
        |004|005|006|007|   
        +---+---+---+---+   
        |008|009|00A|00B|   
        +---+---+---+---+   
        |00C|00D|00E|00F|
        +---+---+---+---+ 
        |010|011|012|013|
        +---+---+---+---+ 
             slice x
        */
        short row_arr[depthSz][colSz][rowSz] = {
        {  /* slice 0 */
          {0x000,0x001,0x002,0x003},
          {0x004,0x005,0x006,0x007},
          {0x008,0x009,0x00A,0x00B},
          {0x00C,0x00D,0x00E,0x00F},
          {0x010,0x011,0x012,0x013}},
        { /* slice 1 */
          {0x100,0x101,0x102,0x103},
          {0x104,0x105,0x106,0x107},
          {0x108,0x109,0x10A,0x10B},
          {0x10C,0x10D,0x10E,0x10F},
          {0x110,0x111,0x112,0x113}},
        {  /* slice 2 */
          {0x200,0x201,0x202,0x203},
          {0x204,0x205,0x206,0x207},
          {0x208,0x209,0x20A,0x20B},
          {0x20C,0x20D,0x20E,0x20F},
          {0x210,0x211,0x212,0x213}}
        };
    #endif
        short col_arr[depthSz*colSz*rowSz]; //
    
    char *calc_RowMajor(char *Base, int elemSz, int depth_idx, int row_idx, int col_idx)
    {  // row major slice is navigated by rows
      char *address;
      int   lbound = 0; // lower bound (0 for zero-based arrays)
      address = Base        /* use base passed */
         + ((depth_idx-lbound)*(colSz*rowSz*elemSz))    /* select slice */
         + ((row_idx-lbound)*rowSz*elemSz)      /* select row */
         + ((col_idx-lbound)*elemSz);       /* select col */
        return address;
    }
    char *calc_ColMajor(char *Base, int elemSz, int depth_idx, int col_idx, int row_idx)
    {  // col major slice is navigated by columns
      char *address;
      int   lbound = 0; // lower bound (0 for zero-based arrays)
      int   pageSz = colSz*rowSz*elemSz; 
      int   offset;
    
      offset = (col_idx-lbound)*(colSz*elemSz)  /* select column */
             + (row_idx-lbound)*(elemSz);   /* select row */
        if (offset >= pageSz)
        {   // page overflow, rollover
            offset -= (pageSz-elemSz);                          /* ajdust offset back onto page */
        }
        address = Base            /* use base passed */
                + ((depth_idx-lbound)*pageSz)  /* select slice */
                + offset;
        return address;
    }
    
    void disp_slice(char *pStr, short *pArr,int slice,int cols, int rows)
    {
      printf("== %s slice %d == %p\r\n",pStr, slice,pArr+(slice*rows*cols));
      for(int x=0;x<rows;x++)
      {
        for(int y=0;y<cols;y++)
          printf("%03X ",*(pArr+(slice*rows*cols)+(x*cols)+y));
          printf("\r\n");
      }
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
      // initialize col based array using row based array data
      { // convert row_arr into col_arr
        short *pSrc = &row_arr[0][0][0];
        short *pDst = &col_arr[0];
        for(int d=0;d<depthSz;d++)
          for(int r=0;r<rowSz;r++)
            for(int c=0;c<colSz;c++)
            {
        *pDst++ = *(pSrc+((d*rowSz*colSz)+(c*rowSz)+r));
            }
      }
    
      printf("Using Array[%d][%d][%d]\r\n",depthSz,rowSz,colSz);
    
    #if (_show_Arrays == 1)
      { for(int x=0;x<depthSz;x++) {disp_slice("rowMajor",&row_arr[0][0][0],x,rowSz,colSz);}}
      { for(int x=0;x<depthSz;x++) {disp_slice("colMajor",&col_arr[0],x,rowSz,colSz);}}
    #endif
    
      int d = 2;    // depth
      int r = 3;    // row
      int c = 4;    // column
    
      for(d=0;d<depthSz;d++)
      { 
        c = r = d;  // simple access test pattern arr[0][0][0],arr[1][1][1],arr[2][2][2],...
        { // retrieve Array element
          printf("    row_arr[%d][%d][%d] = %x\t",d,r,c,row_arr[d][r][c]);
          printf("&row_arr[%d][%d][%d] = %p\r\n",d,r,c,&row_arr[d][r][c]);
        }
        { // retrieve RowMajor element
          short *pRowMajor = (short*)calc_RowMajor((char*)&row_arr[0][0][0],sizeof(short),d,r,c);
          printf("calc_RowMajor(%d,%d,%d) = %x\t\t",d,r,c,*pRowMajor);
          printf("pRowMajor = %p\r\n",pRowMajor);
        }
        {   // retrieve ColMajor element
          short *pColMajor = (short*)calc_ColMajor((char*)&col_arr[0],sizeof(short),d,c,r);
          printf("calc_ColMajor(%d,%d,%d) = %x\t\t",d,r,c,*pColMajor);
          printf("pColMajor = %p\r\n",pColMajor);
        }
     } // for
    
     getchar(); // just to hold the console while looking at the information
      return 0;
    }
    
  • 0
  • Reply
  • Share
    Share
    • Share on Facebook
    • Share on Twitter
    • Share on LinkedIn
    • Share on WhatsApp
    • Report

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Based on http://www.zillow.com/howto/api/APIFAQ.htm#devkit, there is no JavaScript API. Because of… May 12, 2026 at 1:18 am
  • Editorial Team
    Editorial Team added an answer You could always use the person's email address in authentication… May 12, 2026 at 1:18 am
  • Editorial Team
    Editorial Team added an answer We primarily use three methods of communications: GWT-RPC - This… May 12, 2026 at 1:18 am

Related Questions

I built a very simple tab based app with 2 tabs, each tab tied
I've been learning C recently, and in one of my textbooks I found a
Recently in a technical interview, I was asked to write a program to find
I'm a moderately experienced Java / C# programmer, and I've recently started learning C++.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.