Session 1#

Agenda#

  1. OIT, CRC, and DSI Workshop Series

  2. Access to ManeFrame II (M2) and the NVIDIA DGX SuperPOD (MP)

  3. C++ Resources

  4. Tools

  5. C++ Types, Expressions, and Statements

  6. C++ Operators and Conditionals

  7. Excercises

C++ Resources#

C++ Types, Expressions, and Statements#

 1/*
 2 Statements, Expressions, and Variables
 3 
 4 * Open and closed slash-star can be used to make block comments.
 5 * Two consecutive slashes are used to denote a single line comment.
 6 * `#include` is used to reference other files that contain needed functionality
 7 * `using` can be used to indicate a specific namespace or function to be used
 8 * `main` is the entry point to the program, i.e. where the program starts and
 9   it returns an integer. Usually the return integer can denote if the program
10   ended well, e.g. 0, or not well, e.g. > 0.
11 
12 Useful References:
13 * Discovering Modern C++ by Peter Gottschling (amongst many other C++ books)
14 * https://en.cppreference.com/
15*/
16
17// Include statements
18#include <iostream>
19#include <string>
20
21// Namespace declaration statement
22using namespace std;
23
24// Program entry-point, encapsulated by the curly brackets { and }
25int main() {
26    
27    // Print string using `cout` which is short for `std::cout` as the `std`
28    // namespace was declared. Statements end with a semicolon.
29    cout << "Hello, World!" << endl;
30    
31    // C++ is a strongly typed language, i.e. variables have specific types
32    // and these types have a specific precision. Declaring a variable denotes
33    // the variable type, name, and initial value. It is recommended, but not
34    // required to specify an initial value. Note that white space is not part
35    // of the syntax. Below is a table of variable types with the usual the sizes
36    // and value ranges noted for 64-bit Unix using the LP64 data model. Expressions
37    // are used to denote the relationship between variables.
38    // Type       Variable Name   Initial Value     Usual Size on
39    short int                si = -1;               // 16-bits, [-2^15, 2^15-1]
40    unsigned short int      usi =  1;               // 16-bits, [0, 2^16-1]
41    int                       i = -10;              // 32-bits, [-2^31, 2^31-1]
42    unsigned int             ui =  10;              // 32-bits, [0, 2^32-1]
43    long int                 li = -1234567890;      // 64-bits, [-2^63,-2^63-1]
44    unsigned long int       uli =  1234567890;      // 64-bits, [0,-2^64-1]
45    long long int           lli = -1234567890;      // 64-bits, [-2^63,-2^63-1]
46    unsigned long long int ulli =  1234567890;      // 64-bits, [0,-2^64-1]
47    bool                      b =  true;            // 1-bit, true or false, [1, 0]
48    signed char              sc =  'a';             // 8-bits, [-127, 127]
49    unsigned char            uc =  'b';             // 8-bits, [0, 255]
50    char                      c =  'c';             // 8-bits, [-127, 127] (x86_64) or [0, 255] (ARM)
51    float                     f =  3.14159;         // 32-bits, signed decimal number
52    double                    d =  3.14159;         // 64-bits, signed decimal number
53    long double              ld =  3.14159;         // 80-bits (x86_64) or 128-bits (ARM)
54    auto                      a =  d + i;           // Automatic typing based on initialization (a is double)
55    const int                ci =  42;              // Immutable variable
56    string                    s =  "Hello, World!"; // Strings are collections of characters
57    
58    // Literals can be used to define types inline
59    int                 il = i   + 2;    // 2 is signed int
60    unsigned int       uil = ui  + 2u;   // 2 is unsigned int
61    long int           lil = li  + 2l;   // 2 is signed long int
62    unsigned long int ulil = uli + 2ul;  // 2 is unsigned long int
63    float               fl = f   + 2.0f; // 2.0 is float
64    double              dl = d   + 2.0;  // 2.0 is double
65    long double        ldl = ld  + 2.0l; // 2.0 is long double
66    
67    // Return integer for `main`
68    return 0;
69}

C++ Operators and Conditionals#

 1/**
 2 \brief Overview of C++ Operators and Conditionals
 3 
 4 
 5 */
 6
 7// Include statement
 8#include <iostream>
 9#include <string>
