CSc212AB Data Structures Lecture 2 - PowerPoint PPT Presentation

1 / 90
About This Presentation
Title:

CSc212AB Data Structures Lecture 2

Description:

M.P. Johnson, DS, CCNY, Spring 2005. Personnel. Instructor: Matthew P. Johnson ... Amazon/BN (may be cheaper) Amazon.co.uk (may be cheaper still) Searchable on Amazon ... – PowerPoint PPT presentation

Number of Views:108
Avg rating:3.0/5.0
Slides: 91
Provided by: Zhiga6
Category:

less

Transcript and Presenter's Notes

Title: CSc212AB Data Structures Lecture 2


1
CSc212AB Data StructuresLecture 2
  • Matthew P. Johnson
  • City College of New York
  • Spring, 2005

2
Personnel
  • Instructor Matthew P. Johnson
  • mpjohnson_at_gmail.com
  • Please prepend CSc212 to subject line!
  • Office hours
  • MW 1040-1140
  • NAC 8-202b (share with Prof. Grossberg)
  • please visit!

3
Communications
  • Web page
  • http//www-cs.ccny.cuny.edu/mjohnson/212/
  • Tentative syllabus
  • course policies
  • Homeworks
  • Readings to prepare for class
  • Dont fall behind!
  • Check regularly!

4
Textbook
  • Data Structures and Other Objects Using C
  • by Michael Main, Walter Savitch
  • 3 edition (October 12, 2004)
  • Available
  • CCNY bookstore
  • Amazon/BN (may be cheaper)
  • Amazon.co.uk (may be cheaper still)
  • Searchable on Amazon
  • Links on class page

5
C references
  • Course taught in C
  • All programming homeworks done in C
  • Will assume prior knowledge
  • But will review some topics
  • See http//www.columbia.edu/mpj9/3101-2/ for
    links to C courses and notes
  • Links to my notes on C in that pages READING
    section

6
Mini topic common series
  • SUM(i) n(n1)/2
  • Guass
  • Definitely know this one
  • Rest may sometimes be helpful
  • SUM(2i) 2(n1) 1
  • SUM(Ai) A(n1) 1 / (A-1)
  • Hn SUM(1/i) ln n
  • Harmonic numbers

7
Mini topic logs
  • very important
  • memorize properties if havent already
  • 2. df logbX y ? by x
  • intuit Y is number of bs multiplied together
    yield X
  • i.e., log is inverse of exponentiation function
  • 3. in CS, log base is almost always 2 log x or
    lg x
  • elsewhere, log x sometimes means base 10
  • ln x means base e Eulers number 2.71

8
Log properties may be helpful
  • Xaxb x(ab)
  • Xa/xb x(a-b)
  • (Xa)b x(ab)
  • Xnxn 2xn
  • 2n2n 2(n1)
  • Log(A/B) logA logB
  • Log(AB) BlogA
  • 0 0
  • Log1 0, log 2 1, log 1024 10
  • Log(million) 20
  • logbX O(logcX) for any bases b and c

9
Common exponents
  • 210 1024 k
  • 28 256
  • 216 64k
  • 220 1 meg
  • 230 1 gig
  • 2n (23)(n/3.0)
  • (23)(n/3)

10
A Quiz
Big-O notation O(n2) O(n2) O(n2) O(n) O(log n)
Number of operations n25n 100nn2 (n7)(n-2) n10
0 number of digits in 2n
11
Big-O Notation
  • The order of an algorithm generally is more
    important than the speed of the processor

12
Canonical complexity e.g.
13
Time Analysis of C Functions
  • Example
  • Printout all item in an integer array of size N
  • Frequent linear pattern
  • A loop that does a fixed amount of operations N
    times requires O(N) time

14
Time Analysis of C Functions
  • Quiz
  • Printout char one by one in a string of length N
  • What is a single operation?
  • If the function calls do complex things, then
    count the operation carried out there
  • Put a function call outside the loop if you can!

