Notes week of Sep 22

Muddiest points & want to learn

Modulo

The concept of “modulo” was a bit confusing.

We did some exercises with modulo operator (%). See my 3-minute video about it.

The explanation states that any odd number % 2 is 1, while any even number % 2 is 0. Is that the part we discussed in class? Keep subtracting the number after % to get the remainder.

Yes. Let’s use the subtraction technique on 7%2 (it can take a long time for big numbers!). You repeatedly subtract 2 until the result is less than 2. So here, we have the sequence 7, 5, 3, 1; so the answer is 1. Compare that to 6%2, the sequence is 6, 4, 2 (still not less than 2), 0.

Floats, doubles

when to use a double floating point.

float vs double: difference is just the precision (how many digits beyond the decimal point (in scientific notation). Generally, float is 32 bits, double is 64 bits. You can think of it as affecting the number of digits following the decimal point in scientific notation:

  • 8.2223 \times 10^{35} (relatively low precision)
  • 8.24987363794 \times 10^{-34} (relatively high precision)

Activity 2.5.1 which was about defining and assigning double variables.

We worked through this activity in class.

Type conversions

Talked about implicit vs explicit conversions. Conversions are implicit if they can be performed without losing information. For example, the integer 4 can be converted to the float 4.0 without losing information. But in reverse, you cannot convert the float 4.5 to an integer without losing information.

Arrows show the implicit conversions

Arrows show the implicit conversions

Activity 2.4.4

Remember, this is about integer division which means we drop the decimal portion. So 13/3 we would normally say is 4.333… but with integer division it is just 4.

The 4/9 is normally 0.444… but throwing away the decimal part it is just 0.

Similarly, in the third problem, (1 / 3) is 0.

Constants

How const can be used when describing a fixed longitude and latitude point

const double latA = 40.690;
const double lonA = -73.980;

Activity 2.6.1

We worked through this activity in class.

Limits

Why does C++ stop computing after 2million/billion? I forget which one it was.

All of the basic types in C++ (int, char, float, double) are limited in the number of bits that are used to represent them. Those limits can vary from one platform to another, but here are some common sizes:

char:   1 bytes,  8 bits, min        -128,          max +127
int:    4 bytes, 32 bits, min -2147483648,          max +2147483647
long:   8 bytes, 64 bits, min -9223372036854775808, max +9223372036854775807
float:  4 bytes, 32 bits
double: 8 bytes, 64 bits

You can derive those minimums and maximums yourself: given b bits, the minimum is -(2^{b-1}) and the maximum is (2^{b-1})-1.

If you need to go higher than 2 billion, there is a type called long that is usually 64 bits. That gets pretty big! Also, there are some third-party libraries (not built in to C++) that can do what is called “arbitrary precision arithmetic” for even bigger numbers – limited only by the amount of memory available to your program.

chardemo.cpp

A sample program to illustrate calculations with the char type.

// chardemo.cpp
#include <iostream>
using namespace std;

int main()
{
    char c = 'A' + 10;          // 75 (K)
    int x = c; // valid conversion? Yes.
    char d = 64;

    cout << c << endl << x << endl;
    cout << d << endl;

    char initial;
    cout << "What is your first initial? ";
    cin >> initial;
    cout << "Welcome, " << initial << "." << endl;
    int nth = (initial - 'A') + 1;
    // 'C' is 67
    // 'A' is 65
    // 'C' - 'A' == 67 - 65 == 2
    cout << "That is the " << nth << "th letter of the alphabet."
         << endl;

    return 0;
}

strdemo.cpp

A sample program to illustrate constants and input with the string type.

// strdemo.cpp

#include <iostream>
using namespace std;
int main()
{
    const string message = "How have you been";
    string name;
    //int x;

    // string z = 'X'; // Valid conversion? NO.

    // cout << "Enter your name: ";
    // cin >> x;
    // cout << "That's great, Emily." << endl;

    cout << "Enter your name: ";
    cin >> name;  // Reads just one word.
    cout << message << ", " << name << endl;

    cout << "Enter your name again: ";
    getline(cin, name); // Reads entire line
    cout << message << ", " << name << endl;

    return 0;
}

ifstring.cpp

A sample program to illustrate using a conditional with a string input.

#include <iostream>
using namespace std;
int main()
{
    cout << "Enter your name: ";
    string name;
    cin >> name;

    if(name == "Chris") {       // Notice! Double equals!
        cout << "Welcome home, sir. May I help you?" << endl;
    }
    else {
        cout << "Get lost, " << name << endl;
    }

    return 0;
}