Notes week of Oct 6

Muddiest points & want to learn

Numeric bases

Table 2.11.1 does char only store the decimal value (or can it store the binary value also). if char is only used for decimal values what variable can be used to store binary? (If even possible.)

Internally, everything is stored in binary. That’s why the maximum int is something like 2,147,483,647 (2^{31}-1) rather than 999,999,999. You can, however, write numbers in bases other than ten. C++ understands both hexadecimal (using a 0x prefix) and octal (using a 0 prefix). So take a look at this surprising result. The outputs of each cout are shown in comments. The hexadecimal number 3A is what we normally write as 58. And likewise, the octal number 317 is the normal (base ten) number 207.

#include <iostream>
using namespace std;
int main()
{
    // Provide numbers in various bases
    int a = 0x3A;
    int b = 0317;
    int c = 412;

    cout << a << endl; // 58
    cout << b << endl; // 207
    cout << c << endl; // 412

    int d = 980;
    // Output number in various bases
    cout << hex << d << endl;   // 3d4
    cout << oct << d << endl;   // 1724
    cout << dec << d << endl;   // 980
    return 0;
}

You can also convince cout to output values in hexadecimal and octal by prefixing the value with the hex or oct (or dec) configuration, as shown in the second set of output statements above.

we should do a program using binary to decimal (vice-verca) conversions

We can! Once we learn loops.

Chapter 1

Activity 1.4.1: Read user input and print to output.

Activity 1.4.2: Read multiple user inputs.

Chapter 2

Activity 2.2.1

Activity 2.2.2: Assigning a sum

Activity 2.2.3

activity 2.11.2 is a bit confusing for the user input part

challenge HW 2.11.2 & 2.12.1 not clear on what the program is asking for

activity 2.13.2 should have specified where exactly to place the ‘long long’ (at the beginning - replacing ‘int’ before the original value of num)"

Yes, long and long long are types, so when you declare the variable, you write them in place of int:

long long my_bank_balance = 987654321987654321;

2.16.1 Randomness

TODO: can revisit this later.

Chapter 3

activity 3.1.6, #3 is a bit confusing in understanding which line executes and which does not.

This has to do with not using braces to mark the if or else body. Indentation doesn’t matter, except to humans, so if there are no braces than just the first statement is tied to the preceding if or else.

Challenge Homework Activity 3.2.2 was a little odd. The program asks for the student code, to which I enter: (userNum >= 17) or (userNum == 17). It passes 2 out of 3 tests. What did I do wrong?

You don’t want to rely on that value 17 for userNum, because in some of the test cases they’ll change the value. So you can test for odd/even by using userNum%2, and specifically, userNum%2==1 to test for odd. However, that has a problem with negative numbers, as described in the next answer:

homework 3.2.2: did some research and came up with the student code being: userNum %2 != 0 is this the best recommended way?

Has to do with modulus of a negative number. (One of their test cases is -999.) See the mod-neg program below.

homework 3.2.3: fixing the code simply involved changing the = to ==, which I was able to figure out quickly. My question though is why exactly does the single = cause there to be an error (referring back to the original int userNum = 0)? what type of error is this considered? (syntax… etc)

Challenge Homework 3.3.1 was also tough.

Other

About the <student portion> – having us only do that one segment of code instead of the entire program from start to finish takes away from learning how each new topic should be implemented into our learning/coding abilities.

You can always switch to a full coding environment and copy/paste what you were given. But the reason they do it that way is so that they can tweak the surrounding program code to produce different test cases.

Just have to keep reviewing and practicing these topics!

Yes!

