Skip to content

Commit

Permalink
deal with size_t issues in Chapter 10
Browse files Browse the repository at this point in the history
  • Loading branch information
wrigjl committed Sep 19, 2024
1 parent 13a2ed1 commit 7cfcb86
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 87 deletions.
2 changes: 1 addition & 1 deletion _sources/Chapter10/accessing_elements.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Chianti.

.. activecode:: accessing_elements_AC_1
:language: cpp
:compileargs: [ '-Wall', '-Werror', '-Wno-sign-compare' ]
:compileargs: [ '-Wall', '-Werror' ]

Take a look at the active code below. We can modify the vectors by accessing
its elements.
Expand Down
32 changes: 16 additions & 16 deletions _sources/Chapter10/activeCode.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Answer the following **Activecode** questions to assess what you have learned in

.. activecode:: vectors_a1q
:language: cpp
:compileargs: [ '-Wall', '-Werror', '-Wno-sign-compare' ]
:compileargs: [ '-Wall', '-Werror' ]

Fix the code below so that it creates a vector with 5 elements initialized to 1, and changes
the third element of that vector to a 2.
Expand All @@ -29,7 +29,7 @@ Answer the following **Activecode** questions to assess what you have learned in

.. activecode:: vectors_a1a
:language: cpp
:compileargs: [ '-Wall', '-Werror', '-Wno-sign-compare' ]
:compileargs: [ '-Wall', '-Werror' ]

Below is one way to fix the program. You must always include the ``<vector>`` header when
dealing with vectors. Furthermore, to initialize a vector's elements to a certain value, you
Expand All @@ -56,7 +56,7 @@ Answer the following **Activecode** questions to assess what you have learned in

.. activecode:: vectors_a3q
:language: cpp
:compileargs: [ '-Wall', '-Werror', '-Wno-sign-compare' ]
:compileargs: [ '-Wall', '-Werror' ]

