• 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? :)
0
Vasconcelos
Asked:
Vasconcelos
2 Solutions
 
mrjoltcolaCommented:
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]


0
 
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
0
 
ozoCommented:
How was the 2D array stored in the linear array data?
0
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.

 
OrcbighterCommented:
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];
}intArray;

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

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

Open in new window

0
 
ssnkumarCommented:
> 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.
0
 
VasconcelosAuthor Commented:
i already found a solution thanks to the first 2 answers, sorry for my delay :)
0
 
VasconcelosAuthor Commented:
thanks :)
0

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