How can you create a 2D array,say, arr[][]
with 5 rows and each row has a variable number of columns in it?
possibly arr[5][]
with 1st row arr[0][]
with 4 columns
2nd row arr[1][]
with 5 columns and so on?
I wouldn't mind a STL vector solution but I don't know vectors very well yet.
We can pass the 2D array as an argument to the function in C in two ways; by passing the entire array as an argument, or passing the array as a dynamic pointer to the function.
To declare a 2D array, specify the type of elements that will be stored in the array, then ( [][] ) to show that it is a 2D array of that type, then at least one space, and then a name for the array. Note that the declarations below just name the variable and say what type of array it will reference.
You can also use an array initializer to declare, create and initialize a two-dimensional array. FIGURE 7.2 A two-dimensional array is a one-dimensional array in which each element is another one-dimensional array. Each row in a two-dimensional array is itself an array. Thus, the rows can have different lengths.
The length of a 2D array is the number of rows it has. The row index runs from 0 to length-1. Each row of a 2D array can have a different number of cells, so each row has its own length : uneven[0] refers to row 0 of the array, uneven[1] refers to row 1, and so on.
With C++11, you can do it easily with vectors (line breakes added for readability):
std::vector< std::vector <int > > arr = {
{1,2,3},
{4,5},
{6,7,8,9,0}
};
If you don't have a C++11 compiler, it works the exact same way, but you will not be able to initialize them as easy. You can set elements individually:
std::vector< std::vector <int > > arr;//vector of vectors. Think of each element as of a "row"
std::vector<int> sub;//a temporary "row"
sub.push_back(1);
sub.push_back(2);
arr.push_back(sub);//Adding a "row" to the vector
sub.clear();//Making another one
sub.push_back(1);
sub.push_back(12);
sub.push_back(54);
arr.push_back(sub);//Adding another "row" to the vector
Or you can initialize each "row" with an ordinary array:
std::vector< std::vector <int > > arr;
static const int arr[] = {1,2,3,4};//A "row" as an ordinary array
vector<int> vec (arr, arr + sizeof(arr) / sizeof(arr[0]) ); //Setting a "Row" as a vector
arr.push_back(vec);//Adding the "row" to the vector of vectors.
It's not exactly possible to do what you want with ordinary arrays, since when you make an array[X][Y]
, it automaticaly is an X*Y
matrix. You could, however, use an array of pointers:
int * array[3];
//also possible: int ** array = new int*[3]; but don't forget to delete it afterwards.
int sub1[3] = {1,2,3};
int sub2[2] = {1,2};
int sub3[4] = {1,2,3,4};
array[0] = sub1;
array[1] = sub2;
array[2] = sub3;
and access elements with array[X][Y]
. However, the vector solution is much better overall.
You can do it like this (assuming an array of int
elements):
int** arr = new int*[5];
for(size_t i = 0; i < 5; ++i)
{
arr[i] = new int[4];
}
and this gives you a two-dimensional dynamically allocated array of 5 by 4. You can then use it like this: arr[i][j] = 15;
Do not forget to de-allocate the memory after you are done using the array:
for(size_t i = 0; i < 5; ++i)
{
delete[] arr[i];
}
delete[] arr;
I would recommend using std::vector
, however. You can see the other answers for reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With