You are here:

C++/An array inside a 2D array

Advertisement


Question

     Hi Zlatko,                 
       What I want is the set of 5 numbers entered by the user be appended to
the beginning of the array and therefore this is going to change the array
dimension from [5][5] to [6][5]
      Tell me if I wrong, because the size of the array is going to change is
desirable the user determine at runtime the size of the array. To accomplish
this
I need to declare the array using dynamic memory allocation, if so, I tried but
I did not know how to code it. This is the code I got so far. Thanks.

       1.   #include <iostream>
  2.   #include <iomanip>
  3.    
  4.   const int rows=5;
  5.   const int columns=5;
  6.    
  7.   using namespace std;
  8.    
  9.   void printHeading();
  10.    
  11.   int main()
  12.   {
  13.       cout<<endl;
  14.       char choice;
  15.       int array[rows][columns]={{2,13,23,45,50},
  16.                                                      {5,12,34,36,51},
  17.                                                      {2,23,25,46,54},
  18.                                                      {2,7,35,39,48},
  19.                                                      {3,5,26,39,49}};
  20.    
  21.       printHeading();
  22.    
  23.       for(int i=0; i<rows; i++)
  24.       {
  25.           cout<<setw(33)<< i <<")";
  26.           for(int j=0; j<columns; j++)
  27.           {
  28.               cout<<setw(8)<<array[i][j];
  29.           }
  30.           cout<<endl;
  31.       }
  32.       do
  33.       {
  34.           cout<<"
    Enter 5 numbers: ";
  35.    
  36.           for(int i=0; i<1; i++)
  37.           {
  38.               for(int j=0; j<columns; j++)
  39.               {
  40.                  cin>>array[i][j];
  41.               }
  42.           }
  43.           cout<<endl;
  44.           printHeading();
  45.           for(int i=0; i<rows; i++)
  46.           {
  47.               cout<<setw(33)<< i <<")";
  48.               for(int j=0; j<columns; j++)
  49.               {
  50.                   cout<<setw(8)<<array[i][j];
  51.               }
  52.               cout<<endl;
  53.            }
  54.       cout<<"

    Would you like to try again? (y/n): ";
  55.       cin >> choice;
  56.    
  57.       }  while (choice == 'y'|| choice == 'Y');
  58.       cout<<endl;
  59.       return 0;
  60.   }
  61.    
  62.   void printHeading()
  63.   {
  64.   
    cout<<setw(82)<<"==================================
===================

";
  65.       cout<<setw(76)<<"ROWS     (0)     (1)     (2)     (3)     (4)
";
  66.       cout<<setw(81)<<"------------------------------------
-----------------
";
  67.   }
  68.    

Answer
Hello Raul

You have two options here. One option is to create your 2D array with as many rows as you will need during the run of the program. So, create the array with 6 or more rows and keep track of how many rows are filled with data. The programming for this is simple, but not flexible, because your array cannot grow beyond the preset bounds.

Another option is to allocate memory dynamically. This allows the array size to change at runtime. If the array grows many times during the run of the program, then it is common to allocate more rows than needed, so that time is not wasted in the memory manager. Whenever the program reallocates memory, it would have to copy elements from the original memory to the new memory. The programming for this is more complicated, especially because you lose the 2 dimensional quality of the array, and you need to calculate where in memory an element is based on the 2 indexes and the array width. For example, if you have an integer pointer
int *p;
representing the 2D array with R rows and C columns, and you want element [i][j], that element will be located at
p + i*C+j
If choosing this option, it is good to encapsulate the array into an array class, and overload the [] operator. That allows your program to keep using the array[i][j] syntax.

Such a class would contain the following data:
int* pArray; // pointer to the memory
int rowCount; // count of the number of rows in the array
int colCount; // count of the number of columns in the array

And the following functions to access elements and resize the array
int& elementAt(int i, int j) { return *(pArray + i * colCount + j); }
void resize(int newRowCount); // resizes the array
Overriding operator[] is more complicated, so I suggest you use the elementAt access function for now. I could help you build such a class if you like.

Regardless of which option you choose, if you want to add elements to the start of the array, you will need to first shift all other elements down.

I hope that helps you out.

Best regards
Zlatko

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.