| Instructor: | David Goldschmidt, Ph.D. |
|---|---|
| Office Hours: | after class |
| Email: | click here to email me |
Binary Search:
v[0] ≤ v[1] ≤ v[2] ≤ ... ≤ v[n-1]
// Here is the recursive function. If x is in the vector, then it
// must be located within the subscript range low to high. Therefore,
// when low and high are equal, their common value is the only possible
// place for x. Otherwise, the middle value is checked, and the search
// continues recursively in either the lower or upper half of the given
// vector.
//
bool
binsearch( const vector<double> &v, int low, int high, double x )
{
if ( high == low ) {
return x == v[low]; // did we find x?
}
int midpoint = ( low + high ) / 2;
// recursively call binsearch() with half the given vector
if ( x <= v[midpoint] ) {
return binsearch( v, low, mid, x );
} else {
return binsearch( v, mid + 1, high, x );
}
}
// The driver function establishes the search range for the value of x
// based on the minimum and maximum subscripts in the vector.
//
bool
binsearch( const vector<double> &v, double x )
{
return binsearch( v, 0, v.size() - 1, x );
}
Exercises:
Pointers:
int x = 10;
int *p;
p = &x;
*p = 72;
if ( x > 20 ) {
cout << "x is greater than 20\n";
} else {
cout << "x is not greather than 20";
}
Operations on Pointers:
float x = 5, y = 9; float *p = &x, *q = &y; *p = 17.0; *q = *p; q = p; *q = 13.0; cout << x << endl; cout << y << endl;
int *r; r = q; // Illegal: different pointer types p = 35.1; // Illegal: float assigned to a pointer
if ( p == q ) {
// Both pointers point to the same variable
}
if ( p != q ) {
// Pointers point to different memory locations
}
int x = 10, y = 15; int *a = &x; cout << x << " " << y << endl; int *b = &y; *a = x * *b; cout << x << " " << y << endl; int *c = b; *c = 25; cout << x << " " << y << endl;
Null Pointers:
if ( p != NULL ) {
cout << *p << endl;
}
Accessing Arrays with Pointers:
// Using []
const int n = 10;
double a[n];
for ( int i = 0 ; i < n ; i++ ) {
a[i] = sqrt( double( i ) ); // cast i to type double
}
// Using * and pointer arithmetic
const int n = 10;
double a[n];
for ( double *p = a ; p < a + n ; p++ ) {
*p = sqrt( p - a );
}
p = &a[0];
Exercises:
For each of the following problems, use only pointers (no array subscripting):
C-Style Strings vs. C++ Strings:
cout << "Hello!" << endl;
char h1[] = "Hello!";
char h2[] = { 'H', 'e', 'l', 'l', 'o', '!', '\0' };
string s1( "Hello!" ); string s2( h1 );
Defining a Data Type (a.k.a. a Class):
// Date.h <== click here for file class Date { private: // Begin with private member variables int day; int month; int year; public: // Constructors Date(); Date( int m, int d, int y ); // Mutator functions void setDay( int d ); void setMonth( int m ); void setYear( int y ); void increment(); // Accessor functions (often defined using const) int getDay() const; int getMonth() const; int getYear() const; bool isEqual( const Date &date2 ) const; // same day, month, year? bool isLeapYear() const; bool isLastDayInMonth() const; int lastDayInMonth() const; void print() const; }; bool sameDayAndMonth( const Date &date1, const Date &date2 );
// Date.cpp <== click here for file #include <iostream> using namespace std; #include "Date.h" Date::Date() // default constructor { month = 1; // default date is 1/1/2000 day = 1; year = 2000; } Date::Date( int m, int d, int y ) { month = m; day = d; year = y; } void Date::setDay( int d ) { day = d; } void Date::setMonth( int m ) { month = m; } void Date::setYear( int y ) { year = y; } void Date::increment() { if ( !isLastDayInMonth() ) { day++; } else { day = 1; if ( month == 12 ) // December { month = 1; year++; } else { month++; } } } int Date::getDay() const { return day; } int Date::getMonth() const { return month; } int Date::getYear() const { return year; } bool Date::isEqual( const Date& date2 ) const { if ( day == date2.day && month == date2.month && year == date2.year ) { return true; } else { return false; } } bool Date::isLeapYear() const { return year % 4 == 0 && ( year % 100 != 0 || year % 400 == 0 ); } bool Date::isLastDayInMonth() const { return ( day == lastDayInMonth() ); } const int DaysInMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int Date::lastDayInMonth() const { if ( month == 2 && isLeapYear() ) { return 29; } else { return DaysInMonth[ month - 1 ]; } } void Date::print() const { cout << month << '/' << day << '/' << year; } bool sameDayAndMonth( const Date &date1, const Date &date2 ) { return date1.getDay() == date2.getDay() && date1.getMonth() == date2.getMonth(); }
Class Scope Notation:
Constructors:
Constructors are special functions that initialize the values of the member variables. You have already used constructors for string and vector objects.
Member Functions:
Accessing and Changing Member Variables:
Non-member Functions:
Constant Member Functions:
Exercises:
Main Code:
// date_main.cpp <== click here for file #include <iostream> #include <vector> using namespace std; #include "Date.h" int another_function(); int main() { int aMonth = 5, aDay = 27, aYear = 2009; // Create a Date object: Date today( aMonth, aDay, aYear ); // this calls the Date() constructor // that has 3 integer arguments Date d; // NO PARENTHESES HERE! int x = another_function(); cout << "Today's date is: "; today.print(); cout << endl; today.increment(); cout << "Today is now tomorrow: "; today.print(); cout << endl; Date bday( 5, 2, 2010 ); for ( int i = 0 ; i < 365 ; i++ ) { cout << "Next day: "; today.increment(); today.print(); if ( bday.isEqual( today ) ) { cout << " (my birthday!)"; } cout << '\n'; } vector<Date> v; v.push_back( Date( 4, 10, 1988 ) ); v.push_back( Date( 2, 28, 2002 ) ); v.push_back( today ); v.push_back( bday ); for ( int i = 0 ; i < v.size() ; i++ ) { v[i].print(); cout << ' '; } cout << endl; // TODO: sort the vector.... return 0; }