Dec 25, 2023

# Array Data Structure

Arrays are a fundamental data structure in computer science, and they are widely used in programming languages like C++. In this article, we will explore the array data structure in C++ and provide some examples to help you understand how to use arrays in your programs.

An array is a collection of elements of the same data type that are stored in contiguous memory locations. In C++, arrays are declared using the following syntax

``datatype arrayName[arraySize];``

There are two primary ways to make an array in the memory. the first one is the most familiar one which is using the stack. When you declare an array like this, you may have no idea where you are storing data. But if you are creating a local variable it is stored on the stack.

``` int numbers[5];```

The second method to make an array is allocating the memory from the heap as follows. Actually, the stack space is limited. It is optimized to use with functions. The primary purpose of the stack is to use with function calls. So if we are going to need ample memory space, the ideal area is the heap. We can allocate some memory space on the heap using the new keyword in C++.

``````

int *numbers = new int[10];

``````

## ```myArray[0] = 5; myArray[1] = 10;```

Once an array is declared, you can initialize its elements by assigning values to them. For example, to initialize the first element of `myArray` to 5 and the second element to 10, you would write:

## Accessing elements of an array

Array elements are accessed using their index, which is an integer value that indicates their position in the array. In C++, array indices start at 0, so the first element of an array has an index of 0, the second element has an index of 1, and so on. To access an element of an array, you simply use its index as follows:

hgf

## Functions and Arrays

``````int sumArray(int myArray[], int arraySize) {
int sum = 0;
for (int i = 0; i < arraySize; i++) {
sum += myArray[i];
}
return sum;
}
``````

To call this function and pass an array to it, you would write:

``````int myArray[] = {1, 2, 3, 4, 5};
int arraySize = 5;
int total = sumArray(myArray, arraySize);
``````

In this example, the `sumArray()` the function takes an array of integers called `myArray` and an integer value called `arraySize` that indicates the number of elements in the array. The function then uses a for loop to add up all the elements in the array and returns the total.

## Extending the size of an array

We saw that the size of an array is fixed. We can't extend that natively using the built-in programming language methods. So we have to use an alternative method to do it.

For example, assume that we have created an array with the size of 4 and we want to extend it o the size 10.

What we are going to do is create a new array with the size 10 and copy all elements from the previous one. So the new array will look like the extended array of the previous one. The following code does the job for us. Let's read it and see what it does.

``````int main(int argc, char const *argv[])
{
int *numbers = new int[4] = {2, 3, 5, 8};
int *temp  = new int[10];
for(int i; i < 4; i++) temp[i] = numbers[i];

delete[] numbers;
numbers = temp;
return 0;
}``````

The first line of the main function is just how we are creating our original array. Notice that its size is 4 and it holds some integers like 2, 3, 5and 8.

The next few instructions are the actual procedure of extending the array.

First, we make a new array with size 10. It is n empty array at the moment. Even the pointer's name is temp don't understand it in the wrong way that the new array we are declaring is a temporary array. It is the one we are going to use in the future. But the pointer we are making is a temporary one.

Next, we use a for loop and copy all items from the old array to the new array until the limit of the original array.

## Append an item to the array

sdf

sd

``````int main(int argc, char const *argv[])
{
int *numbers = new int[4] = {2, 3, 5, 8, 16};
int *temp  = new int[5];
for(int i; i < 4; i++) temp[i] = numbers[i];
delete[] numbers;
numbers = temp;
numbers[5] = 24;
return 0;
}``````

fs

df

sdf

## Insert a new item at an index

Let's say we want to insert the integer value 35 at index 4. So how we can do it? can we use the statement numbers[4] = 35? No, if we d it like that the current value will be overwritten. At the moment the integer with the value 7 is on the array. What we want to do is insert a new value at index 4 and extend the array. So all items after that index should be shifted to the right.

sdf

sdf

``````int main(int argc, char const *argv[])
{
int *numbers = new int[4] = {2, 3, 5, 8};
int *temp  = new int[10];
for(int i = 0; i < 4; i++) temp[i] = numbers[I];
temp[4] = 35;
for(int i = 5; i < 9; i++) temp[i + 1] = numbers[i];

delete[] numbers;
numbers = temp;
return 0;
}``````

fddgffg

dffg

## Multidimensional Arrays

``````Pros and Cons of Arrays
``````