retrieve matrix from a linear array

Posted on 2011-04-30
Last Modified: 2012-05-11
Hi, i want to retrieve a 2D array from a linear array data

How do i do it? :)
Question by:Vasconcelos
    LVL 40

    Assisted Solution

    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]

    LVL 18

    Accepted Solution

    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
    LVL 84

    Expert Comment

    How was the 2D array stored in the linear array data?
    LVL 9

    Expert Comment

    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

    LVL 8

    Expert Comment

    > 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.

    Author Comment

    i already found a solution thanks to the first 2 answers, sorry for my delay :)

    Author Closing Comment

    thanks :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now