Skip to content

Commit 09d071e

Browse files
committed
Added materail for Section07 thus far.
1 parent a154182 commit 09d071e

24 files changed

+1419
-0
lines changed
+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Coding Exercise 5: Declaring, Initializing and Accessing an Array
2+
3+
- Declare an array of `10` integers named `arr` and initialize the array so that all 10 integers are `0`.
4+
- Then assign `100` to the first element of the array and `1000` to the last element of the array.
5+
6+
## Solution
7+
8+
```c++
9+
#include <vector>
10+
using namespace std;
11+
12+
vector<int> use_array() {
13+
//----DO NOT MODIFY THE CODE ABOVE THIS LINE----
14+
//----WRITE YOUR CODE BELOW THIS LINE----
15+
16+
int arr [10] {};
17+
arr[0] = 100;
18+
arr[9] = 1000;
19+
20+
//-----WRITE YOUR ABOVE THIS LINE----
21+
//-----DO NOT CHANGE THE CODE BELOW THIS LINE----
22+
vector<int> v(arr, arr + sizeof arr / sizeof arr[0]);
23+
return v;
24+
}
25+
```
26+
27+
# Coding Exercise 5: Declaring, Initializing and Accessing Vectors
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Session Name="C:\Users\frank\Desktop\CPPExamples\Section7\Section7.workspace">
3+
<int Value="0" Name="m_selectedTab"/>
4+
<wxString Value="C:\Users\frank\Desktop\CPPExamples\Section7\Section7.workspace" Name="m_workspaceName"/>
5+
<TabInfoArray Name="TabInfoArray"/>
6+
<SerializedObject Name="m_breakpoints">
7+
<long Value="0" Name="Count"/>
8+
</SerializedObject>
9+
</Session>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
##
2+
## Auto Generated makefile by CodeLite IDE
3+
## any manual changes will be erased
4+
##
5+
## Debug
6+
ProjectName :=Arrays
7+
ConfigurationName :=Debug
8+
WorkspacePath :=C:/Users/frank/Desktop/CPPExamples/Section7
9+
ProjectPath :=C:/Users/frank/Desktop/CPPExamples/Section7/Arrays
10+
IntermediateDirectory :=./Debug
11+
OutDir := $(IntermediateDirectory)
12+
CurrentFileName :=
13+
CurrentFilePath :=
14+
CurrentFileFullPath :=
15+
User :=frank
16+
Date :=31/01/2018
17+
CodeLitePath :="C:/Program Files/CodeLite"
18+
LinkerName :=C:/MinGW/bin/g++.exe
19+
SharedObjectLinkerName :=C:/MinGW/bin/g++.exe -shared -fPIC
20+
ObjectSuffix :=.o
21+
DependSuffix :=.o.d
22+
PreprocessSuffix :=.i
23+
DebugSwitch :=-g
24+
IncludeSwitch :=-I
25+
LibrarySwitch :=-l
26+
OutputSwitch :=-o
27+
LibraryPathSwitch :=-L
28+
PreprocessorSwitch :=-D
29+
SourceSwitch :=-c
30+
OutputFile :=$(IntermediateDirectory)/$(ProjectName)
31+
Preprocessors :=
32+
ObjectSwitch :=-o
33+
ArchiveOutputSwitch :=
34+
PreprocessOnlySwitch :=-E
35+
ObjectsFileList :="Arrays.txt"
36+
PCHCompileFlags :=
37+
MakeDirCommand :=makedir
38+
RcCmpOptions :=
39+
RcCompilerName :=C:/MinGW/bin/windres.exe
40+
LinkOptions :=
41+
IncludePath := $(IncludeSwitch). $(IncludeSwitch).
42+
IncludePCH :=
43+
RcIncludePath :=
44+
Libs :=
45+
ArLibs :=
46+
LibPath := $(LibraryPathSwitch).
47+
48+
##
49+
## Common variables
50+
## AR, CXX, CC, AS, CXXFLAGS and CFLAGS can be overriden using an environment variables
51+
##
52+
AR := C:/MinGW/bin/ar.exe rcu
53+
CXX := C:/MinGW/bin/g++.exe
54+
CC := C:/MinGW/bin/gcc.exe
55+
CXXFLAGS := -std=c++14 -Wall -g -O0 -std=c++14 -Wall $(Preprocessors)
56+
CFLAGS := -g -O0 -Wall $(Preprocessors)
57+
ASFLAGS :=
58+
AS := C:/MinGW/bin/as.exe
59+
60+
61+
##
62+
## User defined environment variables
63+
##
64+
CodeLiteDir:=C:\Program Files\CodeLite
65+
Objects0=$(IntermediateDirectory)/main.cpp$(ObjectSuffix)
66+
67+
68+
69+
Objects=$(Objects0)
70+
71+
##
72+
## Main Build Targets
73+
##
74+
.PHONY: all clean PreBuild PrePreBuild PostBuild MakeIntermediateDirs
75+
all: $(OutputFile)
76+
77+
$(OutputFile): $(IntermediateDirectory)/.d $(Objects)
78+
@$(MakeDirCommand) $(@D)
79+
@echo "" > $(IntermediateDirectory)/.d
80+
@echo $(Objects0) > $(ObjectsFileList)
81+
$(LinkerName) $(OutputSwitch)$(OutputFile) @$(ObjectsFileList) $(LibPath) $(Libs) $(LinkOptions)
82+
83+
MakeIntermediateDirs:
84+
@$(MakeDirCommand) "./Debug"
85+
86+
87+
$(IntermediateDirectory)/.d:
88+
@$(MakeDirCommand) "./Debug"
89+
90+
PreBuild:
91+
92+
93+
##
94+
## Objects
95+
##
96+
$(IntermediateDirectory)/main.cpp$(ObjectSuffix): main.cpp $(IntermediateDirectory)/main.cpp$(DependSuffix)
97+
$(CXX) $(IncludePCH) $(SourceSwitch) "C:/Users/frank/Desktop/CPPExamples/Section7/Arrays/main.cpp" $(CXXFLAGS) $(ObjectSwitch)$(IntermediateDirectory)/main.cpp$(ObjectSuffix) $(IncludePath)
98+
$(IntermediateDirectory)/main.cpp$(DependSuffix): main.cpp
99+
@$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) -MG -MP -MT$(IntermediateDirectory)/main.cpp$(ObjectSuffix) -MF$(IntermediateDirectory)/main.cpp$(DependSuffix) -MM main.cpp
100+
101+
$(IntermediateDirectory)/main.cpp$(PreprocessSuffix): main.cpp
102+
$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) $(PreprocessOnlySwitch) $(OutputSwitch) $(IntermediateDirectory)/main.cpp$(PreprocessSuffix) main.cpp
103+
104+
105+
-include $(IntermediateDirectory)/*$(DependSuffix)
106+
##
107+
## Clean
108+
##
109+
clean:
110+
$(RM) -r ./Debug/
111+
112+

0 commit comments

Comments
 (0)