Thread: problem with array

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    8

    problem with array

    how can i organize one dimensional array of data into multidimensional array.
    for example:-
    1
    2
    1
    2
    1
    2
    .
    .
    .

    to

    1 2 1 2
    1 2 1 2
    ....

    i'm using C++ Builder.
    thanks..

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
        int a[5][5] = 
        {
            {1, 2, 3, 4, 5},
            {1, 2, 3, 4, 5},
            {1, 2, 3, 4, 5},
            {1, 2, 3, 4, 5},
            {1, 2, 3, 4, 5},
        };
    gg

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    This may not be entirely correct, but something along these lines:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int oned[9] = { 25, 10, 25, 10, 25, 10, 25, 10, 25 };
    	int twod[3][3];
    	int i, x = 0, y = 0;
    
    	for ( i = 0; i < 9; i++ ) {
    		twod[x][y] = oned[i];
    		x ++;
    		if (x == 3) {
    			x = 0;
    			y ++;
    		}
    	}
    	return 0;
    }
    edit: this is in c, I didn't realise it was the c++ board. oh well.
    Last edited by Brian; 11-02-2003 at 05:10 PM.

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    8
    but...if i want to make it as 400x400? since i have a large set of data...

  5. #5
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int oned[160000] = { ... };
    	int twod[400][400];
    	long i;
    	int x = 0, y = 0;
    
    	for ( i = 0; i < 160000; i++ ) {
    		twod[x][y] = oned[i];
    		x ++;
    		if (x == 400) {
    			x = 0;
    			y ++;
    		}
    	}
    
    	return 0;
    }
    Last edited by Brian; 11-02-2003 at 05:23 PM.

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    8
    thanks for your help brian...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM