Skip to content

GH-199: Array in C++ #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions concepts/cpp/array/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Array in C++

In computer science, an `array` is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key. An array is stored such that the position of each element can be computed from its index tuple by a mathematical formula.

## Time Complexity Analysis of Array

| Operation | Creation | Insertion | Accessing | Update | Deletion | Searching |
|-----------------|----------|------------------|-----------|--------|------------------|-----------|
| Time Complexity | $O(1)$ | $O(1)$ or $O(n)$ | $O(1)$ | $O(n)$ | $O(1)$ or $O(n)$ | $O(n)$ |

## Array implementation in C++

## Basic Operations


```c++
#include <iostream>

using namespace std;
```

**Initialization**: Create an array with given values


```c++
int a[5] = {1, 2, 3, 4, 5};
a
```




{ 1, 2, 3, 4, 5 }



**Accessing Elements**: Access element by index (zero-based)


```c++
// Access elements in array
cout << "a[0] = " << a[0] << endl;
cout << "a[2] = " << a[2] << endl;
```

a[0] = 1
a[2] = 3


**Updating Elements**: Update an element by its index


```c++
a[0] = 100;
cout << "a[0] = " << a[0];
```

a[0] = 100

**Input**: Read values into the array from a stream

Using fixed array

```cpp
int N;
int numbers[N];
for(int i=0; i<N; i++){
cin >> numbers[i];
}
```

Using vector

```cpp
#include <vector>

int N;
vector<int> numbers(N);
for(int i=0; i<N; i++){
cin >> numbers[i];
}
```

## 🔗 Further Reading

* [Arrays](https://cplusplus.com/doc/tutorial/arrays/), cplusplus.com
* ▶️ [Dynamic and Static Arrays](https://www.youtube.com/watch?v=PEnFFiQe1pM&list=PLDV1Zeh2NRsB6SWUrDFW2RmDotAfPbeHu&index=4&ab_channel=WilliamFiset), WilliamFiset
* ▶️ [CS50 2020 - Arrays](https://youtu.be/tI_tIZFyKBw?t=3834), CS50

197 changes: 197 additions & 0 deletions concepts/cpp/array/notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Array in C++\n",
"\n",
"In computer science, an `array` is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key. An array is stored such that the position of each element can be computed from its index tuple by a mathematical formula.\n",
"\n",
"## Time Complexity Analysis of Array\n",
"\n",
"| Operation | Creation | Insertion | Accessing | Update | Deletion | Searching |\n",
"|-----------------|----------|------------------|-----------|--------|------------------|-----------|\n",
"| Time Complexity | $O(1)$ | $O(1)$ or $O(n)$ | $O(1)$ | $O(n)$ | $O(1)$ or $O(n)$ | $O(n)$ |"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Array implementation in C++"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Basic Operations"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"vscode": {
"languageId": "cpp"
}
},
"outputs": [],
"source": [
"#include <iostream>\n",
"\n",
"using namespace std;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Initialization**: Create an array with given values"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"vscode": {
"languageId": "cpp"
}
},
"outputs": [
{
"data": {
"text/plain": [
"{ 1, 2, 3, 4, 5 }"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"int a[5] = {1, 2, 3, 4, 5};\n",
"a"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Accessing Elements**: Access element by index (zero-based)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"vscode": {
"languageId": "cpp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a[0] = 1\n",
"a[2] = 3\n"
]
}
],
"source": [
"// Access elements in array\n",
"cout << \"a[0] = \" << a[0] << endl;\n",
"cout << \"a[2] = \" << a[2] << endl;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Updating Elements**: Update an element by its index"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"vscode": {
"languageId": "cpp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a[0] = 100"
]
}
],
"source": [
"a[0] = 100;\n",
"cout << \"a[0] = \" << a[0];"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Input**: Read values into the array from a stream\n",
"\n",
"Using fixed array \n",
"\n",
"```cpp\n",
"int N;\n",
"int numbers[N];\n",
"for(int i=0; i<N; i++){\n",
" cin >> numbers[i];\n",
"}\n",
"```\n",
"\n",
"Using vector\n",
"\n",
"```cpp\n",
"#include <vector>\n",
"\n",
"int N;\n",
"vector<int> numbers(N);\n",
"for(int i=0; i<N; i++){\n",
" cin >> numbers[i];\n",
"}\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 🔗 Further Reading\n",
"\n",
"* [Arrays](https://cplusplus.com/doc/tutorial/arrays/), cplusplus.com\n",
"* ▶️ [Dynamic and Static Arrays](https://www.youtube.com/watch?v=PEnFFiQe1pM&list=PLDV1Zeh2NRsB6SWUrDFW2RmDotAfPbeHu&index=4&ab_channel=WilliamFiset), WilliamFiset\n",
"* ▶️ [CS50 2020 - Arrays](https://youtu.be/tI_tIZFyKBw?t=3834), CS50\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "C++17",
"language": "C++17",
"name": "xcpp17"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".cpp",
"mimetype": "text/x-c++src",
"name": "c++",
"version": "17"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
20 changes: 16 additions & 4 deletions concepts/general/array/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Array

*See implementation in* C++, Java, [Python](/concepts/python/array/README.md), [Typescript](/concepts/typescript/array/README.md)
*See implementation in* [C++](/concepts/cpp/array/README.md), Java, [Python](/concepts/python/array/README.md), [Typescript](/concepts/typescript/array/README.md)

An array is a data structure that contains a group of elements. Each element can be accessed using an index. The index of the first element is 0, and the index of the last element is the length of the array minus 1.

Expand All @@ -13,9 +13,21 @@ In competive programming, arrays are often used to store a list of values. For e

## Array Operations

* `array.length`: Returns the number of elements in the array.
* `array[index]`: Returns the element at the given index.
* `array[index] = value`: Sets the element at the given index to the given value.
Here are some basic operations that you can perform on arrays:

* Initialization: Create an array with given values
* Accessing Elements: Access an element by its index (zero-based)
* Updating Elements: Update an element by its index
* Traversal: Visit all elements of the array
* Input: Read values into the array from a stream
* Size: Get the number of elements in the array

More operations:

* Delete an element in array
* Insert an element in array
* Searching an element in array (*see searching algorithms*)
* Sorting an array (*see sorting algorithms*)

## 🔗 Further Reading

Expand Down
2 changes: 1 addition & 1 deletion readme/cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ A data structure is a data organization, management, and storage format that is
<ul>
<li>
<code>B</code>
Array
<a href="/concepts/cpp/array/README.md">Array</a>
</li>
<li>
<code>B</code> String
Expand Down