Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Sorting rows of matrix in descending order followed by columns in ascending order
Next article icon

Calculation of address of element of 1-D, 2-D, and 3-D using row-major and column-major order

Last Updated : 28 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

This article focuses on calculating the address of any element in a 1-Dimensional, 2-Dimensional, and 3-Dimensional array in Row major order and Column major order.

Calculating the address of any element In the 1-D array:

A 1-dimensional array (or single-dimension array) is a type of linear array. Accessing its elements involves a single subscript that can either represent a row or column index. 

Example:

1-D array

To find the address of an element in an array the followingformula is used-

Address of A[Index] = B + W * (Index - LB)

Where:

  • Index = The index of the element whose address is to be found (not the value of the element).
  • B = Base address of the array.
  • W = Storage size of one element in bytes.
  • LB = Lower bound of the index (if not specified, assume zero).

    Example: Given the base address of an array A[1300 ............ 1900] as 1020 and the size of each element is 2 bytes in the memory, find the address of A[1700].  

    Solution:

    Given:

    • Base address (B) = 1020
    • Lower bound (LB) = 1300
    • Size of each element (W) = 2 bytes
    • Index of element (not value) = 1700


    Formula used:
    Address of A[Index] = B + W * (Index - LB)
    Address of A[1700] = 1020 + 2 * (1700 - 1300)
                                  = 1020 + 2 * (400)
                                  = 1020 + 800
    Address of A[1700] = 1820

    Calculate the address of any element in the 2-D array:

    The 2-dimensional array can be defined as an array of arrays. The 2-Dimensional arrays are organized as matrices which can be represented as the collection of rows and columns as array[M][N] where M is the number of rows and N is the number of columns. 

    Example:

    2-D array

    To find the address of any element in a 2-Dimensional array there are the following two ways-

    1. Row Major Order
    2. Column Major Order

    1. Row Major Order:

    Row major ordering assigns successive elements, moving across the rows and then down the next row, to successive memory locations. In simple language, the elements of an array are stored in a Row-Wise fashion.
    To find the address of the element using row-major order uses the following formula:

    Address of A[I][J] = B + W * ((I - LR) * N + (J - LC))   

    I = Row Subset of an element whose address to be found, 
    J = Column Subset of an element whose address to be found, 
    B = Base address, 
    W = Storage size of one element store in an array(in byte), 
    LR = Lower Limit of row/start row index of the matrix(If not given assume it as zero), 
    LC = Lower Limit of column/start column index of the matrix(If not given assume it as zero), 
    N = Number of column given in the matrix.

    Example: Given an array, arr[1.........10][1.........15] with base value 100 and the size of each element is 1 Byte in memory. Find the address of arr[8][6] with the help of row-major order.

    Solution:

    Given:
    Base address B = 100
    Storage size of one element store in any array W = 1 Bytes
    Row Subset of an element whose address to be found I = 8
    Column Subset of an element whose address to be found J = 6
    Lower Limit of row/start row index of matrix LR = 1 
    Lower Limit of column/start column index of matrix = 1
    Number of column given in the matrix N = Upper Bound - Lower Bound + 1
                                                                                = 15 - 1 + 1
                                                                                = 15

    Formula:
    Address of A[I][J] = B + W * ((I - LR) * N + (J - LC)) 

    Solution:
    Address of A[8][6] = 100 + 1 * ((8 - 1) * 15 + (6 - 1))
                                       = 100 + 1 * ((7) * 15 + (5))
                                      = 100 + 1 * (110)
    Address of A[I][J] = 210

    2. Column Major Order:

    If elements of an array are stored in a column-major fashion means moving across the column and then to the next column then it's in column-major order. To find the address of the element using column-major order use the following formula:

    Address of A[I][J] = B + W * ((J - LC) * M + (I - LR))  

    I = Row Subset of an element whose address to be found, 
    J = Column Subset of an element whose address to be found, 
    B = Base address, 
    W = Storage size of one element store in any array(in byte), 
    LR = Lower Limit of row/start row index of matrix(If not given assume it as zero), 
    LC = Lower Limit of column/start column index of matrix(If not given assume it as zero), 
    M = Number of rows given in the matrix.

    Example: Given an array arr[1.........10][1.........15] with a base value of 100 and the size of each element is 1 Byte in memory find the address of arr[8][6] with the help of column-major order.

    Solution:

    Given:
    Base address B = 100
    Storage size of one element store in any array W = 1 Bytes
    Row Subset of an element whose address to be found I = 8
    Column Subset of an element whose address to be found J = 6
    Lower Limit of row/start row index of matrix LR = 1
    Lower Limit of column/start column index of matrix = 1
    Number of Rows given in the matrix M = Upper Bound - Lower Bound + 1
                                                                                = 10 - 1 + 1
                                                                               = 10

    Formula: used
    Address of A[I][J] = B + W * ((J - LC) * M + (I - LR))
    Address of A[8][6] = 100 + 1 * ((6 - 1) * 10 + (8 - 1))
                                      = 100 + 1 * ((5) * 10 + (7))
                                     = 100 + 1 * (57)
    Address of A[I][J] = 157 

    From the above examples, it can be observed that for the same position two different address locations are obtained that's because in row-major order movement is done across the rows and then down to the next row, and in column-major order, first move down to the first column and then next column. So both the answers are right.

    So it's all based on the position of the element whose address is to be found for some cases the same answers is also obtained with row-major order and column-major order and for some cases, different answers are obtained.

    Calculate the address of any element in the 3-D Array:

    A 3-Dimensional array is a collection of 2-Dimensional arrays. It is specified by using three subscripts:

    1. Block size
    2. Row size
    3. Column size

    More dimensions in an array mean more data can be stored in that array. 

    Example:

    3-D array

    To find the address of any element in 3-Dimensional arrays there are the following two ways-

    • Row Major Order
    • Column Major Order

    1. Row Major Order:

    To find the address of the element using row-major order, use the following formula:

    Address of A[i][j][k] = B + W *(P* N * (i-x) + P*(j-y) + (k-z))

    Here:

    B = Base Address (start address)
    W = Weight (storage size of one element stored in the array)
    M = Row (total number of rows)
    N = Column (total number of columns)
    P = Width (total number of cells depth-wise)
    x = Lower Bound of Row
    y = Lower Bound of Column
    z = Lower Bound of Width

    Example: Given an array, arr[1:9, -4:1, 5:10] with a base value of 400 and the size of each element is 2 Bytes in memory find the address of element arr[5][-1][8] with the help of row-major order?

    Solution:

    Given:
    Block Subset of an element whose address to be found I = 5
    Row Subset of an element whose address to be found J = -1 
    Column Subset of an element whose address to be found K = 8
    Base address B = 400
    Storage size of one element store in any array(in Byte) W = 2
    Lower Limit of blocks in matrix x = 1
    Lower Limit of row/start row index of matrix y = -4 
    Lower Limit of column/start column index of matrix z = 5
    M(row) = Upper Bound - Lower Bound + 1 = 1 - (-4) + 1 = 6
    N(Column)= Upper Bound - Lower Bound + 1 = 10 - 5 + 1 = 6
     

    Formula used:                                                
    Address of[I][J][K] =B + W (M * N(i-x) + N *(j-y) + (k-z))

    Solution:
    Address of arr[5][-1][8] = 400 + 2 * {[6 * 6 * (5 - 1)] + 6 * [(-1 + 4)]} + [8 - 5]
                                 = 400 + 2 * (6*6*4)+(6*3)+3
                                = 400 + 2 * (165)
                               = 730

    2. Column Major Order:

    To find the address of the element using column-major order, use the following formula:1

    Address of A[i][j][k]=B+W×(M×P×(k−z)+M×(j−y)+(i−x))

    Here:

    B = Base Address (start address)
    W = Weight (storage size of one element stored in the array)
    M = Row (total number of rows)
    N = Column (total number of columns)
    P = Width (total number of cells depth-wise)
    x = Lower Bound of block (first subscipt)
    y = Lower Bound of Row
    z = Lower Bound of Column

    Example: Given an array arr[1:8, -5:5, -10:5] with a base value of 400 and the size of each element is 4 Bytes in memory find the address of element arr[3][3][3] with the help of column-major order?

    Solution:

    Given:
    Row Subset of an element whose address to be found I = 3
    Column Subset of an element whose address to be found J = 3
    Block Subset of an element whose address to be found K = 3
    Base address B = 400
    Storage size of one element store in any array(in Byte) W = 4
    Lower Limit of blocks in matrix x = 1
    Lower Limit of row/start row index of matrix y = -5
    Lower Limit of column/start column index of matrix z = -10
    M (row)= Upper Bound - Lower Bound + 1 = 8 - 1 + 1 = 8
    N (column)= Upper Bound - Lower Bound + 1 = 5 - (-5 ) + 1 = 11

    Formula used:
    Address of A[i][j][k]=B+W×(M×P×(k−z)+M×(j−y)+(i−x))

    Solution:
    Address of arr[3][3][3] = 400 + 4 * ((11*8*(3-(-10)+ 8*(3-(-5)+ (3-1))
                                        = 400 + 4 * ((88 * 13+ 8 * 8 + 2)
                                       = 400 + 4 * (1210)
                                      = 400 + 4840                        
                                     = 5240


    Next Article
    Sorting rows of matrix in descending order followed by columns in ascending order

    S

    shivam11321
    Improve
    Article Tags :
    • Mathematical
    • Computer Subject
    • Data Structures
    • GATE CS
    • DSA
    • Arrays
    Practice Tags :
    • Arrays
    • Data Structures
    • Mathematical

    Similar Reads

    • Calculating the address of an element in an N-dimensional array
      N-Dimensional Arrays: The N-Dimensional array is basically an array of arrays. As 1-D arrays are identified as a single index, 2-D arrays are identified using two indices, similarly, N-Dimensional arrays are identified using N indices. A multi-dimensional array is declared as follows: int NDA[S1][S2
      4 min read
    • XOR of major diagonal elements of a 3D Matrix
      Given a 3D matrix mat[][][] of dimensions N * N * N consisting of positive integers, the task is to calculate Bitwise XOR of all matrix elements present in the major diagonal. Examples: Input: arr[][][] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}Output: 12Explanation: The major diagonal elements are {1,
      9 min read
    • Row Major Order and Column Major Order
      When it comes to organizing and accessing elements in a multi-dimensional array, two prevalent methods are Row Major Order and Column Major Order. These approaches define how elements are stored in memory and impact the efficiency of data access in computing. Table of Content Row Major OrderHow to f
      5 min read
    • Sorting rows of matrix in ascending order followed by columns in descending order
      Given a matrix, sort the rows of matrix in ascending order followed by sorting the columns in descending order. Examples : Input : a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Output : 7 8 9 4 5 6 1 2 3 Input : a[3][3] = {{3, 2, 1}, {9, 8, 7}, {6, 5, 4}}; Output : 7 8 9 4 5 6 1 2 3 Approach: Travers
      10 min read
    • Sorting rows of matrix in descending order followed by columns in ascending order
      Given a matrix of distinct elements. The task is to sort the rows of matrix in descending order followed by sorting the columns in ascending order. Examples: Input: a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Output: 3 2 1 6 5 4 9 8 7 Input: a[3][3] = {{3, 2, 1}, {9, 8, 7}, {6, 5, 4}}; Output: 3 2
      9 min read
    • Sorting 2D Vector in C++ | Set 2 (In descending order by row and column)
      We have discussed some of the cases of sorting 2D vector in below set 1. Sorting 2D Vector in C++ | Set 1 (By row and column) More cases are discussed in this article Case 3 : To sort a particular row of 2D vector in descending order This type of sorting arranges a selected row of 2D vector in desce
      4 min read
    • Enlarge a Matrix such that each element occurs in R rows and C columns
      Given a matrix arr[][] of size N x M, and two numbers R and C, the task is to enlarge this matrix such that each element of the original matrix occurs in R rows and C columns in the enlarged matrix. Examples: Input: arr[][] = {{1, 2, 3}, {4, 5, 6}} R = 3, C = 2 Output: 1 1 2 2 3 3 4 4 5 5 6 6 1 1 2
      9 min read
    • Kth smallest element in a row-wise and column-wise sorted 2D array
      Given an n x n matrix, every row and column is sorted in non-decreasing order. Given a number K where K lies in the range [1, n*n], find the Kth smallest element in the given 2D matrix. Example: Input: mat ={{10, 20, 30, 40}, {15, 25, 35, 45}, {24, 29, 37, 48}, {32, 33, 39, 50 }}K = 3Output: 20Expla
      15+ min read
    • Count of matrices (of different orders) with given number of elements
      Given a number N denotes the total number of elements in a matrix, the task is to print all possible order of matrix. An order is a pair (m, n) of integers where m is number of rows and n is number of columns. For example, if the number of elements is 8 then all possible orders are: (1, 8), (2, 4),
      5 min read
    • Search in a Row-wise and Column-wise Sorted 2D Array using Divide and Conquer algorithm
      Given an n x n matrix, where every row and column is sorted in increasing order. Given a key, how to decide whether this key is in the matrix. Input: x = 62, mat[][] = [[3, 30, 38], [20, 52, 54], [35, 60, 69]]Output: falseExplanation: 62 is not present in the matrix. Input: x = 30, mat[][] = [[3, 30
      7 min read
    geeksforgeeks-footer-logo
    Corporate & Communications Address:
    A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
    Registered Address:
    K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
    GFG App on Play Store GFG App on App Store
    Advertise with us
    • Company
    • About Us
    • Legal
    • Privacy Policy
    • In Media
    • Contact Us
    • Advertise with us
    • GFG Corporate Solution
    • Placement Training Program
    • Languages
    • Python
    • Java
    • C++
    • PHP
    • GoLang
    • SQL
    • R Language
    • Android Tutorial
    • Tutorials Archive
    • DSA
    • Data Structures
    • Algorithms
    • DSA for Beginners
    • Basic DSA Problems
    • DSA Roadmap
    • Top 100 DSA Interview Problems
    • DSA Roadmap by Sandeep Jain
    • All Cheat Sheets
    • Data Science & ML
    • Data Science With Python
    • Data Science For Beginner
    • Machine Learning
    • ML Maths
    • Data Visualisation
    • Pandas
    • NumPy
    • NLP
    • Deep Learning
    • Web Technologies
    • HTML
    • CSS
    • JavaScript
    • TypeScript
    • ReactJS
    • NextJS
    • Bootstrap
    • Web Design
    • Python Tutorial
    • Python Programming Examples
    • Python Projects
    • Python Tkinter
    • Python Web Scraping
    • OpenCV Tutorial
    • Python Interview Question
    • Django
    • Computer Science
    • Operating Systems
    • Computer Network
    • Database Management System
    • Software Engineering
    • Digital Logic Design
    • Engineering Maths
    • Software Development
    • Software Testing
    • DevOps
    • Git
    • Linux
    • AWS
    • Docker
    • Kubernetes
    • Azure
    • GCP
    • DevOps Roadmap
    • System Design
    • High Level Design
    • Low Level Design
    • UML Diagrams
    • Interview Guide
    • Design Patterns
    • OOAD
    • System Design Bootcamp
    • Interview Questions
    • Inteview Preparation
    • Competitive Programming
    • Top DS or Algo for CP
    • Company-Wise Recruitment Process
    • Company-Wise Preparation
    • Aptitude Preparation
    • Puzzles
    • School Subjects
    • Mathematics
    • Physics
    • Chemistry
    • Biology
    • Social Science
    • English Grammar
    • Commerce
    • World GK
    • GeeksforGeeks Videos
    • DSA
    • Python
    • Java
    • C++
    • Web Development
    • Data Science
    • CS Subjects
    @GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
    We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
    Lightbox
    Improvement
    Suggest Changes
    Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
    geeksforgeeks-suggest-icon
    Create Improvement
    Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
    geeksforgeeks-improvement-icon
    Suggest Changes
    min 4 words, max Words Limit:1000

    Thank You!

    Your suggestions are valuable to us.

    What kind of Experience do you want to share?

    Interview Experiences
    Admission Experiences
    Career Journeys
    Work Experiences
    Campus Experiences
    Competitive Exam Experiences