Array in C++ and Python - Data Structures and Algorithms
Tags: cpp, python, computerscience, algorithms
Published: 2020-07-26 04:43:46

### **Introduction to Array**

> Arrays are used to store multiple values in a single variable, rather than declaring separate variables for each value. It's like containers and used to store similar type of elements as in the data type must be the same for all elements.  it's usually presented as a native data structure in many programming languages.

`array` and `list` are different data structures in a language like python.

![Array in C++ and Python- Data Structures and Algorithms](https://dev-to-uploads.s3.amazonaws.com/i/iz02xucesuv7atku4ex4.jpg)

### **Advantages of Array**

- Code Optimization (less code)
- Random Access
- Easy to traverse data
- Easy to manipulate data
- Easy to sort data etc.

### **Array in C++** 

> Arrays are one of the most important and fundamental data structures available in C++. The elements field within square brackets [], representing the number of elements in the array.

```cpp
#include <iostream>
using namespace std;

int foo [5] = {10, 8, 6, 4, 2};

void sum() {
    // it print sum of all elements
    int i, result = 0;
    
    for(i=0;i<5;i++) {
        result += foo[i];
    }
    cout << "Total sum of array : " << result << endl;
}

void updation() {
    // updation of array
    foo[1] = 20;
    cout << "Array element after updation :";
    cout << foo[0]<< " ";
    cout << foo[1]<< " ";
    cout << foo[2]<< " ";
    cout << foo[3]<< " ";
    cout << foo[4]<< " ";

}
int main() {
    sum();
    updation();
    return 0;
};
```

##### output
```bash
Total sum of array : 30
Array element after updation : 10 20 6 4 2
```
### **Array in Python**

> We can access each element of an array using the index of the element.

|Tyepcode  |  Value |
|--|--|
|b  | Represents signed integer of size 1 byte/td>|
| B | Represents unsigned integer of size 1 byte |
| c | Represents character of size 1 byte |
| i | Represents signed integer of size 2 bytes |
| I | Represents unsigned integer of size 2 bytes |
| f | Represents floating point of size 4 bytes |
| d | Represents floating point of size 8 bytes |

#### **Traverse Operation**

> Traverse operation refers to print all the array elements one by one.

```python
from array import *

array1 = array('i', [10,20,30,40,50])
# i is the typecode which Represents signed integer of size 2 

for x in array1:
 print(x)
```

##### Output

```bash
10
20
30
40
50
```

#### **Update Operation**

> Update operation refers to updating an existing element from the array at a given index.

```cpp
from array import *

arr = array('i', [10,20,30,40,50])

arr[2] = 80

for x in arr:
 print(x)
```

##### output

```bash
10
20
80
40
50
```

Python also has numpy array. It's used to work with arrays. The array object in NumPy is called ndarray . We can create a NumPy ndarray object by using the array() function.

`Note: Python array is faster than python List.`

{% post codeperfectplus/data-structure-and-algorithm-in-cpp-and-python-2jbn %}

#### **Github Repository**

{% github https://github.com/codePerfectPlus/AlgorithmsAndDataStructure no-readme %}

React ❤️ to encourage Author.