• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

retrieve matrix from a linear array

Hi, i want to retrieve a 2D array from a linear array data

How do i do it? :)
2 Solutions
You need to know the dimensions of the 2D array first, and those could be constants.
Then you convert x & y to linear dimensions.

#define X_DIM 4
#define Y_DIM 4

int linear[X_DIM * Y_DIM];

So equivalent of matrix[x][y] would be:

linear[x + y * X_DIM]

Raheman M. AbdulCommented:
int linearArray[]={1,2,3,4,5,6,7,8,9,10, .............. };

const int rows=10;
const int columns=5;
int matrix2D[rows][columns];
int r=0;
int c=0;

for (int i=0;i<linearArray.length;i++)

    //get the next element of linear array into the next cell of 2D array


Hope this helps
How was the 2D array stored in the linear array data?
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

If you know the size of both arrays, and these arrays are fixed, you can do some neat shortcutting
(taking the necessary tests and saftey checks first, of course)
typedef struct 
   int row[10];
   int col[5];

int array[] = {1,2,3,,9,10,11,..........};

// for a quick copy of the flat array into the 2D array
memcpy( &intArray, array, sizeof( array))

Open in new window

> Hi, i want to retrieve a 2D array from a linear array data
> How do i do it?
Before you dive into writing code for this, tell us, whether you can do this manually?
If so, can you take an example and show us how you will do it?
Looking at your example, we can suggest you how to write the code.
VasconcelosAuthor Commented:
i already found a solution thanks to the first 2 answers, sorry for my delay :)
VasconcelosAuthor Commented:
thanks :)

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now