Skip to content

Commit

Permalink
Adding more interactivity
Browse files Browse the repository at this point in the history
  • Loading branch information
gvegayon committed Sep 13, 2024
1 parent ebfb9e8 commit 6f401c0
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
75 changes: 68 additions & 7 deletions 05-cpp/slides.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ format:
theme: ["default", "style.scss"]
code-annotations: true
slide-number: c
revealjs-plugins:
- codefocus
engine: knitr
---

Expand Down Expand Up @@ -72,6 +74,10 @@ g++ -std=c++14 hello-world.cpp -o hello-world

## Example: Computing the mean

Download the program [here](means.cpp).

::: {.columns}
::: {.column width="60%"}
```cpp
#include<iostream> // To print
#include<vector> // To use vectors
Expand All @@ -84,10 +90,7 @@ int main() {
// Making room for the output
double ans = 0.0;

// For-loops have three components:
// - Starts in i = 0
// - Until i reaches dat.size() (stops)
// - Increments i + 1
// Looping through the data
for (int i = 0; i < dat.size(); ++i)
ans = ans + dat[i];

Expand All @@ -101,20 +104,41 @@ int main() {

}
```
:::
::: {.column width="40%"}
::: {.fragment data-code-focus="1-2"}
- Loading the libraries
:::

Download the program [here](means.cpp).
::: {.fragment data-code-focus="7"}
- Creating a vector double with three values.
:::

::: {.fragment data-code-focus="12-14"}
- A for-loop that starts from zero and goes to the size of the vector,
incrementing by one (`++i`).
:::

::: {.fragment data-code-focus="1-24"}
To compile it, run the following command:

```{bash}
#| eval: true
#| echo: true
g++ -std=c++14 means.cpp -o means
./means
```
:::

:::
:::

## Example: Computing the mean (take 2)

We can leverage modern C++ to make the code shorter with `std::accumulate()`

::: {.columns}
::: {.column width="60%"}
```cpp
#include<iostream> // To print
#include<vector> // To use vectors
Expand All @@ -126,7 +150,9 @@ int main() {
std::vector< double > dat = {1.0, 2.5, 4.4};

// Making room for the output
double ans = std::accumulate(dat.begin(), dat.end(), 0.0);
double ans = std::accumulate(
dat.begin(), dat.end(), 0.0
);
ans /= dat.size();

// Print out the value to the screen
Expand All @@ -137,13 +163,29 @@ int main() {

}
```
:::
::: {.column width="40%"}

::: {.fragment data-code-focus="3"}
- Using the `numeric` library that has the accumulate function
:::

::: {.fragment data-code-focus="11-13"}
- The `std::accumulate` function summs the elements of the vector.
:::

::: {.fragment data-code-focus="1-22"}
To compile it, run the following command:
```{bash}
#| eval: true
#| echo: true
g++ -std=c++14 means2.cpp -o means2
./means2
```
:::
:::
:::


# Language fundamentals {background-color="black"}

Expand Down Expand Up @@ -201,9 +243,12 @@ my_vector[0]; // Access the first element
my_vector.at(0) // Access the first element (safer)
```

## Vectors in C++: Looping

Looping through vectors can be done in different ways:

::: {}
::: {.columns}
::: {.column width="60%"}
```cpp
// Suppose we have this:
std::vector< int > my_vector = {1, 2, 3, 4, 5};`
Expand All @@ -225,6 +270,22 @@ for (auto i: my_vector) {
}
```
:::
::: {.column width="40%"}
::: {.fragment data-code-focus="4-7"}
- The typical loop, access elements by index.
:::

::: {.fragment data-code-focus="9-13"}
- Using iterators, a bit more complex, `i` becomes a pointer
to the value.
:::

::: {.fragment data-code-focus="15-18"}
- The range-based for loop, simpler and cleaner.
:::

:::
:::