for (i0 istri cout
O(N2)!
15
Time Analysis of C Functions
  • Quiz
  • Printout char one by one in a string of length N
  • What is a single operation?
  • If the function calls do complex things, then
    count the operation carried out there
  • Put a function call outside the loop if you can!

N strlen(str) for (i0 istri cout
O(N)!
16
Time Analysis of C Functions
  • Worst case, average case and best case
  • search a number m in an integer array a of size
    N
  • Can you provide an exact number of operations?
  • Best case 121
  • Worst case 13N1
  • Average case 13N/21

for (i0 (iai
17
New topic Complexity Settings
  • Can measure complexity in diff. settings
  • Best, ave, worse
  • Best case sometimes easy to figure
  • but not very interesting
  • easy to modify slow alg above to look for simple
    in-a-row case
  • ? for that, algs very fast
  • Ave case representative
  • the expected time, averaged over all inputs
  • often hard to figure
  • sometimes doesnt make sense eg?
  • Worst-case what we almost always want
  • strong guarantee
  • can promise your boss will never be worse than
    this

18
New topic RAM model
  • The abstract model we find algorithms for
  • not TM!
  • Consider adding two numbers
  • on a TM, would take log time just to write one
    down
  • on a real computer, would happen in essentially
    one instruction
  • very fast
  • multiplying takes a little longer, but still very
    fast
  • 3. on RAM model, each simple operation takes
    one unit of time
  • 4. real life is slightly more complicated
  • mult takes longer but not much!
  • Adding billions takes same time as adding 22, if
    using ints
  • Data types have fixed sizes! 32 bits
  • 5. Same w/ memory access, calling a function,
    etc.
  • 6. But loops do matter
  • For I 1 to 1010 (do something), takes longer
    than just do something
  • Time for loop time for body num. of
    repetitions
  • 7. RAM model is Flat Earth Model

19
Ideas for measuring complexity
  • measure actual RTs
  • Hw eg find, implemented fast sort alg
  • Print total time, hand in number
  • Prob sorting NYC p.b. always slower than sorting
    Jersey City p.b.
  • measure RT ftn of input
  • Might make sense if inputnumber
  • Prob interp sorting files as sorting
    filenames
  • measure RT as ftn of input size
  • Prob some computers faster
  • standardize machine
  • Prob log on late at night
  • measure actual ops in abstract RAM model as ftn
    of input size
  • Prob too messy
  • measure approximate ops in abstract RAM model
    as ftn of input size

20
Testing and Debugging
  • Test run a program and observe its behavior
  • input - expected output?
  • how long ?
  • software engineering issues
  • Choosing Test Data two techniques
  • boundary values
  • fully exercising code (tool profiler)
  • Debugging find the bug after an error is found
  • rule never change if you are not sure whats
    the error
  • tool debugger

21
Summary
  • Often ask yourselves FOUR questions
  • WHAT, WHY, WHERE HOW
  • Topics DSs, C, STL, basic algorithms
  • Data Structure experts
  • Schedule 23 lectures, 7 assignments, 3 exams
  • some credits (10) for attending the class
  • Information website
  • Remember and apply two things (Ch 1)
  • Basic design strategy
  • Pre-conditions and post-conditions
  • Running time analysis
  • Testing and Debugging (reading 1.3)

22
Outline
  • A Review of C Classes (Lecture 2)
  • OOP, ADTs and Classes
  • Class Definition, Implementation and Use
  • Constructors and Value Semantics
  • More on Classes (Lecture 3)
  • Namespace and Documentation
  • Classes and Parameters
  • Operator Overloading

23
Object Oriented Programming
  • Chapter 2 introduces Object Oriented Programming.
  • OOP is a relatively new approach to programming
    which supports the creation of new data types and
    operations to manipulate those types.
  • This lecture gives a review of C Classes and
    introduces ADTs.

24
C Classes and ADTs
  • Class
  • Mechanism to create objects and member functions
  • Support information hiding
  • Abstract Date Types (ADTs)
  • mathematical data type
  • Class as an ADT that programmers can use without
    knowing how the member functions are implemented
    - i.e. with information hiding

25
A point ADT
  • A data type to store and manipulate a single
    point on a plane
  • Manipulations
  • Initialize
  • Retrieval
  • Shift

26
A point ADT
  • A data type to store and manipulate a single
    point on a plane
  • Manipulations
  • Initialize
  • Retrieval coordinates
  • Shift

(-1, 0.8)
27
A point ADT
  • A data type to store and manipulate a single
    point on a plane
  • Manipulations
  • Initialize
  • Retrieval coordinates
  • Shift

p1
x
0.8
-1.0
28
A point ADT
  • A data type to store and manipulate a single
    point on a plane
  • Manipulations
  • Initialize
  • Retrieval coordinates
  • Shift by

p1
x
p2
(1.3, -1.4)
(0.3, -0.6)
29
Outline
  • A Review of C Classes (Lecture 2)
  • OOP, ADTs and Classes
  • Class Definition, Implementation and Use
  • Constructors and Value Semantics
  • More on Classes (Lecture 3)
  • Namespace and Documentation
  • Classes and Parameters
  • Operator Overloading

30
point Definition
  • We can implement the point object using a data
    type called a class.

class point . . .
Dont forget the semicolon at the end
31
point Definition
  • The class will have two components called x and
    y. These components are the x and y coordinates
    of this point.
  • Using a class permits two new features . . .

class point . . . double x
double y
32
point Definition
  • The two components will be private member
    variables. This ensures that nobody can directly
    access this information. The only access is
    through functions that we provide for the class.

class point . . . private double
x double y
33
point Definition
  • In a class, the functions which manipulate the
    class are also listed.

class point public . . . private
double x double y
Prototypes for the point functions go here, after
the word public
34
point Definition
  • In a class, the functions which manipulate the
    class are also listed.

class point public . . . private
double x double y
Prototypes for the point member functions go here
35
point Definition
Our point has at least four member functions
class point public void
initialize(double init_x, double init_y)
void shift(double dx, double dy) double
get_x() const double get_y( )
const private double x double y
Function bodies will be elsewhere.
36
point Definition
The keyword const appears after two prototypes
class point public void
initialize(double init_x, double init_y)
void shift(double dx, double dy) double
get_x( ) const double get_y( )
const private double x double y
This means that these functions will not
change the data stored in a point ADT.
37
Files for the point ADT
  • The point class definition, which we have just
    seen, is placed with documentation in a file
    called point.h, outlined here.
  • The implementations of the
    three member functions will be
    placed in a separate file called point.cxx, which
    we will examine in a few minutes.

Documentation (Preconditions and Postconditions)
  • Class definition
  • point class
  • definition which we
  • have already seen

38
Outline
  • A Review of C Classes (Lecture 2)
  • OOP, ADTs and Classes
  • Class Definition, Implementation and Use
  • Constructors and Value Semantics
  • More on Classes (Lecture 3)
  • Namespace and Documentation
  • Classes and Parameters
  • Operator Overloading

39
Using the point ADT
  • A program that wants to use the point ADT must
    include the point.h header file (along with its
    other header inclusions).
  • File pointmain1.cxx

include include include
point.h" ...
40
Using the point ADT
  • Just for illustration, the example program will
    declare two point variables named p1 and p2.

include include include
point.h" int main( ) point p1 point
p2
41
Using the point ADT
  • Just for illustration, the example program will
    declare two point objects named p1 and p2.

include include include
point.h" int main( ) point p1
point p2
42
Using the point ADT
  • The program starts by calling the
    initialize member function for p1.

include include include
point.h" int main( ) point p1
point p2 p1.initialize(-1.0, 0.8)
43
Using the point ADT
  • The program starts by activating the
    initialize member function for p1.

include include include
point.h" int main( ) point p1
point p2 p1.initialize(-1.0, 0.8)
44
Using the point ADT
  • The member function activation consists of four
    parts, starting with the object name.

int main( ) point p1 point p2
p1.initialize(-1.0, 0.8)
Name of the object
45
Using the point ADT
  • The instance (object) name is followed by a
    period.

int main( ) point p1 point p2
p1.initialize(-1.0, 0.8)
A Period
46
Using the point ADT
  • After the period is the name of the member
    function that you are activating.

int main( ) point p1 point p2
p1.initialize(-1.0, 0.8)
Name of the Function
47
Using the point ADT
  • Finally, the arguments for the member function.
    In this example the first argument (x coordinate)
    is
  • -1.0 and the second argument (y coordinate)
    is 0.8

include "thinker.h" int main( ) point
p1 point p2 p1.initialize(-1.0, 0.8)
Arguments
48
A Quiz
  • How would you activate p1's get_x member
    function ?
  • What would be the output of p1's get_x member
    function at this point in the program ?

int main( ) point p1 point p2
p1.initialize(-1.0, 0.8)
49
A Quiz
  • Notice that the get_x member function has no
    arguments.
  • At this point, activating p1.get_x will return a
    double value
  • -1.0.

int main( ) point p1 point p2
p1.initialize(-1.0, 0.8) cout )
50
A Quiz
  • Trace through this program, and tell me the
    complete output.

int main( ) point p1 point p2
p1.initialize(-1.0, 0.8) cout p1.get_x( ) p2.initialize(p1.get_x(), p1.get_y()) cout
p2.shift(1.3, 1.4) cout p2.get_y()
51
A Quiz
  • -1.0 0.8
  • -1.0 0.8
  • 0.3 2.2

int main( ) point p1 point p2
p1.initialize(-1.0, 0.8) cout p1.get_x( ) p2.initialize(p1.get_x(), p1.get_y()) cout
p2.shift(1.3, 1.4) cout p2.get_y()
52
What you know about Objects
  • Class Data Member Functions.
  • You know how to define a new class type, and
    place the definition in a header file.
  • You know how to use the header file in a program
    which declares instances of the class type.
  • You know how to activate member functions.
  • But you still need to learn how to write the
    bodies of a classs member functions.

53
Outline
  • A Review of C Classes (Lecture 2)
  • OOP, ADTs and Classes
  • Class Definition, Implementation and Use
  • Constructors and Value Semantics
  • More on Classes (Lecture 3)
  • Namespace and Documentation
  • Classes and Parameters
  • Operator Overloading

54
point Implementation
Remember that the member functions bodies
generally appear in a separate point.cxx file.
class point public void
initialize(double init_x, double init_y)
void shift(double dx, double dy) double
get_x( ) const double get_y( )
const private double x double y
Function bodies will be in .cxx file.
55
point Implementation
We will look at the body of intialize, which must
assign its two arguments to the two private
member variables.
class point public void
initialize(double init_x, double init_y)
void shift(double dx, double dy) double
get_x( ) const double get_y( )
const private double x double y
56
point Implementation
For the most part, the functions body is no
different than any other function body.
void pointinitialize(double init_x, double
init_y) x init_x y init_y
  • But there are two special features about a member
    functions body . . .

57
point Implementation
  • In the heading, the function's name is preceded
    by the class name and - otherwise C won't
    realize this is a classs member function.

void pointinitialize(double init_x, double
init_y) x init_x y init_y
58
point Implementation
  • Within the body of the function, the classs
    member variables and other member functions may
    all be accessed.

void pointinitialize(double init_x, double
init_y) x init_x y init_y
59
point Implementation
  • Within the body of the function, the classs
    member variables and other member functions may
    all be accessed.

But, whose member variables are these? Are
they p1.x p1.y p2.x p2.y
void pointinitialize(double init_x, double
init_y) x init_x y init_y
?
60
point Implementation
  • Within the body of the function, the classs
    member variables and other member functions may
    all be accessed.

If we activate p1.initialize p1.x p1.y
61
point Implementation
  • Within the body of the function, the classs
    member variables and other member functions may
    all be accessed.

If we activate p2.initialize p2.x p2.y
62
point Implementation
  • Here is the implementation of the get_x member
    function, which return the x coordinate

double pointget_x() return x
63
point Implementation
  • Here is the implementation of the get_x member
    function, which return the x coordinate

double pointget_x() return x
Notice how this member function implementation
uses the member variable x of the point object.
64
point Implementation
  • Member functions may activate other member
    functions

void pointorigin() x 0.0 y
0.0
Notice this member function implementation still
directly assign the member variables x and y.
65
point Implementation
  • Member functions may activate other member
    functions

void pointorigin() initialize(0.0,
0.0)
Without object name
Notice how this member function implementation
uses the member function initialize.
66
A Common Pattern
  • Often, one or more member functions will place
    data in the member variables...

Initialize shift
get_x get_y
  • ...so that other member functions may use that
    data.

67
Summary of classes
  • Classes have member variables and member
    functions. An object is a variable where the data
    type is a class.
  • You should know how to declare a new class type,
    how to implement its member functions, how to use
    the class type.
  • Frequently, the member functions of an class type
    place information in the member variables, or use
    information that's already in the member
    variables.
  • Next we will see more features of OOP and classes.

68
Outline
  • A Review of C Classes (Lecture 2)
  • OOP, ADTs and Classes
  • Class Definition, Implementation and Use
  • Constructors and Value Semantics
  • More on Classes (Lecture 3)
  • Namespace and Documentation
  • Classes and Parameters
  • Operator Overloading

69
Constructors point Initialization
  • The program starts by activating the
    initialize member function for p1.

include include include
point.h" int main( ) point p1
point p2 p1.initialize(-1.0, 0.8)
First improvement automatic initialization
without activating the initialize function
70
Constructors point Initialization
We can provide a normal member function initialize
class point public void
initialize(double init_x, double init_y)
void shift(double dx, double dy) double
get_x() const double get_y( )
const private double x double y
71
Constructors point Initialization
Or use a constructor that is automatically called
class point public point(double init_x,
double init_y) void shift(double dx, double
dy) double get_x() const double
get_y( ) const private double x
double y
  • function name same as class name
  • no return type, even no void !

72
Constructors Implementation
For the most part, the constructor is no
different than any other member functions.
void pointinitialize(double init_x, double
init_y) x init_x y init_y
  • We only need to replace initialize with point

73
Constructors Implementation
For the most part, the constructor is no
different than any other member functions.
pointpoint(double init_x, double init_y)
x init_x y init_y
  • But there are three special features about
    constructor . . .

74
Constructors
  • Constructor is a member function which
  • automatically called whenever a variable of the
    class is declared
  • the name must be the same as the class name
  • arguments must be given after the variable name
    (when declared)
  • A way to improve the initialize function
  • by providing an initialization function that is
    guaranteed to be called

75
Constructors point Initialization
  • Automatically called when declared.
  • Parameters after the object names

include include include
point.h" int main( ) point p1
point p2 p1.initialize(-1.0, 0.8)
First improvement automatic initialization
without explicitly activating an initialize
function
76
Constructors point Initialization
  • Automatically called when declared.
  • Parameters after the object names

include include include
point.h" int main( ) point p1(-1.0,
0.8) point p2(0.3, 0.6)
First improvement automatic initialization
without explicitly activating an initialize
function
77
Default Constructors
  • Automatically called when declared.
  • Parameters after the object names

include include include
point.h" int main( ) point p1(-1.0,
0.8) point p2(0.3, 0.6)
Sometime we want to define an object with no
parameters
78
Default Constructors
  • Automatically called when declared.
  • NO parameters after the object name p2

include include include
point.h" int main( ) point p1(-1.0,
0.8) point p2
not even a pair of parentheses
79
Default Constructors
We could provide a second constructor with no
parameters
class point public point()
point(double init_x, double init_y)
private double x double y
Implementation pointpoint() x 0.0
y 0.0
80
Constructors Function Overloading
  • You may declare as many constructors as you like
    one for each different way of initializing an
    object
  • Each constructor must have a distinct parameter
    list so that the compiler can tell them part
  • Question How many default constructor is
    allowed?

