Redeclaring An Array To Change The Number Of Elements

This is a discussion on Redeclaring An Array To Change The Number Of Elements within the C++ Programming forums, part of the General Programming Boards category; I was wondering if it was possible to change (add or subtract) the elements in a multi-dimensional array, without using ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    5

    Redeclaring An Array To Change The Number Of Elements

    I was wondering if it was possible to change (add or subtract) the elements in a multi-dimensional array, without using the new tag. I also can't change the array's name.
    For example, I want to change:
    Code:
     int array[10][10]
    to something like:
    Code:
     int array[13][12]
    Or:
    Code:
     int array [2][7]

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Once an array is created you can't change it's size. What you can do however is use a vector which will grow and shrink as you need

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    You'll have to grab some more memory from somewhere if one or both of the array bounds are increasing. Using new (or malloc/realloc/calloc) is the only way I know to do this.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    expanding on what pianorain just said, consider using pointers. in C++, you can declare a pointer to an array, and then deallocate that array and reallocate a bigger or smaller array. for example, consider this code and tailor it to your needs:
    Code:
    #include<iostream>
    
    int main()
    {
      int*arr;  //an integer pointer
      int count;  //the amount of integers
    
      do
      {
        std::cout<<"Enter the amount of values: ";
        std::cin>>count;
        std::cout<<"Enter the values:\n";
        
        arr=new int[count];   //create (allocate) a new array of size count
    
        for(int i=0;i<count;++i)   //loop through the entire array
        { 
          std::cout<<"> ";  //prompt for input
          std::cin>>arr[i];  //recieve input
        }
    
        for(int i=0;arr[i];++i)      //loop through the actual array
          std::cout<<arr[i]<<", ";  //output everything in the array
          
        std::cout<<std::endl;         //nice formatting
      
        delete[]arr;  //deallocate the memory for the array
        
      }while(count>0);
      return 0;
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    So, how would that work with a multi-dimensional array? When I tried, I got a compiler error.

    Code:
    cannot convert `TILE (*)[((rows - 1) + 1)]' to `TILE*' in  assignment
    I have it declared like:
    Code:
    typedef struct TILE
    {
        int tile;
        int collision;
    }TILE;
    
    int columns = 25;           // Number Of Columns
    int rows = 19;                // Number Of Rows
    TILE *map;                    // The Map
    
    map = new TILE[columns][rows];
    
    (all that junk in main...)
    
    delete []map;
    What is it that I'm doing wrong.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    map = new TILE[columns][rows];

    you are assining a 2 dimensional array to a one dimensional one.

    declare map as:
    TILE *map[];
    signature under construction

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Here's a quick, untested, schema to change size of an array dynamically. Considering all the places where mishaps can occur, you should do something like this only if you don't know how or aren't allowed to use containers in STL.
    Code:
    //declare map
    Tile** map;
     
    //declare memory for map
    map = new Tile*[rows];
    for(i = 0; i < rows; ++i)
       map[i] = new Tile[columns];
     
    //fill map here
     
    //decide to expand maps size here
     
    //declare tempMap to hold contents of map pending changes in map. 
    Tile ** tempMap;
     
     
    //declare memory for tempMap as above for map.  Use same rows and columns.
     
    //copy data in map to tempMap.  I use loops, but there may be other ways
    for(i = 0; i < rows; ++i)
      for(j = 0; j < columns; ++j)
    	 tempMap[i][j] = map[i][j];
     
    //delete memory in map
    for(i = 0; i < rows; ++i)
       delete map[i];
    delete map[];
     
    //determine new size for rows and columns of map
     
    //declare new memory for map
    map = new Tile*[newRows];
    for(i = 0; i < newRows; ++i)
       map[i] = new Tile[newColumns];
     
    //copy tempMap back into map
    for(i = 0; i < rows; ++i)
      for(j = 0; j < columns; ++j)
    	 map[i][j] = tempMap[i][j];
     
    //delete memory for tempMap like above for map
     
    NOTE:  be sure that the data in the old map will fit in the size of the new map to prevent overwrite errors
    You're only born perfect.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Raven Arkadon
    map = new TILE[columns][rows];

    you are assining a 2 dimensional array to a one dimensional one.

    declare map as:
    TILE *map[];
    What compiler are you using?

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    oops...
    that only works with pointers to fixed size arrays..
    e.g.
    TILE (*blah)[5] = new TILE[4][5];

    sry
    signature under construction

  10. #10
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    Quote Originally Posted by elad
    Here's a quick, untested, schema to change size of an array dynamically. Considering all the places where mishaps can occur, you should do something like this only if you don't know how or aren't allowed to use containers in STL.
    Code:
    //declare map
    Tile** map;
     
    //declare memory for map
    map = new Tile*[rows];
    for(i = 0; i < rows; ++i)
       map[i] = new Tile[columns];
     
    //fill map here
     
    //decide to expand maps size here
     
    //declare tempMap to hold contents of map pending changes in map. 
    Tile ** tempMap;
     
     
    //declare memory for tempMap as above for map.  Use same rows and columns.
     
    //copy data in map to tempMap.  I use loops, but there may be other ways
    for(i = 0; i < rows; ++i)
      for(j = 0; j < columns; ++j)
    	 tempMap[i][j] = map[i][j];
     
    //delete memory in map
    for(i = 0; i < rows; ++i)
       delete map[i];
    delete map[];
     
    //determine new size for rows and columns of map
     
    //declare new memory for map
    map = new Tile*[newRows];
    for(i = 0; i < newRows; ++i)
       map[i] = new Tile[newColumns];
     
    //copy tempMap back into map
    for(i = 0; i < rows; ++i)
      for(j = 0; j < columns; ++j)
    	 map[i][j] = tempMap[i][j];
     
    //delete memory for tempMap like above for map
     
    NOTE:  be sure that the data in the old map will fit in the size of the new map to prevent overwrite errors
    Thank you so very much. It's working now. However, I'm curious to know why
    Code:
    TILE **map;
    has two asterisks (*) in it. I know that it needs them, but why.

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Well, it's a pointer to a pointer. Since depending on what a pointer points to, it can be used as an array, in this case both pointer-levels act as arrays, so it's an array of arrays, i.e. a 2D array.

    Err ok, so that doesn't make so much sense. Let me try again:
    Code:
    TILE *map;  //TILE pointer called 'map'
    map = new TILE[5];  //allocate an array of 5 tiles and assign to 'map'
    //So now 'map' refers to a dynamic array of TILE
    
    TILE **2Dmap;  //Pointer to a TILE pointer, called '2Dmap'
    2Dmap = new TILE*[10];  //Allocate an array of 10 tile pointers, assign to '2Dmap'
    //So now '2Dmap' refers to a dynamic array of TILE pointers
    
    for(int i = 0; i < 10; ++i)
       2Dmap[i] = new TILE[5];  //Allocate an array of 5 tiles, assign to this element of '2Dmap'
    
    //So now each TILE pointer in '2Dmap' refers to an array of 4 tiles.
    //In other words, we have made '2Dmap' into a "2D array" of TILE.
    It isn't really a 2D array, but it looks and acts like one
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number array
    By matt_570 in forum C++ Programming
    Replies: 12
    Last Post: 11-13-2008, 04:44 PM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 01:56 PM
  3. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 08:01 PM
  4. escape & array elements
    By FrawgLips in forum C Programming
    Replies: 1
    Last Post: 11-18-2002, 01:03 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21