Article on Arrays and dynamic arrays in C++

In this post, I will discuss arrays that are static in size and the arrays whose size change based on the size in the runtime. They are called dynamic arrays.

What is an array?

An array is a sequence of elements that are stored in contiguous memory locations. We can access other elements of the array when the first index is known. By adding one to each location we get the next one.

Arrays in any programming language
numArray : Array

As we can see in the example, The index starts at 0. We can access the arrays sequentially or by adding the start number to the element position we want to access. I listed different ways we can declare/initialize an array below. The ways we can access an item in an array is also shown below.

void arrays()
{
	int numArray[6] = {10,20,30,40,50};
	float decimalArr[] = {0.99, 0.86, 0.78};
	string strArr[]{"Hello", "Today","is a ","nice day"};
	int amount[9]; //create an array for 9 elements without values 
	int first = numArray[0];
	cout << "First Number " << first << ", numArray[0] " << numArray[0] << endl;
	int i = 0; 
	cout << "Third number " << numArray[i + 2] << endl;
}
/********OUTPUT************/
First Number 10, numArray[0] 10
Third number 30

Multi-dimensional arrays

Multi-dimensional array is an array of arrays. I have an example of how a 2-D and a 3D array looks like.

Row/col->01
010 20
13040
2- D array table1
Row/Col01
01525
13545
First 2-D array table2
Row/Col01
067988
19002000
2nd 2-D array table2

In table1, the array is a 2-dimensional array with 2 rows and 2 columns. Whereas in table2, it is a 3-Dimensional array with 2 of them each having 2 rows and 2 columns. As we declare more numbers by adding dimensions the size increases exponentially. These work similar to 1-D array. I shall leave this exercise to the user to work more on this and create new code samples for multi dimensional arrays.

Dynamic Arrays

As the word ‘dynamic’ refers to run-time. It means that C++ allows users to allocate size of the array at run-time. I have an example for you to demonstrate how we can use this feature.

void dynamicArrays()
{
	int n;
	cout << "Enter the size of array" << endl; 

	cin >> n;
	int* newArray = new int(n);
	cout << "Enter  items" << endl;
	for (auto i = 0; i < n; i++) {
		cin >> newArray[i];
	}
	cout << "You entered: " << endl; 
	for (auto y = 0; y < n; y++)
	{
		cout << newArray[y] << endl;
	}
}

Note that, I used the keyword called “new” to allocate memory. “new” is used to allocate memory from the heap. It can be used for any data type. It returns a pointer to the memory location which has been allocated to the array with the variable name.

These are very useful when we don’t know the size of the array at compile time.

Conclusion

We learned about arrays in C++. Arrays are important data structure that is used frequently in the code. I will hop on to the next topic which is about functions and parameters.

You like my posts? Subscribe here to download eBooks and pdf’s for these tutorials.

Go back to the tutorial overview?

Prev: Pointers

Next : Functions and Parameters