Notes week of Sep 8

day2.cpp

A sample program we did, to illustrate variables, input, output, comments, etc.

// A program by Chris League
#include <iostream>
using namespace std;
// This is a comment.
/* Older comment syntax
   that can span multiple lines.
   */
int main()
{
    float distance; // miles
    cout << "Enter a distance in miles: ";
    cin >> distance;
    float minutes = 90;
    cout << "Enter a minute: ";
    cin >> minutes;
    float speedMPH = distance / (minutes/60);
    cout << "You traveled " << distance
         << " miles in " << minutes
         << " minutes, which is "
         << speedMPH << " mph.";
    // All finished!
    return 0;
}

hours.cpp

A program to convert hours and minutes into a total number of minutes.

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

    int minutes;
    cout << "Enter minutes: ";
    cin >> minutes;

    int totalMinutes = hours*60 + minutes;
    cout << "That is "
         << totalMinutes
         << " minutes.";

    return 0;
}

It’s also an interesting problem to do this same operation in reverse: 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.

broken.cpp

A broken program that you were supposed to fix in class.

#include <iostrean>
using namespace std

int main[]
{
    int x = 40
   cout >> “Hello, this is a test.” << endl;
   cout >> x*2;
   float y = x / 3;
   cout >> y;
}