## Important keywords

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ them.
| 8/27 and 8/29 | R Essentials + [Homework 1](https://UofUEpiBio.github.io/PHS7045-advanced-programming/02-essentials/01-essentialsSimulations.html) ([source](02-essentials/01-essentialsSimulations.qmd)) | [slides](https://UofUEpiBio.github.io/PHS7045-advanced-programming/02-essentials/slides.html) ([source](https://github.com/UofUEpiBio/PHS7045-advanced-programming//tree/main/02-essentials/slides.qmd)) | [lab](https://UofUEpiBio.github.io/PHS7045-advanced-programming/02-essentials/lab.html) ([source](https://github.com/UofUEpiBio/PHS7045-advanced-programming//tree/main/02-essentials/lab.qmd)) |
| 9/03 and 9/5 | Debugging and profiling | [slides](https://UofUEpiBio.github.io/PHS7045-advanced-programming/04-debugging-and-profiling/slides.html) ([source](https://github.com/UofUEpiBio/PHS7045-advanced-programming//tree/main/04-debugging-and-profiling/slides.qmd)) | [lab](https://UofUEpiBio.github.io/PHS7045-advanced-programming/04-debugging-and-profiling/lab.html) ([source](https://github.com/UofUEpiBio/PHS7045-advanced-programming//tree/main/04-debugging-and-profiling/lab.qmd)) |
| 9/10 and 9/12 | Functions and data.table + [Homework 2](https://UofUEpiBio.github.io/PHS7045-advanced-programming/03-more-functions-and-datatable/homework.html) ([source](03-more-functions-and-datatable/homework.qmd)) | [slides](https://UofUEpiBio.github.io/PHS7045-advanced-programming/03-more-functions-and-datatable/slides.html) ([source](https://github.com/UofUEpiBio/PHS7045-advanced-programming//tree/main/03-more-functions-and-datatable/slides.qmd)) | [lab](https://UofUEpiBio.github.io/PHS7045-advanced-programming/03-more-functions-and-datatable/lab.html) ([source](https://github.com/UofUEpiBio/PHS7045-advanced-programming//tree/main/03-more-functions-and-datatable/lab.qmd)) |
| 9/17 and 9/19 | Introduction to C++ + Homework 3 (C++), Deliver a paragraph in abstract mode describing their projects for the midterm. | | |
| 9/17 and 9/19 | Introduction to C++ + Homework 3 (C++), Deliver a paragraph in abstract mode describing their projects for the midterm. | [slides](https://UofUEpiBio.github.io/PHS7045-advanced-programming/05-cpp/slides.html) ([source](https://github.com/UofUEpiBio/PHS7045-advanced-programming//tree/main/05-cpp/slides.qmd)) | |
| 9/24 and 9/26 | C++ in R | [slides](https://UofUEpiBio.github.io/PHS7045-advanced-programming/05-rcpp/slides.html) ([source](https://github.com/UofUEpiBio/PHS7045-advanced-programming//tree/main/05-rcpp/slides.qmd)) | [lab](https://UofUEpiBio.github.io/PHS7045-advanced-programming/05-rcpp/lab.html) ([source](https://github.com/UofUEpiBio/PHS7045-advanced-programming//tree/main/05-rcpp/lab.qmd)) |
| 10/01 and 10/03 | Parallel Computing | | [lab](https://UofUEpiBio.github.io/PHS7045-advanced-programming/06-parallel-computing/lab.html) ([source](https://github.com/UofUEpiBio/PHS7045-advanced-programming//tree/main/06-parallel-computing/lab.qmd)) |
| 10/08 and 10/10 | Fall Break | | |
Expand Down
2 changes: 1 addition & 1 deletion pages.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ date,week,name,id,issue,extra
8/27 and 8/29,2,R Essentials,02-essentials,,+ [Homework 1](https://UofUEpiBio.github.io/PHS7045-advanced-programming/02-essentials/01-essentialsSimulations.html) ([source](02-essentials/01-essentialsSimulations.qmd))
9/03 and 9/5,3,Debugging and profiling,04-debugging-and-profiling,,
9/10 and 9/12,4,Functions and data.table,03-more-functions-and-datatable,,+ [Homework 2](https://UofUEpiBio.github.io/PHS7045-advanced-programming/03-more-functions-and-datatable/homework.html) ([source](03-more-functions-and-datatable/homework.qmd))
9/17 and 9/19,5,Introduction to C++,,,"+ Homework 3 (C++), Deliver a paragraph in abstract mode describing their projects for the midterm."
9/17 and 9/19,5,Introduction to C++,05-cpp,,"+ Homework 3 (C++), Deliver a paragraph in abstract mode describing their projects for the midterm."
9/24 and 9/26,6,C++ in R,05-rcpp,,
10/01 and 10/03,7,Parallel Computing,06-parallel-computing,,
10/08 and 10/10,8,Fall Break,,,
Expand Down

0 comments on commit 6f401c0

Please sign in to comment.