Notes week of Sep 15

Meaning of sine/cosine

The C++ functions sin and cos require their parameters to be in radians, but our user is entering latitude and longitude in degrees. The conversion factor is that 360° = 2π radians.

A unit circle diagram (full size)

A unit circle diagram (full size)

More about degrees, radians, sine, cosine (full size)

More about degrees, radians, sine, cosine (full size)

Muddiest points & want to learn

At the end of 1.2 where it has the “challenge homework” the instructions as to what we should or should not write are confusing.

Whatever you type into the box is copied and pasted into the complete program in place of <STUDENT CODE> before it is compiled and run.

Do all statements need to be put on their own line with cout or can they be put into one line ( such as with the <<endl; )

You’ve probably learned this by now, but yes we can output multiple items within the same cout statement by repeating the << operator over and over. Compare these, and pay close attention to where semi-colons are used (or not used):

cout << "The distance is ";
cout << dist;
cout << " km.";
cout << endl;
// vs
cout << "The distance is " << dist
     << " km." << endl;

Activity 1.9.4 was a bit confusing to understand.

The (fake) assembly language instruction Mul 97, #9, 98 means: “Take the value in memory location 97, multiply it by the number 9, and store the result in memory location 98.

Table 1.9.1 was a little confusing. How would that look in the program?

These are more assembly language instructions. They are not typically used in C++ programs at all. This section is supposed to give you background into what the compiler is doing behind the scenes.

In using namespace std;, what does std stand for exactly? Is it an abbreviation?

It’s just an abbreviation of “standard”, so we’re using the standard namespace that contains all the built-in library components of C++. Some third-party libraries put things in namespaces with different names.

Learning the difference of single line comments and multi line comments.

Single-line comments begin with // and continue until the end of that line. Therefore, anything following // on a line is ignored.

Multi-line comments start with /* and continue until the compiler sees the sequence */, which might (or might not) be on a subsequent line.

Solution to hours/minutes problem

Previously, we posed this problem: from a total number of minutes, split it into hours and minutes. For example, 384 minutes is 6 hours and 24 minutes:

Enter total number of minutes: 384
That is 6 hours and 24 minutes.

Here is the program we developed to do this.

#include <iostream>
using namespace std;
int main()
{
    const int minutesPerHour = 60;
    cout << "Enter total number of minutes: ";
    int minutes;
    cin >> minutes;
    int h = minutes / minutesPerHour;
    int m = minutes - minutesPerHour * h;
    cout << "That is " << h << " hours and "
         << m << " minutes." << endl;
    return 0;
}