81
Constructors automatic default constructor
  • What happens if you write a class without any
    constructors?
  • The compiler automatically creates a simple
    default constructor
  • which only calls the default constructors for
    the member variables that are objects of some
    other classes
  • Programming Tip Always provide your own
    constructors, and better with a default
    constructor

82
Value Semantics of a Class
  • Value semantics determines how values are copied
    from one object to another
  • Consists of two operations in C
  • The assignment operator
  • The copy constructor
  • Document the value semantics
  • When you implement an ADT, the document should
    include a comment indicating that the value
    semantics is safe to use.

83
Value Semantics assignment operator
  • Automatic assignment operator
  • For a new class, C normally carries out
    assignment by simply copying each variable from
    the object on the right to that on the left
  • our new class point can use automatic assignment
    operator
  • When automatic assignment fails
  • we will see examples in Lecture 4 (pointers and
    dynamic arrays)

point p1(-1.0, 0.8), p2 p2 p1 cout p2.get_x()
84
Value Semantics copy constructor
  • A copy constructor
  • is a constructor with exactly one parameter whose
    data type is the same as the constructors class
  • is to initialize a new object as an exact copy
    of an existing object
  • An example

point p1(-1.0, 0.8) point p2 (p1) cout p2.get_x()
85
Value Semantics copy constructor
  • A copy constructor
  • is a constructor with exactly one parameter whose
    data type is the same as the constructors class
  • is to initialize a new object as an exact copy
    of an existing object
  • An alternative syntax

