-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlecture1.tex
447 lines (360 loc) · 12 KB
/
lecture1.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
%Lecture 1
%Copyright (C) 2019 Patrick Diehl
%
%This program is free software: you can redistribute it and/or modify
%it under the terms of the GNU General Public License as published by
%the Free Software Foundation, either version 3 of the License, or
%(at your option) any later version.
%This program is distributed in the hope that it will be useful,
%but WITHOUT ANY WARRANTY; without even the implied warranty of
%MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%GNU General Public License for more details.
%You should have received a copy of the GNU General Public License
%along with this program. If not, see <http://www.gnu.org/licenses/>.
\providecommand\classoption{12pt}
\documentclass[\classoption]{beamer}
\input{./template/packages}
\input{template/variables.tex}
% frame slide
\title{\coursename}
\subtitle{Lecture 1: Introduction and Getting started}
\author{\tiny Patrick Diehl \orcid{0000-0003-3922-8419}}
%\institute {
% \href{}{\tt \scriptsize \today}
%}
\date {
\tiny \url{\courseurl}
\vspace{2cm}
\doclicenseThis
}
\begin{document} {
\setbeamertemplate{footline}{}
\frame {
\titlepage
}
}
\begin{frame}{Outline}
\tableofcontents
\end{frame}
\AtBeginSection[]{
\begin{frame}{}
\vfill
\centering
\begin{beamercolorbox}[sep=8pt,center,shadow=true,rounded=true]{title}
\usebeamerfont{title}\insertsectionhead\par%
\end{beamercolorbox}
\vfill
\end{frame}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Administration/Organization}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Important dates}
\begin{block}{Lectures}
\coursedate, \courselocation
\end{block}
\begin{block}{Grading}
\begin{itemize}
\item Homework 30\%
\item Project 20\%
\item Midterm exam 20\%
\item Final exam 30\%
\end{itemize}
\end{block}
\begin{block}{Exams}
\begin{itemize}
\item Midterm exam: 13.10 during lecture
\item Final exams: 10.12 from 12:30 to 2:30
\end{itemize}
\end{block}
\centering
More: \href{\coursesyllabus}{Syllabus} and \href{\coursetimeline}{Timeline}.
\end{frame}
\begin{frame}{Reading}
\begin{block}{Course's books}
\begin{itemize}
\item Andrew, Koenig. Accelerated C++: practical programming by example. Pearson Education India, 2000.
\item Stroustrup, Bjarne. Programming: principles and practice using C++. Pearson Education, 2014.
\end{itemize}
\end{block}
\begin{block}{Assistance C++ basics }
\begin{itemize}
\item Stroustrup, Bjarne. A Tour of C++. Addison-Wesley Professional, 2018.
\item O'Dwyer, Arthur. Mastering the C++17 STL. Packt Publishing Ltd; 2017.
\end{itemize}
\end{block}
\end{frame}
\begin{frame}{Submitting home work}
\begin{block}{Theory exercises}
At the beginning of the lecture in printed form
\end{block}
\begin{block}{Programming exercises}
\begin{itemize}
\item Github Classroom\footnote{\tiny\url{https://www.diehlpk.de/blog/githubclassroom/}} for submission of the programming exercises and the course project.
\item Juypter Server\footnote{\tiny\url{\courseserver}} to work in your browser on the exercises and course project\footnote{\tiny\url{https://www.diehlpk.de/blog/jupyter-notebooks/}}.
\end{itemize}
\end{block}
Note that we use these tools the first time for this course. We anticipate to do a short survey at the end of the semester.
\end{frame}
\begin{frame}{Communication-Intensive (C-I) course}
\begin{block}{Mode I: Written}
\begin{itemize}
\item Learn how to write C++ standard confirm code
\item Learn how to write proper documentation
\item Use the pieces of the assignments to code the course project
\end{itemize}
\end{block}
\begin{block}{Mode II: Technological}
\begin{itemize}
\item Use GitHub for remote collaborative software development
\item Translate mathematical and algorithms into C++ code
\end{itemize}
\end{block}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Getting started}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{A small C++ program}
\lstinputlisting{cpp/lecture1-main.cpp}
\begin{block}{Compile}
\lstinputlisting[language=bash,firstline=2, lastline=2]{cpp/lecture1-main.sh}
\end{block}
\begin{block}{Run}
\lstinputlisting[language=bash,firstline=3, lastline=3]{cpp/lecture1-main.sh}
\end{block}
\end{frame}
\begin{frame}{Structure of a C++ program}
\lstinputlisting{cpp/lecture1-main.cpp}
\only<1>{
\begin{block}{Comments~\cite{kernighan1974elements}}
\begin{itemize}
\item A one line comment starts with $//$
\item A comment over multiple lines starts with $/*$ and ends with $*/$
\item Comments are important to understand the program, especially if the code is shared
\end{itemize}
\end{block}
}
\only<2>{
\begin{block}{Include directives}
\begin{itemize}
\item Is needed to include functionality of the C++ standard library, e.g. IO, which is not part of the core language
\item To include functionality of external libraries or structure your own code
\end{itemize}
\end{block}
}
\only<3>{
\begin{block}{Main function}
\begin{itemize}
\item Every C++ program needs a function called main returning an integer value
\item Return zero means success and any other value indicates failure
\item When we execute any C++ program the main function is invoked and all instructions are executed
\end{itemize}
\end{block}
}
\only<4>{
\begin{block}{\lstinline|return| statement }
\begin{itemize}
\item The value of the return statement is passed to the program, which called the function
\item One function can have multiple return statements
\end{itemize}
\end{block}
}
\end{frame}
\begin{frame}{Built-in types\footnote{\tiny\url{https://en.cppreference.com/w/cpp/language/types}}}
\begin{block}{Integer types}
\begin{itemize}
\item \lstinline|bool| Representation of truth values: \lstinline|true| or \lstinline|false|
\item \lstinline|unsigned| Integral type for non-negative values only
\item \lstinline|short| Integral type that must hold at least 32 bits
\item \lstinline|long| Integral type that must hold at least 64 bits
\item \lstinline|size_t| Unsigned Integral type
\end{itemize}
\end{block}
\begin{block}{Floating points}
\begin{itemize}
\item \lstinline|float| Single precision floating point type
\item \lstinline|double| Double precision floating point type
\item \lstinline|long double| Extended precision floating point type
\end{itemize}
\end{block}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Looping and counting}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Using loops and counting}
\only<1>{
\begin{block}{Compute the sum of $1,\ldots,n$}
\begin{align*}
result = \sum\limits_{i=1}^n i
\end{align*}
\end{block}
}
\begin{block}{Using the loop statement\footnote{\tiny\url{https://en.cppreference.com/w/cpp/language/for}}}
\begin{lstlisting}
size_t result = 0;
for(size_t i = 1; i != 5; i++){
result = result + i;
}
\end{lstlisting}
\only<2>{
\begin{block}{Condition}
\begin{itemize}
\item The variable i is only available inside the loop's body
\item The loop will execute the statements in the curly braces until i is equal to 5
\item The value of i is incremented after all statements are executed
\item \lstinline|i++| is equivalent to \lstinline|i = i+1|
\end{itemize}
\end{block}
}
\end{block}
\end{frame}
\begin{frame}[fragile]{The while statement\footnote{\tiny\url{https://en.cppreference.com/w/cpp/language/while}}}
\begin{lstlisting}
size_t result = 0;
size_t i = 1;
while (i != 5 ) {
result += i;
i++;
}
\end{lstlisting}
\only<1>{
\begin{block}{Condition}
\begin{itemize}
\item \lstinline|i != 5| the statement within the curly braces will be repeated five times
\item \lstinline|!=| is the inequality operator and once \lstinline|i| is equal to \lstinline|5| the loop stops
\end{itemize}
\end{block}
}
\end{frame}
\begin{frame}[fragile]{Conditionals\footnote{\tiny\url{https://en.cppreference.com/w/cpp/language/if}}}
\only<1>{
\begin{block}{Compute the sum of $f(i)$ for $i=1,\ldots,n$}
\begin{align*}
result = \sum\limits_{i=1}^n f(i) \text{ with } f(i) =
\begin{cases}
i, \text{ if } i \text{ is even} \\
i^2, \text{ else}
\end{cases}
\end{align*}
\end{block}
}
\begin{lstlisting}
size_t result = 0;
for(size_t i = 1; i != 5; i++){
if(i % 1 == 0)
result = result + i;
else
result = result + i * i;
}
\end{lstlisting}
\only<2>{
\begin{block}{if statement}
\begin{itemize}
\item If the condition is \lstinline|true| the statements in the \lstinline|if| branch are executed
\item If the condition is \lstinline|false| the statements in the \lstinline|else| branch are executed
\end{itemize}
\end{block}
\begin{block}{Logical operator}
\begin{itemize}
\item \lstinline|\%| Modulo operator for integers
\end{itemize}
\end{block}
}
\end{frame}
\begin{frame}{Operators\footnote{\tiny\url{https://en.cppreference.com/w/cpp/language/operator_precedence}}}
\begin{block}{Logical operators}
\begin{itemize}
\item \lstinline|&&| Logical and
\item \lstinline|||| Logial or
\item \lstinline|!x| Logical negation
\end{itemize}
\end{block}
\begin{block}{Comparison operators}
\begin{itemize}
\item \lstinline|==| Compares to equal
\item \lstinline|!=| Compares to unequal
\item \lstinline|<| Compares to be less
\item \lstinline|>| Compares to be higher
\item \lstinline|<=| Compares to be less or equal
\item \lstinline|>=| Compares to be higher or equal
\end{itemize}
\end{block}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Working with strings}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Reading strings}
\only<1>{
\lstinputlisting{cpp/lecture1-string-io.cpp}
}
\only<2>{
\lstinputlisting[firstline=3, lastline=3]{cpp/lecture1-string-io.cpp}
\lstinputlisting[firstline=9, lastline=9]{cpp/lecture1-string-io.cpp}
\begin{block}{Variables: Definition}
\begin{itemize}
\item Variables have a name (name) and a type (\lstinline|std::string|)
\item We need to include the string type, since it is not in the core language
\item We just defined the variable and currently it is a empty or null string
\end{itemize}
\end{block}
}
\only<3>{
\lstinputlisting[firstline=10, lastline=10]{cpp/lecture1-string-io.cpp}
\begin{block}{Variables: Initialization}
\begin{itemize}
\item Now we initialize the string by reading from \lstinline|std::cin| and assigning the value to it
\item The \lstinline|<<| operator writes a string to \lstinline|std::cout|
\item The \lstinline|>>| operator reads a string to \lstinline|std::cin|
\end{itemize}
\end{block}
Variables can be defined in three different ways:
\begin{itemize}
\item \lstinline|std::string name = "Peter Pan";|
\item \lstinline|std::string name; // empty string|
\item \lstinline|std::string stars(3,'*')| // string of three stars
\end{itemize}
\vspace{0.25cm}
More details: \tiny\url{https://en.cppreference.com/w/cpp/string/basic_string}
}
\end{frame}
\begin{frame}[fragile]{More functionality of strings }
\begin{lstlisting}
const std::string greetings = "Hi, " + name + "!";
\end{lstlisting}
\begin{block}{Concatenation}
\lstinline|+| operator combines string
\end{block}
\begin{block}{Defining constants}
\lstinline|const| operator to make the promise that we will not change the value later
\end{block}
\begin{lstlisting}
const size_t length = greetings.size();
\end{lstlisting}
\begin{block}{Getting the size}
\lstinline|.size()| operator to get the string's size
\end{block}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Summary}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Summary}
\begin{block}{After this lecture, you should know}
\begin{itemize}
\item Structure of a C++ program
\item Handling strings
\item Loops and counting
\item Conditionals
\item Operators
\item Built-in types
\end{itemize}
\end{block}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{References}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[t, allowframebreaks]
\frametitle{References}
\bibliographystyle{plain}
\bibliography{bib}
\end{frame}
\end{document}