Skip to content

Commit 8ab95eb

Browse files
author
Jamal M. Aw Yoonis
committed
fixed #39
1 parent 7f586bb commit 8ab95eb

File tree

11 files changed

+105
-109
lines changed

11 files changed

+105
-109
lines changed

_sources/AtomicData/AtomicData.rst

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ are actually ``1`` and ``0`` respectively. Hence, we see this in output as well.
153153
#include <iostream>
154154
using namespace std;
155155

156-
// function that demonstrates logical operators
156+
// function that demonstrates logical operators
157157
int main() {
158158
cout << true << endl;
159159
cout << false << endl;
@@ -166,7 +166,7 @@ are actually ``1`` and ``0`` respectively. Hence, we see this in output as well.
166166

167167
.. activecode:: logical_1py
168168
:caption: Logical Operators Python
169-
169+
170170
# Function that demonstrates logical operators.
171171
def main():
172172
print(True)
@@ -211,8 +211,8 @@ and logical operators with examples shown in the session that follows.
211211

212212
#include <iostream>
213213
using namespace std;
214-
215-
// function that demonstrates relational operators.
214+
215+
// function that demonstrates relational operators.
216216
int main(){
217217

218218
cout << (5 == 10) << endl;
@@ -226,7 +226,7 @@ and logical operators with examples shown in the session that follows.
226226

227227
.. activecode:: logicalpy
228228
:caption: Basic Relational and Logical Operators Python
229-
229+
230230
# Function that demonstrates relational operators.
231231
def main():
232232

@@ -339,8 +339,8 @@ Consider the following code.
339339
#include <string>
340340
using namespace std;
341341

342-
// Demonstrates how chars and strings can not be
343-
// directly compared.
342+
// Demonstrates how chars and strings can not be
343+
// directly compared.
344344
int main(){
345345

346346
string strvar = "b";
@@ -376,7 +376,7 @@ Pointers
376376
~~~~~~~~
377377

378378
A C++ **pointer** is a variable that stores a memory address and can be used to indirectly
379-
access data stored at that memory location.
379+
access data stored at that memory location.
380380

381381
We know that variables in a computer program are used to label data with a
382382
descriptive identifier so that the data can be accessed and used by that
@@ -428,7 +428,7 @@ In C++ the results of running this code will look like the diagram below:
428428
In each case, when we want to output the value to the console, we use the variable name
429429
to do so.
430430

431-
But, we can also identify the memory location of the variable by its address.
431+
But, we can also identify the memory location of the variable by its address.
432432
In both Python and C++, this address
433433
may change each time the program is run. In C++, the address will always look
434434
odd because it will be the actual memory address written in a hexadecimal code
@@ -453,7 +453,7 @@ while in C++ we use the *address-of operator*, ``&``.
453453

454454

455455
// outputs the value of a variable
456-
// as well as the memory address in C++.
456+
// as well as the memory address in C++.
457457
int main(){
458458
int varN = 101;
459459
cout << varN << endl;
@@ -493,7 +493,7 @@ Pointer Syntax
493493
^^^^^^^^^^^^^^
494494

495495
When declaring a pointer in C++ that will "point" to the memory address of some
496-
data type,
496+
data type,
497497
you will use the same rules of declaring variables and data types.
498498
The key difference is that there must be an asterisk (*) between the data type and the
499499
identifier.
@@ -523,25 +523,25 @@ Now that we know how to declare pointers, how do we give them the address of
523523
where the value is going to be stored? One way to do this is to have a pointer
524524
refer to another variable by using the address-of operator, which is denoted by the
525525
ampersand symbol, ``&``. The address-of operator ``&`` does exactly what it indicates,
526+
variableType varN; // a variable to hold the value
526527
namely it returns the address.
527528

528529
The syntax is shown below, where varN stores the value, and ptrN stores
529530
the address of where varN is located:
530531

531532
::
532533

533-
variableType varN; // a variable to hold the value
534534
variableType *ptrN = &varN; // a variable pointing to the address of varN
535535

536536
Keep in mind that when declaring a C++ pointer, the pointer needs to
537537
reference the same type as the variable or constant to which it points.
538538

539-
Expanding on the example above where varN has the value of 100.
539+
Expanding on the example above where varN has the value of 9.
540540

541541
::
542542

543543
//variable declaration for a single integer value
544-
int varN = 100;
544+
int varN = 9;
545545
int *ptrN;
546546
ptrN = &varN;
547547

@@ -558,14 +558,14 @@ The results of running this C++ code will look like the diagram below.
558558
Accessing Values from Pointers
559559
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
560560

561-
Once you have a C++ pointer, you use the asterisk before the pointer variable,
561+
Once you have a C++ pointer, you use the asterisk before the pointer variable,
562562
to *dereference* the pointer, which means go to the location pointed at by the 3.
563563

564564
::
565565

566566
In other words, varN and *ptrN (note the asterisk in front!) reference the same
567567
value in the code above.
568-
568+
569569

570570
Let's extend the example above to output the value of a variable and its address
571571
in memory:
@@ -577,23 +577,23 @@ in memory:
577577

578578
#include <iostream>
579579
using namespace std;
580-
581-
// demonstrates what happens when you dereference a pointer
580+
581+
// demonstrates what happens when you dereference a pointer
582582
int main( ) {
583-
int varN = 100;
583+
int varN = 9;
584584
int *ptrN = &varN; // ptrN points to varN address
585585
586586
cout << "varN value: " << varN << endl;
587587
cout << "varN location: " << ptrN << endl;
588588
cout << "dereference ptrN: " << *ptrN << endl;
589589
590-
590+
591591
return 0;
592592
}
593-
593+
594594

595595
.. mchoice:: mc_pntrhlp
596-
:answer_a: varPntr: 100
596+
:answer_a: varPntr: 9
597597
:answer_b: varPntr: 50
598598
:answer_c: varPntr: 150
599599
:answer_d: 0x7ffeb9ce053c
@@ -604,8 +604,8 @@ in memory:
604604
:feedback_c: No, the values do not add together!
605605
:feedback_d: We are dereferencing the pointer, so you would not get the address of varN. Try again!
606606
:feedback_e: One of the above is indeed correct.
607-
608-
If the lines (varN = 50;) and (cout << \*ptrN << endl;) were inserted into line 7-8, what would it cout?
607+
608+
If the lines (varN = 50;) and (cout << \*ptrN << endl;) were inserted into line 7-8, what would it cout?
609609

610610
Compiling and running the above code will have the program output the
611611
value in varN,

_sources/CollectionData/Vectors.rst

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,68 @@ Because vectors can change size, vectors typically allocate some extra storage t
5151
Thus the vector typically has an actual *capacity* greater than the storage *size* strictly needed to contain its elements.
5252

5353

54+
Iterating through Vectors
55+
~~~~~~~~~~~~~~~~~~~~~~~~~
56+
57+
When iterating vectors, you must first find the length of your container. You can simply call the ``.length()`` function.
58+
For arrays, the number of elements can be found by getting the size in memory of the array
59+
by using the ``sizeof()`` function, and then dividing it by the size of the first element of
60+
the array using the same ``sizeof()`` function. Because all elements in C++ arrays are
61+
the same type, they take the same amount of space and that can be used to find the number
62+
of elements the Array contains!
63+
64+
.. activecode:: vectorIteration1
65+
:language: cpp
66+
:sourcefile: ArrayIteration.cpp
67+
68+
#include <iostream>
69+
using namespace std;
70+
71+
int main() {
72+
int nums[] = {1,3,6,2,5};
73+
//Divide the size of the array (in bytes) by the size of a single element (in bytes)
74+
// to get the total number of elements in the array.
75+
int numsSize = sizeof(nums)/sizeof(nums[0]); // Get size of the nums array
76+
77+
for (int index=0; index<numsSize; index++) {
78+
cout << nums[index] << endl;
79+
}
80+
81+
82+
// Simpler Implementation that may only work in
83+
// Newer versions of C++
84+
85+
// for (int item:nums) {
86+
// cout << item << endl;
87+
// }
88+
89+
return 0;
90+
}
91+
92+
93+
An optional secondary version of the ``for`` loop has been commented out of the above code.
94+
You can try running this in your version of C++ to see if it works, but in some older versions of C++,
95+
such as C++98, it does not.
96+
97+
The above loop assigns the variable ``index`` to be each successive value from 0 to numsSize.
98+
Then, the value at that index in the array is printed to the console.
99+
100+
54101
Matching
55102
^^^^^^^^
56103
.. dragndrop:: matching_vectors
57104
:feedback: Feedback shows incorrect matches.
58-
:match_1: [ ]|||Accesses value of an element.
59-
:match_2: =||| Assigns value to an element.
105+
:match_1: [ ]|||Accesses value of an element.
106+
:match_2: =||| Assigns value to an element.
60107
:match_3: push_back|||Appends item to the end of the vector.
61108
:match_4: pop_back||| Deletes last item of the vector.
62-
:match_5: insert|||Injects an item into the vector.
109+
:match_5: insert|||Injects an item into the vector.
63110
:match_6: erase|||Deletes an element from the choosen index.
64111
:match_7: size|||Returns the actual capacity used by elements.
65112
:match_8: capacity|||Returns the ammount of allocated storage space.
66113
:match_9: reserve||| Request a change in space to amount
67114

68-
Match the Vector operations with their corresponding explination.
115+
Match the Vector operations with their corresponding explination.
69116

70117
.. tabbed:: intro_vector
71118

@@ -75,8 +122,8 @@ Matching
75122
:caption: Using a vector in C++
76123
:language: cpp
77124

78-
// function that uses a vector to square
79-
// every number from 0 to 49
125+
// function that uses a vector to square
126+
// every number from 0 to 49
80127
// uses the reserve operation to save space in memory
81128
#include <iostream>
82129
#include <vector>
@@ -99,7 +146,7 @@ Matching
99146
.. activecode:: introvector_py
100147
:caption: Using a Python list
101148

102-
"""Uses a list to square every
149+
"""Uses a list to square every
103150
number from 0 to 49 """
104151
def main():
105152
intlist=[]
@@ -123,10 +170,10 @@ example that follows.
123170
:caption: With use of ``reserve``
124171
:language: cpp
125172

126-
// function that uses a vector to square
127-
// every number from 0 to 49
128-
// and does not use reserve.
129-
// shows amount of space used
173+
// function that uses a vector to square
174+
// every number from 0 to 49
175+
// and does not use reserve.
176+
// shows amount of space used
130177
#include <iostream>
131178
#include <vector>
132179
using namespace std;
@@ -156,8 +203,8 @@ we will likely not be surprised by the following:
156203
:caption: Vectors out of bounds
157204
:language: cpp
158205

159-
// Note: counting always starts at 0
160-
// This demonstrates what happens when
206+
// Note: counting always starts at 0
207+
// This demonstrates what happens when
161208
// accessing datat outside of your vector
162209

163210
#include <iostream>
@@ -186,7 +233,7 @@ we will likely not be surprised by the following:
186233
.. activecode:: vector_errors_py
187234
:caption: Python list out of bounds
188235

189-
"""Demonstrates python's protections
236+
"""Demonstrates python's protections
190237
against iterating outside of a list"""
191238
def main():
192239
intlist=[]

0 commit comments

Comments
 (0)