#0 Hello World of DATA STRUCTURES
Tags: cpp, datastructures, codenewbie, arrays
Published: 2020-12-14 10:11:23

According to [wikipedia](https://en.wikipedia.org/wiki/Data_structure),
> In computer science, a data structure is a data organization, management, and storage format that enables efficient access and modification.

In simple terms, it is a way of writing data which helps to manage, organise or modify data.

# Arrays
- Arrays are the most basic data structures. 
- Elements in array are stored in contiguous memory location.
- Elements can be accessed with the help of their index.

We will be implementing our code in C++. Since logic is same for most of the languages you will only need to change the syntax to implement it in any other language.

**Advantages of Arrays**
- Easy and random access to array elements.
- Sorting of data can be done easily and in less code.
- Traversal through array is easy.

**Disadvantages of Arrays**
- Array size once specified cannot be changed.
- Insertion and deletion is hectic as we have to move the elements to manage the continuous memory indexing.

**Array declaration in C++**

```
// Array declaration by specifying size
int arr1[5];

// Array declaration by specifying user defined size
int s = 5;
char arr2[s];

// Array declaration by initializing elements
float arr3[] = { 11.2, 34.5, 89.5, 32.09}

// above is same as "float arr3[4] = { 11.2, 34.5, 89.5, 32.09}"

```
**Accept elements from user in Array**

```
int size, arr[size]; 

cout<<"Enter size of array: ";
cin>>size;    //prompt user to enter size of array

for (int i = 0; i < size; i++){
    cin>>arr[i];
}

```

**Insert at beginning of Array**
```
void insertAtBeginning(int arr[], int size, int beg){
    if (isFull()){
        cout<<"Array is Full!";
    }
    
    else if (isEmpty()){
        arr[0] =  beg;
    }
    else{
        int arrSize = sizeof(arr[0])/sizeof(arr);
        for (int i = 0; i < arrSize; i++){
            arr[arrSize] = arr[arrSize - 1];
        }
        
        arr[0] = beg;
    }
}
```
**NOTE:** Henceforth, only the algorithm will be discussed and it is expected that reader implements the algorithm by writing his own code.

**Insert at position in Array**
> 1. First get the element to be inserted.
> 2. Then get the position at which this element is to be inserted.
> 3. Then shift the array elements from this position to one position forward, and do this for all the other elements next to pos.
> 4. Insert the element at the given position like `int[pos] = element`
> 5. Hope your element is inserted at proper position.

**Insert at End**
> Oh c'mon... I know you can do it without the algorithm.

**Delete the element from the Array**
You can delete element either by getting it's position or by getting it's value
<u>Delete element by Position</u>

> 1. Get the position of element to be deleted. 
> 2. Loop through the array from the position to end and moving each element one index down. 

**NOTE:** Various testcases are ignored and are expected to be done by the reader.

<u>Delete element by Value</u>

> 1. Get the value to be deleted.
> 2. Loop through the array and search for the value. Remember to keep track of count/position.
> 3. Once element is found, break the loop.
> 4. Loop through the array from the position to end and moving each element one index down.

**Update the value in Array**

Dude...!! Seriously?!!
Just `arr[pos] = newValue` AS SIMPLE AS THAT.
 
**Here are some problems to practice**
- Write a C++ program to find the second lowest and highest numbers in a given array.
- Write a C++ program to arrange the numbers of a given array in a way that the sum of some numbers equal the largest number in the array.
- Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.
-  Write a C++ program to find and print all common elements in three sorted arrays of integers.

<u> Find more at <strong>[W3Resource](https://www.w3resource.com/cpp-exercises/array/index.php)</strong> </u>

Enough theory now.. Just go around and mess with the stuff.
**KEEP PRACTISING. STAY SAFE AND KEEP CODING!!**


### References
1. https://en.wikipedia.org/wiki/Data_structure
2. https://en.wikipedia.org/wiki/Array_data_structure
3. https://www.geeksforgeeks.org/arrays-in-c-cpp/
4. https://www.w3resource.com/cpp-exercises/array/index.php