| Instructor: | David Goldschmidt, Ph.D. |
|---|---|
| Office Hours: | after class |
| Email: | click here to email me |
// helloworld.cpp <== click here for file // First program in C++ #include <iostream> using namespace std; int main() { // std::cout << "Hello folks!" << std::endl; cout << "Hello folks!" << endl; // (cout << "Hello folks!") << endl; // cout << endl; return 0; }
Basic Syntax:
The Standard Library:
Expressions:
std::cout << "Hello folks!" << std::endl;
std::cout << "Hello folks!"
std::cout << std::endl;
C++ vs. Java:
public class HelloWorld
{
public static void main( String[] args )
{
System.out.println( "Hello folks!" );
}
}
Our second introductory example converts a Fahrenheit temperature to a Celsius temperature and decides if the temperature is above the boiling point or below the freezing point.
// tempconversion.cpp <== click here for file #include <iostream> using namespace std; // Eliminates the need for std:: int main() { // Request and input a temperature. cout << "Please enter a Fahrenheit temperature: "; float fahrenheit_temp; cin >> fahrenheit_temp; // Convert it to Celsius and output it. float celsius_temp = (fahrenheit_temp - 32) * 5.0 / 9.0; cout << "The equivalent Celsuis temperature is " << celsius_temp << " degrees.\n"; // Output a message if the temperature is above boiling or below freezing. const int BoilingPointC = 100; const int FreezingPointC = 0; if ( celsius_temp > BoilingPointC ) { cout << "That is above the boiling point of water.\n"; } else if ( celsius_temp < FreezingPointC ) { cout << "That is below the freezing point of water.\n"; } return 0; }
Variables and Constants:
Expressions, Assignments, and Statements:
float celsius_temp = (fahrenheit_temp - 32) * 5.0 / 9.0;
Conditional statements:
if ( conditional-expression ) {
statement;
} else {
statement;
}
A Julian Day simply tells us the number of days that have passed since the beginning of the given year. For example, the Julian Day corresponding to May 18, 2009 is 138 (or 31+28+31+30+18).
// julianday.cpp <== click here for file #include <iostream> using namespace std; const int DaysInMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // The following function returns true if the given year is a leap year // and returns false otherwise. bool is_leap_year( int year ) // Pass by value { return year % 4 == 0 && ( year % 100 != 0 || year % 400 == 0 ); // A leap year occurs every four years, but not when a year is // divisible by 100; however, if a year is divisible by 400, it // actually is a leap year. See Wikipedia! } // Calculate and return the Julian day associated with the given // month and day of the year. int julian_day( int month, int day, int year ) // Pass by value { int jday = 0; // initialize ; condition ; update for ( unsigned int m = 1 ; m < month ; ++m ) { jday += DaysInMonth[ m - 1 ]; if ( m == 2 && is_leap_year( year ) ) ++jday; // February 29th // <== the "update" part happens here (i.e. ++m or m++) } jday += day; return jday; } int main() { // DaysInMonth[15] = 30; <== will compile and run, but behavior is unpredictable cout << "Please enter three integers (a month, a day and a year): "; int month, day, year; cin >> month >> day >> year; cout << "That is Julian day " << julian_day( month, day, year ) << endl; return 0; }
Arrays and Constant Arrays:
double a[15];
int i = 5; a[i] = 3.14159; // assign 3.14159 to index 5 (6th position) of the array a[7] = 2.78183; // assign 2.78183 to index 7 (8th position) of the array
const int DaysInMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
DaysInMonth[0] = 31; DaysInMonth[1] = 28; DaysInMonth[2] = 31; // ... DaysInMonth[10] = 30; DaysInMonth[11] = 31;
Functions and Arguments:
int julian_day( int month, int day, int year )
Loops:
for ( expr1 ; expr2 ; expr3 ) {
statement;
}
for ( unsigned int m = 1 ; m < month ; ++m )
{
jday += DaysInMonth[ m - 1 ];
if ( m == 2 && is_leap_year( year ) ) ++jday; // February 29th
}
// rewrite above for loop as a while loop:
unsigned int m = 1;
while ( m < month )
{
// same code from above
jday += DaysInMonth[ m - 1 ];
if ( m == 2 && is_leap_year( year ) ) ++jday; // February 29th
++m;
}
When is a Leap Year a Leap Year?
return year % 4 == 0 && ( year % 100 != 0 || year % 400 == 0 );
// scope.cpp <== click here for file #include <iostream> using namespace std; int main() { int a = 5, b = 10; // SCOPE "A" int x = 15; // New SCOPE "B" { double a = 1.5; b = -2; int x = 20; int y = 25; cout << "a = " << a << ", b = " << b << '\n' << "x = " << x << ", y = " << y << endl; } // SCOPE "B" ENDS! So a, x, y are "gone" // ADD here within scope "A": int y = 400; cout << "a = " << a << ", b = " << b << '\n' << "x = " << x << ", y = " << y << endl; return 0; }
Scope:
// monthdayfunction.cpp <== click here for file // Compute the month and day corresponding to the Julian day within // the given year. void // pass by reference month_and_day( int julian_day, int year, int &month, int &day ) { bool month_found = false; month = 1; // Loop through the months, subtracting the days in this month // from the Julian day, until the month is found where the // number of remaining days is less than or equal to the total // days in the month. while ( !month_found ) { // Calculate the days in this month by looking it up in the // array. Add one if it is a leap year. int days_this_month = DaysInMonth[ month - 1 ]; if ( month == 2 && is_leap_year( year ) ) ++days_this_month; if ( julian_day <= days_this_month ) month_found = true; // Done! else { julian_day -= days_this_month; ++month; } } day = julian_day; }
Value Parameters and Reference Parameters:
month_and_day( julian, year, month, day_in_month );
void month_and_day( int julian_day, int year, int &month, int &day )
Arrays as Function Arguments:
void
do_it( double a[], int n )
{
for ( int i = 0 ; i < n ; i++ )
{
if ( a[i] < 0 )
a[i] = 0;
}
}
void swap( double x, double &y )
{
double temp = x;
x = y;
y = temp;
}
int main()
{
double a = 15.0, b = 20.0;
cout << "a = " << a << ", b = " << b << endl;
swap( a, b );
cout << "a = " << a << ", b = " << b << endl;
return 0;
}