|
| 1 | +# Arrays and Vectors |
| 2 | + |
| 3 | +## What is an Array? |
| 4 | + |
| 5 | +- Compound data type or data structure |
| 6 | + - Collection of elements |
| 7 | +- All elements are of the same type |
| 8 | +- Each element can be accessed directly |
| 9 | + |
| 10 | +### Characterisitcs |
| 11 | + |
| 12 | +- Fixed size |
| 13 | +- Elements are all the same type |
| 14 | +- Stored contiguously in memeory |
| 15 | +- Individual elements can be accessed by their position or index |
| 16 | + - First element is at `index 0` |
| 17 | + - Last element is at `index size-1` |
| 18 | +- No checking to see if you are out of bounds |
| 19 | +- Always initialize arrays |
| 20 | +- Very efficient |
| 21 | +- Iteration (looping) is often used to process |
| 22 | + |
| 23 | +## Declaring and Initalizing Arrays |
| 24 | + |
| 25 | +`Element_Type array_name [constant number of elements] {init list};` |
| 26 | + |
| 27 | +```c++ |
| 28 | +int test_scores [5] {100, 95, 99, 87, 88}; // 5 integers |
| 29 | + |
| 30 | +int high_score_per_level [10] {3, 5}; // init to 3, 5 and remaining to 0 |
| 31 | + |
| 32 | +const int days_in_year {365}; |
| 33 | +double hight_temperatures [days_in_year] {0}; // init all to zero |
| 34 | + |
| 35 | +int another_array [] {1, 2, 3, 4, 5}; // size automatically calculated |
| 36 | +``` |
| 37 | +
|
| 38 | +## Accessing and Modifying Array Elements |
| 39 | +
|
| 40 | +### Accessing array elements |
| 41 | +
|
| 42 | +`array_name [element_index]` |
| 43 | +
|
| 44 | +```c++ |
| 45 | +int test_scores [5] {100, 95, 99, 87, 88}; |
| 46 | +
|
| 47 | +cout << "First score at index 0:" << test_scores[0] << endl; |
| 48 | +cout << "Second score at index 1:" << test_scores[1] << endl; |
| 49 | +cout << "Third score at index 2:" << test_scores[2] << endl; |
| 50 | +cout << "Fourth score at index 3:" << test_scores[3] << endl; |
| 51 | +cout << "Fifth score at index 4:" << test_scores[4] << endl; |
| 52 | +
|
| 53 | +// output |
| 54 | +First score at index 0: 100 |
| 55 | +Second score at index 1: 95 |
| 56 | +Third score at index 2: 99 |
| 57 | +Fourth score at index 3: 87 |
| 58 | +Fifth score at index 4: 88 |
| 59 | +``` |
| 60 | + |
| 61 | +### Changing the contents of array elements |
| 62 | + |
| 63 | +`array_name [element_index]` |
| 64 | + |
| 65 | +```c++ |
| 66 | +int test_scores [5] {100, 95, 99, 87, 88}; |
| 67 | + |
| 68 | +cin >> test_scores [0]; |
| 69 | +cin >> test_scores [1]; |
| 70 | +cin >> test_scores [2]; |
| 71 | +cin >> test_scores [3]; |
| 72 | +cin >> test_scores [4]; |
| 73 | + |
| 74 | +// assignment statment |
| 75 | +test_scores[0] = 90; |
| 76 | +``` |
| 77 | +
|
| 78 | +### How does Arrays work? |
| 79 | +
|
| 80 | +- The name of the array represent the location of the first element in the array (`index 0`) |
| 81 | +- The `[index]` represents the offset from the beginning of the array |
| 82 | +- C++ simply performs a calculation to find the correct element |
| 83 | +- Remember - no bounds checking! |
| 84 | +
|
| 85 | +
|
| 86 | +## Multidimensional Arrays |
| 87 | +
|
| 88 | +### Declaring multi-dimensional arrays |
| 89 | +
|
| 90 | +`Element_Type array_name [dim1_size][dim2_size]` |
| 91 | +
|
| 92 | +```c++ |
| 93 | +int movie_rating [3][4]; // 3 rows, 4 columns |
| 94 | +``` |
| 95 | + |
| 96 | +| 0 | 1 | 2 | 3 | |
| 97 | +| --- | --- | --- | --- | |
| 98 | +| 0 | 0 | 4 | 3 | 5 | |
| 99 | +| 1 | 2 | 3 | 3 | 5 | |
| 100 | +| 2 | 1 | 4 | 4 | 5 | |
| 101 | + |
| 102 | +```c++ |
| 103 | +const int rows {3}; |
| 104 | +const int cols {4}; |
| 105 | +int movie_rating [rows][cols]; |
| 106 | +``` |
| 107 | +
|
| 108 | +### Accessing array elements in multi-dimensional arrays |
| 109 | +
|
| 110 | +```c++ |
| 111 | +cin >> movie_rating [1][2]; |
| 112 | +cout << movie_rating [1][2]; |
| 113 | +
|
| 114 | +// output |
| 115 | +5 |
| 116 | +``` |
| 117 | + |
| 118 | +### Initializing multi-dimensionaly arrays |
| 119 | + |
| 120 | +```c++ |
| 121 | +int movie_rating [3][4] |
| 122 | +{ |
| 123 | + {0, 4, 3, 5}, |
| 124 | + {2, 3, 3, 5}, |
| 125 | + {1, 4, 4, 5} |
| 126 | +}; |
| 127 | +``` |
| 128 | + |
| 129 | +## What is a vector |
| 130 | + |
| 131 | +- Container in the C++ Standard Template Library |
| 132 | +- An array that can grow and shrink in size at execution time |
| 133 | +- Provides dsimilar semantics and syntax as arrays |
| 134 | +- Very efficient |
| 135 | +- Can provide bounds checking |
| 136 | +- Can use lots of cool functions like sort, reverse, find, and more |
| 137 | + |
| 138 | +### Characteristics |
| 139 | + |
| 140 | +- Dynamic size |
| 141 | +- Elements are all the same type |
| 142 | +- Stored contiguously in memory |
| 143 | +- Individual elements can be accessed by their position or index |
| 144 | +- First element is at `index 0` |
| 145 | +- Last element is at `index size-1` |
| 146 | +- `[]` no checking to see if you are out of bounds |
| 147 | +- Provided many useful function taht do bounds check |
| 148 | +- Elements initialized to zero |
| 149 | +- Very efficient |
| 150 | +- Iteration (looping) is often used to process |
| 151 | + |
| 152 | +## Declaring and Initializing Vectors |
| 153 | + |
| 154 | +- Suppose we want to store test scores for my school and I have no way of knowing how many students will register next year... |
| 155 | +- Options: |
| 156 | + - Pick a size that you are not likely to exceed and use static arrays |
| 157 | + - Use a dynamic array such as vector |
| 158 | + |
| 159 | +### Declaring |
| 160 | + |
| 161 | +```c++ |
| 162 | +#include <vector> |
| 163 | +using namspace std; |
| 164 | + |
| 165 | +vector <char> vowels (5); |
| 166 | +vector <int> test_scores (10); // automatically set to zero |
| 167 | + |
| 168 | +vector <char> vowels {'a', 'e', 'i', 'o', 'u'}; // single quotes |
| 169 | +vector <int> test_scores {100, 98, 89, 85, 93}; |
| 170 | +vector <double> high_temperature (365, 80.0); // 365 initializes size of vector, 80.0 initializes all the 365 doubles to. |
| 171 | +``` |
| 172 | +
|
| 173 | +## Accessing and Modigying Vector Elements |
| 174 | +
|
0 commit comments