- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this problem, we will understand the conversion of 2-D array to 1-D array. We will see how to store the elements of a 2-D array to a 1-D array.

Here, the size of 1-D array is same as the total number of elements in 2-D array which is n*m.

In programming there are two ways to store a 2-D array to 1-D array. They are−

- Row Major
- Column Major

**Row Major: **In row major, All the elements of a row are stored together then it moves to the next row.

If an element of 2-D array of size nXm has an index (i, j) is stored in 1-D array, its index in 1-D array is

*(j) + (i)*m*

**Column Major: **In column major, all elements of a column are stored together then the next column is traversed.

If an element of 2-D array of size nXm has an index (i, j) is stored in 1-D array, its index in 1-D array is

*(i) + (j)*n*

**Input: **n = 3, m = 5, (i,j) = (0, 2)

**Output: **row-major =

column-major =

**Explanation: **

Row-major = 2 + 0*3 = 3

Col-major = 0 + 2*5 = 10

#include<iostream> using namespace std; int main() { int n = 3; int m = 5; int grid[n][m] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}, {13, 14, 15}}; int i = 0; int j = 2; int rowMajorIndex = i*n + j; cout<<"Index of element at index (0, 2) in 1-D array using row-major is "<<rowMajorIndex<<endl; int colMajorIndex = i + j*m; cout<<"Index of element at index (0, 2) in 1-D array using column-major is "<<colMajorIndex<<endl; return 0; }

Index of element at index (0, 2) in 1-D array using row-major is 2 Index of element at index (0, 2) in 1-D array using column-major is 10

- Related Questions & Answers
- Reshaping 2-D array in JavaScript
- Print a 2 D Array or Matrix in C#
- Finding transpose of a 2-D array JavaScript
- Build maximum array based on a 2-D array - JavaScript
- Searching in a sorted 2-D array in JavaScript
- Grouping and sorting 2-D array in JavaScript
- Converting object to 2-D array in JavaScript
- Find largest d in array such that a + b + c = d in C++
- Print a 2 D Array or Matrix in Java Programming
- Sorting only columns of a 2-D array in JavaScript
- Compute sum of all elements in 2 D array in C
- Nth smallest element in sorted 2-D array in JavaScript
- Constructing 2-D array based on some constraints in JavaScript
- Merging nested arrays to form 1-d array in JavaScript
- Sorting 2-D array of strings and finding the diagonal element using JavaScript

Advertisements