From dbb4c729f3a942c3333d3356098ec3e01d81d0cb Mon Sep 17 00:00:00 2001 From: jainaman224 Date: Wed, 1 Jun 2016 19:17:58 +0530 Subject: [PATCH 1/3] added data-structures-arrays.md --- Data-Structure-Arrays.md | 82 ++++++++++++++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 11 deletions(-) diff --git a/Data-Structure-Arrays.md b/Data-Structure-Arrays.md index a3279570c..3969a832e 100644 --- a/Data-Structure-Arrays.md +++ b/Data-Structure-Arrays.md @@ -4,6 +4,58 @@ Internally, `array` is a kind of data structure that can store a fixed-size sequ `array` consists of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. + +## 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. + +``` + +#### Inserting elements to `array`: + +```cpp + +intarray[0] = 1; // Inserts an integer value of 1 at index 0 +intarray[1] = 0; // Inserts an integer value of 0 at index 1 +intarray[2] = -1; // Inserts an integer value of -1 at index 2 +intarray[3] = 1; // Inserts an integer value of 1 at index 3 + +``` + +#### Printing an `array`: + +```cpp + +std::cout << intarray[0] << std::endl; // Returns 1 which is element at index of the array +std::cout << intarray[11] << std::endl; // A random number is expected, while in reality this is `dangerous`, and is primary cause of crashes as it's accessing a memory location which does not exist. + +// To print all the elements of the array +for(int i = 0; i < n; i++) + std::cout << intarray[i] << std::endl; + +``` + +#### Basic operations on `array`: + +```cpp + +std::cout << sizeof(intarray)/sizeof(intarray[0]) << std::endl; // Returns the length of the array i.e. 10. +std::cout << sizeof(intarray[0]) << std::endl; // Returns length in bytes of one array item i.e. 4 as it is an integer + +``` + +:rocket: [Run Code](https://repl.it/CWZE/3) + + ## Arrays in Python Python doesn't have a native `array` data structure. An `array` in Python should not be confused with `list`. The major difference between a `list` @@ -12,47 +64,55 @@ and an `array` in Python is that a `list` can have different types of values whe #### Declaration of `array` ```python + from array import array intarray = array('i') # Declares an array of integer type + ``` -#### Adding elements to `array`: +#### Inserting elements to `array`: ```python -intarray.append(1) # Adds an integer value of 1 to the array -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(1) # Inserts an integer value of 1 to the array +intarray.append(0) # Inserts an integer value of 0 to the array +intarray.append(-1) # Inserts an integer value of -1 to the array +intarray.append(1) # Inserts an integer value of 1 to the array + +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 intarray.itemsize # Returns length in bytes of one array item i.e. 4 as it is an integer 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) From 37398ae4dd5147e2fdd8f94cb3497565a09b9a5d Mon Sep 17 00:00:00 2001 From: jainaman224 Date: Thu, 2 Jun 2016 18:09:20 +0530 Subject: [PATCH 2/3] Update --- Data-Structure-Arrays.md | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/Data-Structure-Arrays.md b/Data-Structure-Arrays.md index 3969a832e..9aa7db14e 100644 --- a/Data-Structure-Arrays.md +++ b/Data-Structure-Arrays.md @@ -11,51 +11,42 @@ C++ provides a data structure, `array`, which stores a fixed-size sequential col #### Declaration of `array` -```cpp - +```c++ 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. - ``` #### Inserting elements to `array`: -```cpp - +```c++ intarray[0] = 1; // Inserts an integer value of 1 at index 0 intarray[1] = 0; // Inserts an integer value of 0 at index 1 intarray[2] = -1; // Inserts an integer value of -1 at index 2 intarray[3] = 1; // Inserts an integer value of 1 at index 3 - ``` #### Printing an `array`: -```cpp - +```c++ std::cout << intarray[0] << std::endl; // Returns 1 which is element at index of the array std::cout << intarray[11] << std::endl; // A random number is expected, while in reality this is `dangerous`, and is primary cause of crashes as it's accessing a memory location which does not exist. // To print all the elements of the array for(int i = 0; i < n; i++) std::cout << intarray[i] << std::endl; - ``` #### Basic operations on `array`: -```cpp - +```c++ std::cout << sizeof(intarray)/sizeof(intarray[0]) << std::endl; // Returns the length of the array i.e. 10. std::cout << sizeof(intarray[0]) << std::endl; // Returns length in bytes of one array item i.e. 4 as it is an integer - ``` :rocket: [Run Code](https://repl.it/CWZE/3) - ## Arrays in Python Python doesn't have a native `array` data structure. An `array` in Python should not be confused with `list`. The major difference between a `list` @@ -64,16 +55,13 @@ and an `array` in Python is that a `list` can have different types of values whe #### Declaration of `array` ```python - from array import array intarray = array('i') # Declares an array of integer type - ``` #### Inserting elements to `array`: ```python - intarray.append(1) # Inserts an integer value of 1 to the array intarray.append(0) # Inserts an integer value of 0 to the array intarray.append(-1) # Inserts an integer value of -1 to the array @@ -82,13 +70,11 @@ intarray.append(1) # Inserts an integer value of 1 to the array 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`: ```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. @@ -98,13 +84,11 @@ print(intarray[3]) # Would give IndexError as there is no element at index 3 of # To print all the elements of the array for i in intarray: print(i) - ``` #### Basic operations on `array`: ```python - len(intarray) # Returns the length of the array i.e. 3 intarray.itemsize # Returns length in bytes of one array item i.e. 4 as it is an integer intarray.count(1) # Returns the number of occurrences of 1 in the array i.e. 2 @@ -112,7 +96,6 @@ intarray.insert(1, 3) # Insert a new item with value x in the array before posit 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 - ``` :rocket: [Run Code](https://repl.it/CWJB) From 30b999ff8fa4fe8ca87dec22fa7bb01a21ab0280 Mon Sep 17 00:00:00 2001 From: jainaman224 Date: Thu, 2 Jun 2016 19:57:20 +0530 Subject: [PATCH 3/3] implemented suggestion --- Data-Structure-Arrays.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Data-Structure-Arrays.md b/Data-Structure-Arrays.md index 9aa7db14e..70c10044b 100644 --- a/Data-Structure-Arrays.md +++ b/Data-Structure-Arrays.md @@ -31,7 +31,8 @@ intarray[3] = 1; // Inserts an integer value of 1 at index 3 ```c++ std::cout << intarray[0] << std::endl; // Returns 1 which is element at index of the array -std::cout << intarray[11] << std::endl; // A random number is expected, while in reality this is `dangerous`, and is primary cause of crashes as it's accessing a memory location which does not exist. +std::cout << intarray[11] << std::endl; // Would give a a "Garbage" value as there is no element at index 11 of array. +// That memory location is beyond the range of the array. // To print all the elements of the array for(int i = 0; i < n; i++)