DEV Community 👩‍💻👨‍💻

DEV Community 👩‍💻👨‍💻 is a community of 963,274 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for Array in C++ and Python - Data Structures and Algorithms
Deepak Raj
Deepak Raj

Posted on • Updated on

Array in C++ and Python - Data Structures and Algorithms

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

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.

#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;
};
Enter fullscreen mode Exit fullscreen mode
output
Total sum of array : 30
Array element after updation : 10 20 6 4 2
Enter fullscreen mode Exit fullscreen mode

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.

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)
Enter fullscreen mode Exit fullscreen mode
Output
10
20
30
40
50
Enter fullscreen mode Exit fullscreen mode

Update Operation

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

from array import *

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

arr[2] = 80

for x in arr:
 print(x)
Enter fullscreen mode Exit fullscreen mode
output
10
20
80
40
50
Enter fullscreen mode Exit fullscreen mode

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.

Github Repository

GitHub logo codePerfectPlus / AlgorithmsAndDataStructure

Algorithms And DataStructure Implemented In Python & CPP, Give a Star 🌟If it helps you

More Articles by Author

Join for Weekly Updates.

React ❤️ to encourage Author.

Top comments (0)

Update Your DEV Experience Level:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. 🛠