Thread: Changing from one dimensional arrays to two

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    3

    Changing from one dimensional arrays to two

    I want to take the entries of a one diemsional array that is 1 X a*b in size and transfer them into two dimesional array that is a X b in size. The problem is that I am pretty new to C. I am simply trying to use it for a project in statistics. I am familiar with basic loops and such, but basically I have only one semester of programming experience.

    Thanks for any tips anyone can give me.

    Dean

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I don't understand what you're trying to say. A little sample code may help.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    5
    Try this:
    Code:
    /**
     * make2d: takes an array of integers and returns a 2-dimensional array
     *         of integers with specified number of rows and columns.  Allocates
     *         a new array, so you must remember to free it after use.
     */
    int** make2d(int* flatArray, unsigned int numRows, unsigned int numCols)
    {
        int** retArray(NULL);
        unsigned int i, j;
    
        if( NULL != flatArray )
        {
            retArray = (int**) malloc(sizeof(int*) * numRows);
    
            for( i = 0; i < numRows; i++ )
            {
                retArray[i] = (int*) malloc(sizeof(int) * numCols);
                for( j = 0; j < numCols; j++ );
                {
                    retArray[i][j] = flatArray[i * numRows + j];
                }
            }
        }
    
        return retArray;
    }
    Does that make sense? It's been a while since I last wrote regular C, so I might be using some unsupported syntax, but I think that does what you're asking. A few caveats are that if you give it an array that isn't as big as the specified size (numRows * numCols), it'll segfault. Also, you have to remember to free the array returned after you're done, otherwise it'll be a memory leak.

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    3

    Clarification

    Does this work? Note that I willl always know the value of nrows and ncols beforehand. I'm just looking for something that will move a list of numbers (one dimensional) to a table of numbers (two dimensional) in a way that fills the table from top to bottom, right to left.

    Code:
    double onedim[nrows*ncols+1]
    double twodim[nrows+1][ncols+1];
    int i, j, k;
    
    k=1;
    
    for(j=1 ; j<=ncols ; j++)
            for(i=1 ; i<=nrows ; i++)
                    {
                     twodim[i][j]=0.0;
                     twodim[i][j]=onedim[k];
                     k++;
                     }
    Thanks guys (and girls).

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, it would be best to start your for loops counting at zero. And perhaps you could use < instead of <=, which would then allow you to leave out the +1s in your declarations. Also, k is unnessesary; you can calculate it like so:
    Code:
    onedim[j*ncols + i];
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Schmag View Post
    Does this work? Note that I willl always know the value of nrows and ncols beforehand. I'm just looking for something that will move a list of numbers (one dimensional) to a table of numbers (two dimensional) in a way that fills the table from top to bottom, right to left.

    Code:
    double onedim[nrows*ncols+1]
    double twodim[nrows+1][ncols+1];
    int i, j, k;
    
    k=1;
    
    for(j=1 ; j<=ncols ; j++)
            for(i=1 ; i<=nrows ; i++)
                    {
                     twodim[i][j]=0.0;
                     twodim[i][j]=onedim[k];
                     k++;
                     }
    Thanks guys (and girls).
    Nope.

    You'll need to dimension 1Dim differently, so your program will not be out of the bounds of the array.

    If nrow = 6 and ncol = 7, then 1Dim is now set to give you 6 * 7 + 1 == 43 elements
    2Dim is sized for (6 + 1) * (7 + 1) == 56 elements.

    What is filling the array from "top to bottom, and right to left", really mean? Top to bottom implies ascending order, row major, and right to left implies column order will be descending?? Confusing, but I don't know your specific needs here, either.

    This is a fine little trick to understand, imo. I've used it in my Sudoku program, in several places, to take advantage of the natural structure of the one and two dimension arrays.

    I would ask *YOU* to run the program and test it, yourself. YOU tell us if it works or not. That's a BIG part of learning to program.
    Last edited by Adak; 05-14-2007 at 02:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  2. Dynamic two dimensional arrays
    By ThWolf in forum C++ Programming
    Replies: 14
    Last Post: 08-30-2006, 02:28 PM
  3. reading structured arrays, changing their structure
    By _kevin007 in forum C Programming
    Replies: 6
    Last Post: 05-15-2003, 03:49 PM
  4. two dimensional arrays
    By ssjnamek in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2002, 08:12 PM
  5. Two dimensional arrays
    By Jax in forum C Programming
    Replies: 1
    Last Post: 11-07-2001, 12:53 PM