10#include <bitset> // bitset
11#include <cmath>  // fmod, remainder
12
13// Namespace declaration statement
14using namespace std;
15
16// Program entry-point, encapsulated by the curly brackets { and }
17int main() {
18
19    // Declare initialized integer variable
20    int n = 42;
21    int m = -2;
22    cout << "We begin with n initialized as " << n
23         << " and m initialized as " << m << "." << endl;
24    
25    // Arithmetic Operators
26
27    // Postfix operators return the original value
28    cout << "Post-increment: n++, " << n++ << " is printed, but n is now " << n << "." << endl;
29    cout << "Post-decrement: n--, " << n-- << " is printed, but n is now " << n << "." << endl;
30
31    // Prefix operators return the modified value
32    cout << "Prefix-increment: ++n, " << ++n << " is printed and n is now " << n << "." << endl;
33    cout << "Prefix-decrement: --n, " << --n << " is printed and n is now " << n << "." << endl;
34    
35    // Unary operators
36    cout << "Unary plus, positive " << +n << " is printed." << endl;
37    cout << "Unary minus, positive " << -n << " is printed." << endl;
38    
39    // Binary operators
40    cout << n * m << endl;           // Multiplication, -84 is printed
41    cout << n / m << endl;           // Division, -21 is printed
42    cout << n % m << endl;           // Modulo (integer only), 0 is printed
43    cout << fmod(n, m) << endl;      // Modulo (floats, truncated), 0.0 is printed
44    cout << remainder(n, m) << endl; // Modulo (floats, rounded), 0.0 is printed
45    cout << n + m << endl;           // Addition, 40 is printed
46    cout << n - m << endl;           // Subtraction, 44 is printed
47    
48    // Boolean operators
49    
50    bool a = true;  // Has value of 1
51    bool b = false; // has value of 0
52
53    // Unary operators
54    cout << !a << endl; // Not, 0 (false) is printed
55    
56    // Binary operators
57    cout << (n >  m) << endl; // Greater than, 1 (true) is printed
58    cout << (n >= m) << endl; // Greater than or equal to, 1 (true) is printed
59    cout << (n <  m) << endl; // Less than, 0 (false) is printed
60    cout << (n <= m) << endl; // Less than or equal to, 0 (false) is printed
61    cout << (n == m) << endl; // Equal to, 0 (false) is printed
62    cout << (n != m) << endl; // Not equal to, 1 (true) is printed
63    cout << (a && b) << endl; // Logical AND, 0 (false) is printed
64    cout << (a || b) << endl; // Logical OR, 1 (true) is printed
65
66    // Bitwise operators
67    
68    bitset<3> i{"100"}; // 3-bit binary representation of 4
69    bitset<3> j{"010"}; // 3-bit binary representation of 2
70
71    cout << ~i       << endl; // One's complement (flips each bit), 011 is printed
72    cout << (i &  j) << endl; // AND, 000
73    cout << (i ^  j) << endl; // Exclusive OR
74    cout << (i |  j) << endl; // Inclusive OR
75    cout << (i << 1) << endl; // Left shift, 000 is printed
76    cout << (i >> 1) << endl; // Right shift, 010 is printed
77
78    // Assignment operators
79
80    int x = 3;
81    int y = 2;
82
83    x   = y; cout << "Assignment, x is now "                                  << x << endl;
84    x  *= y; cout << "Multiplication assignment, x = x * y, x is now "        << x << endl;
85    x  /= y; cout << "Division assignment, x = x / y, x is now "              << x << endl;
86    x  %= y; cout << "Modulo assignment (integer only), x = x % y, x is now " << x << endl;
87    x  += y; cout << "Addition assignment, x = x + y, x is now "              << x << endl;
88    x  -= y; cout << "Subtraction assignment, x = x - y, x is now "           << x << endl;
89    x <<= 1; cout << "Bitwise shift left assignment, x = x << y, x in now "   << x << endl;
90    x >>= 1; cout << "Bitwise shift right assignment, x = x << y, x in now "  << x << endl; 
91    
92    return 0;
93}

Excercises#

Problem 1 from Project Euler.