point p1(-1.0, 0.8) point p2 p1 cout p2.get_x()
86
Value Semantics discussion
  • point p2 p1 versus p2 p1
  • The assignment p2 p1 merely copies p1 to the
    already existing object p2 using the assignment
    operator.
  • The syntax point p2 p1 looks like an
    assignment statement, but actually a declaration
    that both declare a new object, and calls the
    copy constructor to initialize p2 as a copy of
    p1.
  • p2 will be the same iff the assignment operator
    and the copy constructor do the same things

87
Copy Constructor Implementation
  • You may write a copy constructor much like any
    other constructor
  • Lecture 4 and later
  • Take advantage of a C feature
  • automatic copy constructor
  • similar to assignment, the automatic copy
    constructor initializes a new object by merely
    copy all the member variables from the existing
    object.
  • Automatic versions may fail!

A Demo
88
Constructors, etc. a summary
  • Constructor is a member function
  • define your own constructors (including a
    default)
  • automatic default constructor
  • inline member functions ( Ch 2.2)
  • Place a function definition inside the class
    definition
  • for time efficiency
  • value semantics of a class
  • assignment operators and copy constructor
  • automatic assignment op and copy constructor

89
Outline
  • A Review of C Classes (Lecture 2)
  • OOP, ADTs and Classes
  • Class Definition, Implementation and Use
  • Constructors and Value Semantics
  • More on Classes (Lecture 3)
  • Namespace and Documentation
  • Classes and Parameters
  • Operator Overloading

90
Assignments - draft
  • Reading
  • Chapter 2.3-2.5
  • Programming assignment 1 - Due Sept. 14
  • Projects 2 3 in Chapter 2 (pp 86)
  • Need all of chapter 2 to finish, but you can
    start doing it
  • Requirements and guidelines have been posted on
    the course web site
Write a Comment
User Comments (0)
About PowerShow.com