Fix the function below so that it creates a vector of all of the words in ``words`` that end with
the passed character.
Expand All @@ -69,7 +69,7 @@ Answer the following **Activecode** questions to assess what you have learned in
int endsWith (const vector<string>& vec, char c) {
int count;
for (size_t i = 0; i < vec.size(); i++) {
last = vec.size() - 1;
size_t last = vec.size() - 1;
if (vec[last] == c) {
count++;
}
Expand All @@ -81,7 +81,7 @@ Answer the following **Activecode** questions to assess what you have learned in

.. activecode:: vectors_a3a
:language: cpp
:compileargs: [ '-Wall', '-Werror', '-Wno-sign-compare' ]
:compileargs: [ '-Wall', '-Werror' ]

Below is one way to fix the function. You must initialize ``count`` to zero.
You also must initialize ``last`` as an integer. To access a string *inside*
Expand All @@ -96,7 +96,7 @@ Answer the following **Activecode** questions to assess what you have learned in
int endsWith (const vector<string>& vec, char c) {
int count = 0;
for (size_t i = 0; i < vec.size(); i++) {
int last = vec[i].size() - 1;
size_t last = vec[i].size() - 1;
if (vec[i][last] == c) {
count++;
}
Expand All @@ -116,7 +116,7 @@ Answer the following **Activecode** questions to assess what you have learned in

.. activecode:: vectors_a5q
:language: cpp
:compileargs: [ '-Wall', '-Werror', '-Wno-sign-compare' ]
:compileargs: [ '-Wall', '-Werror' ]

Finish the code below so that it creates removes elements from the end of the vector until
it ends with ``"stop"``.
Expand All @@ -139,7 +139,7 @@ Answer the following **Activecode** questions to assess what you have learned in

.. activecode:: vectors_a5a
:language: cpp
:compileargs: [ '-Wall', '-Werror', '-Wno-sign-compare' ]
:compileargs: [ '-Wall', '-Werror' ]

Below is one way to finish the program. We just use the ``pop_back`` function until the
last element of the vector is ``"stop"``.
Expand Down Expand Up @@ -169,7 +169,7 @@ Answer the following **Activecode** questions to assess what you have learned in

.. activecode:: vectors_a7q
:language: cpp
:compileargs: [ '-Wall', '-Werror', '-Wno-sign-compare' ]
:compileargs: [ '-Wall', '-Werror' ]

Write a function called ``has_char`` that returns a boolean of whether every string in the
vector ``vec`` contains the character ``let``. It should return true if all strings contain the ``let``.
Expand All @@ -183,7 +183,7 @@ Answer the following **Activecode** questions to assess what you have learned in

.. activecode:: vectors_a7a
:language: cpp
:compileargs: [ '-Wall', '-Werror', '-Wno-sign-compare' ]
:compileargs: [ '-Wall', '-Werror' ]

Below is one way to finish the program. We loop through the vector, and we loop through each string
inside it. If the string has the character, it is added to ``count``. We then check whether ``count``
Expand All @@ -194,8 +194,8 @@ Answer the following **Activecode** questions to assess what you have learned in
using namespace std;


int has_char (const vector<string>& vec, char let) {
int count = 0;
bool has_char (const vector<string>& vec, char let) {
size_t count = 0;
for (size_t i = 0; i < vec.size(); i++) {
for (size_t c = 0; c < vec[i].size(); c++) {
if (vec[i][c] == let) {
Expand All @@ -221,7 +221,7 @@ Answer the following **Activecode** questions to assess what you have learned in

.. activecode:: vectors_a9q
:language: cpp
:compileargs: [ '-Wall', '-Werror', '-Wno-sign-compare' ]
:compileargs: [ '-Wall', '-Werror' ]

Write the function ``mean`` which returns the average of a vector of numbers.
~~~~
Expand All @@ -234,7 +234,7 @@ Answer the following **Activecode** questions to assess what you have learned in

.. activecode:: vectors_a9a
:language: cpp
:compileargs: [ '-Wall', '-Werror', '-Wno-sign-compare' ]
:compileargs: [ '-Wall', '-Werror' ]

Below is one way to finish the program. First we take the sum, then divide the sum by the number
of elements in ``nums``.
Expand Down Expand Up @@ -263,7 +263,7 @@ Answer the following **Activecode** questions to assess what you have learned in

.. activecode:: vectors_a11q
:language: cpp
:compileargs: [ '-Wall', '-Werror', '-Wno-sign-compare' ]
:compileargs: [ '-Wall', '-Werror' ]

Write the function ``make_odd`` which subtracts 1 from every even number in a vector of integers.
We don't want any negative values so don't subtract 1 from 0.
Expand All @@ -278,7 +278,7 @@ Answer the following **Activecode** questions to assess what you have learned in

.. activecode:: vectors_a11a
:language: cpp
:compileargs: [ '-Wall', '-Werror', '-Wno-sign-compare' ]
:compileargs: [ '-Wall', '-Werror' ]

Below is one way to finish the program. We us the modulus operator to check for even numbers and decrement them.
we keep an extra check for 0 to make sure wew are not decrementing 0.
Expand Down
36 changes: 18 additions & 18 deletions _sources/Chapter10/activeCode_sq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Coding Practice

.. activecode:: vectors_a2
:language: cpp
:compileargs: [ '-Wall', '-Werror', '-Wno-sign-compare' ]
:compileargs: [ '-Wall', '-Werror' ]

Fix the function below so that it returns how many even numbers are in ``nums``.
Select the Parsonsprob tab for hints for the construction of the code.
Expand All @@ -17,7 +17,7 @@ Coding Practice
using namespace std;

int evenCount (const vector<int> &vec) {
for (int i = 0; i < vec.size(); i++) {
for (size_t i = 0; i < vec.size(); i++) {
if (i % 2 == 0) {
count++;
}
Expand Down Expand Up @@ -46,7 +46,7 @@ Coding Practice
=====
int count; #distractor
=====
for (int i = 0; i < vec.size(); i++) {
for (size_t i = 0; i < vec.size(); i++) {
=====
if (vec[i] % 2 == 0) {
=====
Expand All @@ -68,7 +68,7 @@ Coding Practice

.. activecode:: vectors_a4
:language: cpp
:compileargs: [ '-Wall', '-Werror', '-Wno-sign-compare' ]
:compileargs: [ '-Wall', '-Werror' ]

Someone could have COVID-19 if their temperature is above 99.9 degrees Fahrenheit. Finish
the code below so that it counts and prints how many students in the class may have been exposed.
Expand All @@ -83,7 +83,7 @@ Coding Practice
vector<double> temps = {98.6, 97.8, 100.3, 97.2, 98.7, 97.8, 99.8, 96.9, 98.2, 99.1, 99.9};

int covid_count = 0;
for (int i = 0; i < temps.size(); i++) {
for (size_t i = 0; i < temps.size(); i++) {


}
Expand All @@ -109,7 +109,7 @@ Coding Practice
=====
int covid_count = 0;
=====
for (int i = 0; i < temps.size(); i++) {
for (size_t i = 0; i < temps.size(); i++) {
=====
if (temps[i] > 99.9) {
=====
Expand All @@ -129,7 +129,7 @@ Coding Practice

.. activecode:: vectors_a6
:language: cpp
:compileargs: [ '-Wall', '-Werror', '-Wno-sign-compare' ]
:compileargs: [ '-Wall', '-Werror' ]

Write the function ``endsEven`` that takes a vector and removes elements from the end of the vector until
it ends with an even number. Select the Parsonsprob tab for hints for the construction of the code.
Expand All @@ -144,7 +144,7 @@ Coding Practice
int main() {
vector<int> vec{1,2,3,4,5,6,7,7,9};
endsEven(vec);
for(int unsigned i = 0; i < vec.size(); i++) {
for(size_t i = 0; i < vec.size(); i++) {
cout << vec[i] << endl;
}
}
Expand All @@ -165,7 +165,7 @@ Coding Practice
=====
while (vec.back() % 2 != 0) {
=====
for (int i = 0; i < vec.size(); i++) { #paired
for (size_t i = 0; i < vec.size(); i++) { #paired
=====
vec.pop_back();
=====
Expand All @@ -179,7 +179,7 @@ Coding Practice

.. activecode:: vectors_a8
:language: cpp
:compileargs: [ '-Wall', '-Werror', '-Wno-sign-compare' ]
:compileargs: [ '-Wall', '-Werror' ]

Write the function ``randomNums`` that takes two integers: ``num`` which is the number of random numbers
you wish to generate, and ``max``, which is the maximum value of random number you wish to generate. Your
Expand All @@ -198,7 +198,7 @@ Coding Practice
int num = 10;
int max = 100;
randomNums(num,max);
for (int i = 0; i < randomNums(num,max).size(); i++) {
for (size_t i = 0; i < randomNums(num,max).size(); i++) {
cout << randomNums(num,max)[i] << endl;
}
}
Expand Down Expand Up @@ -238,7 +238,7 @@ Coding Practice

.. activecode:: vectors_a10
:language: cpp
:compileargs: [ '-Wall', '-Werror', '-Wno-sign-compare' ]
:compileargs: [ '-Wall', '-Werror' ]

Write the function ``hundyBundy`` that returns a count of all numbers in the passed vector
``vec`` that are divisible by 100. Select the Parsonsprob tab for hints for the construction of the code.
Expand Down Expand Up @@ -271,9 +271,9 @@ Coding Practice
=====
int count = 0;
=====
for (int i = 0; i < vec.size(); i++) {
for (size_t i = 0; i < vec.size(); i++) {
=====
for (int i = 0; i < count(); i++) { #distractor
for (size_t i = 0; i < count(); i++) { #distractor
=====
if (vec[i] % 100 == 0) {
=====
Expand All @@ -293,7 +293,7 @@ Coding Practice

.. activecode:: vectors_a12
:language: cpp
:compileargs: [ '-Wall', '-Werror', '-Wno-sign-compare' ]
:compileargs: [ '-Wall', '-Werror' ]

Write the function ``weird_print`` that prints the first half of a vector of integers in reverse order
and then prints the second half in the order present in the vector.
Expand Down Expand Up @@ -330,15 +330,15 @@ Coding Practice
-----
void weird_print (vector<int> vec) {
=====
int half = vec.size() / 2;
size_t half = vec.size() / 2;
=====
for (int i = vec.size() - 1; i >= half; i--){
for (size_t i = vec.size() - 1; i >= half; i--){
=====
cout << vec[i-half] << ' ';
=====
}
=====
for (int h = 0; h < half; h++) {
for (size_t h = 0; h < half; h++) {
=====
cout << vec[h + half] << ' ';
=====
Expand Down
2 changes: 1 addition & 1 deletion _sources/Chapter10/copying_vectors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ would expect.

.. activecode:: copying_vectors_AC_1
:language: cpp
:compileargs: [ '-Wall', '-Werror', '-Wno-sign-compare' ]
:compileargs: [ '-Wall', '-Werror' ]

Take a look at the active code below, which uses the copy constructor.
~~~~
Expand Down
14 changes: 7 additions & 7 deletions _sources/Chapter10/counting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ return value is the number of times the value appears.

.. activecode:: counting_AC_1
:language: cpp
:compileargs: [ '-Wall', '-Werror', '-Wno-sign-compare' ]
:compileargs: [ '-Wall', '-Werror' ]

Take a look at the active code below which uses the ``howMany`` function. Run the
code to see how many times the target appears in the vector! Feel free to
Expand Down Expand Up @@ -82,14 +82,14 @@ return value is the number of times the value appears.

vector<int> randomVector (int n, int upperBound) {
vector<int> vec (n);
for (size_t i = 0; i<vec.size(); i++) {
vec[i] = random () % upperBound;
for (size_t i = 0; i < vec.size(); i++) {
vec[i] = rand() % upperBound;
}
return vec;
}

void printVector (const vector<int>& vec) {
for (size_t i = 0; i<vec.size(); i++) {
for (size_t i = 0; i < vec.size(); i++) {
cout << vec[i] << " ";
}
}
Expand All @@ -113,13 +113,13 @@ return value is the number of times the value appears.

Construct a block of code that counts how many numbers are between lowerbound and upperbound inclusive.
-----
int just_right(const vector<int>& vec, int lowerbound, int upperbound) {
int just_right(const vector&lt;int&gt;&amp; vec, int lowerbound, int upperbound) {
=====
int count = 0;
=====
for (size_t i = 0; i &#60; vec.size(); i++) {
for (size_t i = 0; i < vec.size(); i++) {
=====
for (int i = 0; i &#60; upperbound; i++) #paired
for (size_t i = 0; i > upperbound; i++) #paired
=====
if (vec[i] >= lowerbound && vec[i] <= upperbound) {
count++;
Expand Down
10 changes: 5 additions & 5 deletions _sources/Chapter10/for_loops.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ is equivalent to

.. activecode:: for_loops_AC_1
:language: cpp
:compileargs: [ '-Wall', '-Werror', '-Wno-sign-compare' ]
:compileargs: [ '-Wall', '-Werror' ]

Run the active code below, which uses a ``for`` loop.
~~~~
Expand All @@ -69,7 +69,7 @@ is equivalent to

.. activecode:: for_loops_AC_2
:language: cpp
:compileargs: [ '-Wall', '-Werror', '-Wno-sign-compare' ]
:compileargs: [ '-Wall', '-Werror' ]

Run the active code below, which uses a ``while`` loop.
~~~~
Expand Down Expand Up @@ -120,9 +120,9 @@ is equivalent to
=====
int new_amount = initial_amount;
=====
for (int i = 0; i &#60; num; i++) {
for (int i = 0; i &lt; num; i++) {
=====
for (int i = 0; i &#60;= num; i++) { #paired
for (int i = 0; i &lt; num; i++) { #paired
=====
new_amount = new_amount / 2;
=====
Expand All @@ -137,7 +137,7 @@ is equivalent to

.. activecode:: for_loops_AC_3
:language: cpp
:compileargs: [ '-Wall', '-Werror', '-Wno-sign-compare' ]
:compileargs: [ '-Wall', '-Werror' ]

Run the active code below, which uses a ``for`` loop with a negative change in the "INCREMENTOR".
~~~~
Expand Down
Loading

0 comments on commit 7cfcb86

Please sign in to comment.