Notes week of Sep 26

Geo coordinates for Project 3

I introduced project 3 with an overview of the geographic coordinate system.

Longitude are the lines marked on a globe from the north pole to the south pole, also called meridians. One of them is identified as the prime meridian, which goes through the Royal Observatory in Greenwich, England. Longitude demarcations to the east of the prime meridian are given positive numbers, and to the west (including all of the United States) are negative. These wrap around and meet each other at 180°/-180° in the Pacific Ocean.

The lines of latitude include the equator at 0°. Locations north of the equator are positive numbers (up to 90° at the north pole) and south are negative numbers (down to -90° at the south pole).

The origin of this coordinate system is 0,0 which is located off of Africa in the Gulf of Guinea, south of Ghana and west of Gabon. There’s no land there, but it’s sometimes called “Null Island.”

We also looked more closely at representing angles in degrees vs. in radians. A standard scientific calculator can do mathematical functions such as sin/cos/tan in either degrees or radians. Switching between them is what the “DRG” button does.

However, in C++, the functions sin, cos, and tan only support radians. So we must do conversions. The conversion factor is that 360° equals \(2\pi\) radians. Below is an example:

The sine function

The sine function

Suppose we want the value of the sine function at 10°. On the calculator, if it’s in DEG mode, we push 1 0 sin and get 0.173648178.

But if I write a C++ program to do that calculation:

#include <iostream>
#include <cmath>      // Include math functions
using namespace std;
int main()
{
    double x = sin(10);
    cout << x << endl;
    return 0;
}

I will get this output:

-0.544021

What happened? The C++ sin function is assuming the angle is in radians, so it’s giving the value for 10 radians, not 10° (degrees).

We need to do the conversion by multiplying by \(2\pi\) and dividing by 360. In the following program we implement that conversion, but use the cmath constant M_PI for \(\pi\), and also increase the precision of the output stream to ten digits.

#include <iostream>
#include <cmath>      // Include math functions
using namespace std;
int main()
{
    double deg = 10;
    double rad = deg * 2 * M_PI / 360;
    double x = sin(rad);
    cout.precision(10);
    cout << "sin(10) = " << x << endl;
    return 0;
}

and then we get (approximately) the same result of

sin(10) = 0.1736481777

Notes from chapter 2

compound.cpp

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() // Random number demo
{
    srand(time(0)); // Initialize
    cout << rand()%6+1 << endl;
    cout << rand()%6+1 << endl;
    cout << rand()%6+1 << endl;
    cout << rand()%6+1 << endl;
    cout << rand()%6+1 << endl;
    return 0;
}

int stringDemo()
{
    string name = "Anonymous";
    cout << "Enter your name: ";
    //cin >> name;  // Works only for single word
                    // (stops at first space)
    getline(cin, name);
    cout << "Hello, " << name << endl;
    return 0;
}

int charDemo()
{
    char c = 15;
    c *= 4;
    cout << c << endl;
    cout << (int)c << endl;
    int z = 'A' * 2 + '*';
    cout << z << endl;
    return 0;
}

int overflowDemo()
{
    unsigned x = 0; // integer, 32 bits?
    x = 2147483647;
    cout << "Size of int is "
         << sizeof(int)
         << " bytes." << endl;
    cout << x << endl;
    x++;
    cout << x << endl;
    x = 0;
    cout << x << endl;
    x--;
    cout << x << endl;
    return 0;
}

int incrementDemo()
{
    // Demonstrating the increment operator
    int k = 8;
    cout << k << endl;
    k++;  // Add one to k.
    cout << k << endl;
    return 0;
}