p4-template.cpp

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

    float quantity = 0;
    cout << "Enter a quantity: ";
    cin >> quantity;

    float meters = 0;  // Intermediate unit

    // if-else chain:
    if(unit == "yd") {
        // something to convert yards
        cout << "SHOULD CONVERT TO YARDS" << endl;
        meters = quantity / 1.0936;
    }
    else if(unit == "mi") {
        // something to convert miles
        cout << "SHOULD CONVERT TO MILES" << endl;
        // TODO: meters = .....
    }
    else if(unit == "m") {
        meters = quantity;
    }
    else if(unit == "km" || unit == "kilometer"){
        // Above uses "or" (||) to support multiple names for the
        // same unit (which is optional).
        // convert km
        cout << "SHOULD CONVERT TO KM" << endl;

    }
    else {
        cout << "ERROR: Unknown unit: " << unit << endl;
        return 1; // Quit the program early, don't do the rest
        // ERROR!
    }

    // AFTER THAT CHAIN OF if-else, the variable 'meters'
    // should contain the quantity converted to meters.
    cout << "Intermediate value is " << meters << " meters." << endl;

    string target;
    cout << "Enter target unit: ";
    cin >> target;

    float result = 0;
    // Do another series of if-else statements for the bottom
    // half (output unit conversion).
    // TODO:
    if(target == "yd") {
        // Either set a variable like 'result' and output it later
        // (preferred), OR you can just output the answer now:
        result = meters * 1.0936;   // preferred
        cout << meters*1.0936 << " yards";
    }

    // AFTER the entire if-else chain,
    cout << result << " " << target << endl;

    return 0;
}

mod-neg.cpp

Exploring the mod of a negative number.

#include <iostream>
using namespace std;
int main()
{
    cout << "16 % 2: "  << 16%2  << endl;
    cout << "17 % 2: "  << 17%2  << endl;
    cout << "-16 % 2: " << -16%2 << endl;
    cout << "-17 % 2: " << -17%2 << endl;
    return 0;
}

The results are:

16 % 2: 0
17 % 2: 1
-16 % 2: 0
-17 % 2: -1

switch2.cpp

Example with switch.

#include <iostream>
using namespace std;
int main()
{
    // Want to print a date, like:
    // 4th of July
    // 8th of October
    // 21st of December
    int day = 21; // TODO: ask user for day
    int month = 9; // TODO: ask user for month

    cout << "Enter a month number: ";
    cin >> month;

    cout << "Enter a day number: ";
    cin >> day;

    cout << day;

    switch(day) {
    case 1: case 21: case 31: // fall-through
        cout << "st";
        break;
    case 2: case 22:
        cout << "nd";
        break;
    case 3: case 23:
        cout << "rd";
        break;
    default:
        cout << "th";
    }

//    if(day == 21 || day == 1 || day == 31) {  // 1
//        cout << "st";
//    }
//    else if(day == 23 || day == 3) {
//        cout << "rd";
//    }
//    else if(day == 22 || day == 2) {
//        cout << "nd";
//    }
//    else {
//        cout << "th";
//    }

    cout << " of ";

    switch(month) {
    case 1:
        cout << "January";
        break;
    case 2:
        cout << "February";
        break;
    default: // Default (if used) must be last case
        cout << "December";
        // break; // not necessary because already the end
    }

//    if(month == 1) {
//        cout << "January";
//    }
//    else if(month == 2) {
//        cout << "February";
//    }
//    else if(month == 12) {
//        cout << "December";
//    }


    return 0;
}

switch-xmas.cpp

Clever use of switch for song lyrics.

#include <iostream>
using namespace std;
int main()
{
    for(int i = 1; i <= 12; i++)
    {
       cout << "On the " << i;
       cout << "th"; // TODO: put the suffix switch here
       cout << " day of Christmas:" << endl;

       switch(i) {
       case 5:
            cout << "  five golden rings" << endl;
       case 4:
            cout << "  four mocking birds" << endl;
       case 3:
            cout << "  three french hens" << endl;
       case 2:
            cout << "  two turtle doves" << endl;
       case 1:
            cout << "  a partridge in a pear tree" << endl;
       }
    }
    return 0;
}