Skip to content
This repository has been archived by the owner on Apr 1, 2021. It is now read-only.

Commit

Permalink
Added cpp implemention of array
Browse files Browse the repository at this point in the history
  • Loading branch information
jainaman224 committed May 29, 2016
1 parent c521e04 commit 878b69e
Showing 1 changed file with 52 additions and 6 deletions.
58 changes: 52 additions & 6 deletions Data-Structure-Arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,26 @@ intarray.append(0) # Adds an integer value of 0 to the array
intarray.append(-1) # Adds an integer value of -1 to the array
intarray.append(1) # Again adds an integer value of 1 to the array

intarray.append('d') # Would give a TypeError as the array is of integer type.
intarray.append('d') # Would give a TypeError as the array is of integer type.

#Resolve the above error and then move ahead.
```

#### Printing an `array`:
#### Printing an `array`:

```python
print(intarray) # Returns array('i', [1, 4, -1])
print(intarray[0]) # Returns 1 which is the element at index 0 of the array
print(intarray[3]) # Would give IndexError as there is no element at index 3 of array.
print(intarray[3]) # Would give IndexError as there is no element at index 3 of array.

#Resolve the above error and then move ahead.

# To print all the elements of the array
for i in intarray:
print(i)
```

#### Basic operations on `array`:
#### Basic operations on `array`:

```python
len(intarray) # Returns the length of the array i.e. 3
Expand All @@ -52,9 +52,55 @@ intarray.count(1) # Returns the number of occurrences of 1 in the array i.e. 2
intarray.insert(1, 3) # Insert a new item with value x in the array before position i
intarray.remove(1) # Remove the first occurrence of 1 from the array
intarray.reverse() # Reverse the order of the items in the array
intarray.pop(1) # Removes the item with the index 1 from the array and returns it
intarray.pop(1) # Removes the item with the index 1 from the array and returns it
```

:rocket: [Run Code](https://repl.it/CWJB)

[Official Docs](https://docs.python.org/3.5/library/array.html)

## Arrays in C++

C++ provides a data structure, `array`, which stores a fixed-size sequential collection of elements of the same data-type. An `array` is used to store a collection of data, but it is better to think of an `array` as a collection of variables of the same type.

#### Declaration of `array`

```cpp
int intarray[10]; // Declares an array of integer type of size 10 with elements having random values. Index ranges from 0 to 9(i.e. size-1).
int intarray[10] = { 0 }; // Declares an array of integer of size 10 with all elements having value 0

// Choose one the two declarations and then move ahead.
```
#### Adding elements to `array`:
```cpp
intarray[0] = 1; // Adds an integer value of 1 at index 0
intarray[1] = 0; // Adds an integer value of 0 at index 1
intarray[2] = -1; // Adds an integer value of -1 at index 2
intarray[3] = 1; // Again adds an integer value of 1 at index 3
intarray[4] = "dd"; // Would give a TypeError as the array is of integer type.
// Resolve the above error and then move ahead.
```

#### Printing an `array`:

```cpp
cout << intarray[0]; // Returns 1 which is element at index of the array
cout << intarray[11]; // Would give a random value as there is no element at index 11 of array.

// To print all the elements of the array
for(int i = 0; i < n; i++)
cout << intarray[i];
```

#### Basic operations on `array`:

```cpp
cout << sizeof(intarray)/sizeof(intarray[0]); // Returns the length of the array i.e. 10.
cout << sizeof(intarray[0]); // Returns length in bytes of one array item i.e. 4 as it is an integer
```

:rocket: [Run Code](https://repl.it/CWZE)

0 comments on commit 878b69e

Please sign in to comment.