Notes week of Sep 29

Muddiest points & want to learn

2.6.1 Challenge

One trick here was that they used int throughout to represent costs, so the unit is cents, not dollars. That is, $3.65 would be represented as the integer 365.

Next, they want you to define COST_PER_POUND as a constant that is independent of the item weight. Here’s a solution:

const int COST_PER_POUND = 25;
shipCost = FLAT_FEE + COST_PER_POUND * shipWeight;

2.2.1 Trace the variable value was a bit confusing.

Did this in class.

2.11.2 challenge: Singing the alphabet. How do I make it print ab? I can only get it to print aa.

There are a couple ways to do this. Just using letter+1 doesn’t work, because it gets promoted to an integer. So if you type in a for letter, then writing letter+1 gives you 97+1 and prints 98 rather than b.

One way to force the value to be a character, is to declare a char variable such as next, below. Another way is to use static_cast (from section 2.9).

#include <iostream>
using namespace std;
int main()
{
    char letter;
    cout << "Enter a letter: ";
    cin >> letter;

    char next = letter+1;
    cout << letter;
    cout << next;
    // Alternative: casts (see section 2.9)
    cout << static_cast<char>(letter+1);
    // This changes it from a number (int) to <char>
    cout << static_cast<char>(65);

    return 0;
}

2.12.1: Reading and printing a string. I don’t get it… maybe I misunderstood the directions?"

#include <iostream>
using namespace std;

int main() {
    string userWord;
    int userNum = 0;

    cout << "Enter word and num: "; // Amy   5
    cin >> userWord;
    cin >> userNum;

    //cout << "Test output: " << userWord << userNum << endl;

    cout << userWord << "_" << userNum << endl;

    return 0;
}

Activity 2.8.2 Calculating math functions

ceil means round up, floor means round down, fabs is absolute value. There is also a function abs, but it’s used only for integers.

Challenge 2.5.2, gravitation.

Previously we used 6373 for the radius of the earth (in km), that’s approximately the same as the value (in meters) they use in this program.

6373 km = 6,373,000 meters

6.38e6 = 6,380,000.

The formula is implemented as:

accelGravity = G * M / pow(distCenter,2);

Challenge homework 2.16.2, on random numbers.

Part of the trick here is that they want you to use seedVal for the seed in the pseudo-random number generator, rather than using time(0). That way they can reproduce the answers in the test cases deterministically.

The next part is to make sure you are using the correct operation on rand(). How do you ensure that whatever integer returns is converted to the range 0 to 9?

“Why is math so confusing? That Haversine formula really had me stuck, even though the answer was in front of me the whole time.”

Here is an interesting perspective on this, from Ed Frenkel and Numberphile. He has a recent book called Love and Math that’s in my queue, but I haven’t read it yet.

getline-demo.cpp

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string name1, name2;
    // Normally, the syntax for input is:
    cout << "Enter your name: ";
    getline(cin, name1);
    // But for strings, that just reads one word at a time
    // We can read an entire line (up to the user pressing
    // enter) using this syntax:
    cout << "Enter your name again: ";
    cin >> name2;

    cout << "I got '" << name1 << "' and '"
         << name2 << "'" << endl;
    return 0;
}

if-else-chain.cpp

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

    // if-else chain:
    if(unit == "yd") {
        // something to convert yards
    }
    else if(unit == "mi") {
        // something to convert miles
    }
    else if(unit == "km" || unit == "kilometer"){
        // Above uses "or" (||) to support multiple names for the
        // same unit (which is optional).
        // convert km
    }
    else {
        // ERROR!
    }
}