1 2. intro. to data structures & adts – c-style types goal: to organize data criteria: to...

60
1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient storage of data retrieval of data manipulation of data Design Issue: select and design appropriate data types. (This is the real essence of OOP.)

Upload: kory-evans

Post on 13-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

1

2. Intro. To Data Structures &ADTs – C-Style Types

• Goal: to organize data• Criteria: to facilitate efficient

– storage of data– retrieval of data– manipulation of data

• Design Issue: – select and design appropriate data types.

(This is the real essence of OOP.)

Page 2: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

2

Examples

Airline Reservations Trans-Fryslan Airlines (pp. 30-31)Attempt 1:enum SeatStatus {OCCUPIED, UNOCCUPIED};SeatStatus seat1, seat2, . . . , seat10;

Horrible algorithms for the basic operations!

Attempt 2:const int MAX_SEATS = 10; // upper limit on number of seatsenum SeatStatus {OCCUPIED, UNOCCUPIED};typedef SeatStatus SeatList[MAX_SEATS];

SeatList seat;

Nice algorithms for the basic operations!

Tradeoff::simplicity of data organization simplicity/elegance of algorithms

Simple (unsophisticated data structure)may require much work for processing data.

More complex data organizationmay yield nicer algorithms for the basic operations.

Page 3: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

3

Examples - cont.

_______________________________________________is an important factor.

Us hash tables

_______________________________________________is an important factor.

Searching an online phone directory: Linear search?OK for Calvin College

too slow for Grand Rapids or New York

Amount of data is an important factor.Restructure (order) the data set for efficient processing

use binary search or an indexed sequential search

Compiler lookup of an identifier's type, etc. in a symbol table:Linear search? No, too slowBinary search? No, too much work to keep sorted

Text processing: Store in an array / vector?OK for text analysis — word counts, average word length, etc.Not for word-processing — Too inefficient if many insertions

& deletions

Page 4: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

4

Abstract Data Type (ADT)

Def.

e.g. whole numbers (integers) and arithmetic operators for addition, subtraction, multiplication and division.

e.g. Seats for TFA Basic operations: find empty seat, reserve a seat,

cancel a seat assignmentWhy "abstract?" Data, operations, and relations are studied

_____________________________________

_________ not _______ is the focus.

Page 5: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

5

Implementation of an ADT

Def.Consists of

The storage structures/data structures used in implementations are provided in a language (primitive or built-in) or are built from the language constructs (user-defined).

In either case, successful software design uses data abstraction:

Page 6: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

6

C++ Types

Characters

Integral Floating point (reals)

charshort int

int

unsigned short unsigned

floatdouble

long double

IntegersEnumerations

Arithmetic void

class

pointers structunion

valarray

vectordequelist

set

map

multiset

multimap

stackqueue

priority_queuelong int

unsigned long

unsigned char signed char

array

string

bitset

bool complex

Fundamental Types Structured Types

istreamostreamiostreamifstreamofstreamfstreamistringstream

ostringstreamstringstream

Page 7: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

7

Simple Data Types (§2.2)

Memory: 2-state devices « ___________________

Organized into _____________________ and ______________(machine dependent — e.g.,

4 bytes).

Each byte (or word) has an _____________ making it possible to store and retrieve contents of any given memory location.

Therefore: the most basic form of data: __________________________ simple data types (values are atomic — can't be subdivided) are ADTs. Implementations have: Storage structures: memory locations Algorithms: system hardware/software to do basic operations.

Page 8: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

8

Boolean data

Data values: {false, true}

In C/C++: false = 0, true = 1 (or nonzero)

Could store 1 value per bit, but usually use a byte (or word)

Operations: and: && (See bit tables on p. 34)or: ||not: !

x !x0 11 0

&&

||

Page 9: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

9

Character Data

Store numeric codes (ASCII, EBCDIC, Unicode)1 byte for ASCII and EBCDIC,2 bytes for Unicode (see examples on p. 35).

Basic operation: comparison to determine if ==, <, >, etc.use their numeric codes (i.e. use ordinal value)

Page 10: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

10

Integer Data

Nonegative (unsigned) integer:

type unsigned (and variations) in C++Store its base-two representation in a fixed number w of bits

(e.g., w = 16 or w = 32)

88 =

Signed integer: type int (and variations) in C++

Store in a fixed number w of bits using one of the following representations:

Page 11: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

11

Sign-magnitude representation

Save one bit (usually most significant) for sign

(0 = +, 1 = – )

Use base-two representation in the other bits.

88 _000000001011000

Cumbersome for arithmetic computations

–88 _000000001011000

Page 12: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

12

Two's complement representation

For nonnegative n: Use ordinary base-two representation with leading (sign) bit 0

For negative n (–n):(1) Find w-bit base-2 representation of n (2) Complement each bit.(3) Add 1 (Flip all bits from rightmost 0 to the end)

Example: –881. 88 as a 16-bit base-two number2. Complement this bit string3. Add 1

Page 13: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

13

Good for arithmetic computations (see p. 38)

5 + 7:

0000000000000101+0000000000000111

5 + –6: 0000000000000101+1111111111111010

These work for both + and – integers

Page 14: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

14

Add a constant bias to the number (typically, 2w – 1) ;then find its base-two representation.

Examples: 88 using w = 16 bits and bias of 215 = 32768 1. Add the bias to 88, giving 328562. Represent the result in base-two notation:

Note: For n > 0, just change leftmost bit of binary representation of n to 1

–88:

1. Add the bias to -88, giving 326802. Represent the result in base-two notation:

Good for comparisons; so, it is commonly used for exponents in floating-point representation of reals.

Biased representation

Page 15: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

15

Problems with Integer Representation

Limited Capacity — a finite number of bits

An operation can produce a value that requires more bitsthan maximum number allowed.

This is called __________________.

So none of these is a perfect representation of (mathematical) integers — can only store a finite (sub)range of them.

Page 16: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

16

Real Data

T ypes f l o a t and d o u b l e (and variations) in C ++Single precision (I E E E F loating-P oint F ormat )

1. W rite binary representation in floating-point form:b1.b2b3 . . . 2k with each bi a bit and b1 = 1 (unless number is 0)

mantissa exponent or fractional part

2. S tore:— sign of mantissa in leftmost bit (0 = +, 1 = – )— biased binary rep. of exponent in next 8 bits (bias = 127)— bits b2b3 . . . in rightmost 23 bits. (N eed not store b1 — know it's 1)

Example: 22.625 = _______________________ (see p.41)

Floating point form: _______________________

Page 17: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

17

Problems with Real Representation

Exponent overflow/underflow (p. 41)Only a finite range of reals can be stored exactly.

Roundoff error (pp. 41-42)

Most reals do not have terminating binary representations.

Example:0.7 = (0.101100110011001100110011001100110 . . .)2

Roundoff error may be compounded in a sequence of operations.

Be careful in comparing reals with == and !=.

Page 18: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

18

Assignment #1

Be able to answer the questions in Quick Quiz 2.2.Write out the following to hand in.

Due Date:

Exercises 2.2110, 12 (Exers 2, 4 in sign-magnitude)16, 18 (Exers 2, 4 in two's complement)22, 24 (Exers 2, 4 in biased notation)27, 32, 37, 38, 40, 43

Page 19: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

19

C-Style Data Structures: Arrays (§2.3)

Defn of an array as an ADT:

where the basic operation is

Properties:• Fixed number of elements• Ordered so there is a first element, a second one, etc.• Elements must be the same type (and size);

use arrays only for homogeneous data sets.• Direct access: Access an element by giving its location

— the time to access each element is the same for all elements, regardless of position.— in contrast to sequential access (where to access an element, one must first access all those that precede it.)

Page 20: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

20

Declaring arrays in C++

element_type array_nameelement_type array_name[[CAPACITYCAPACITY];];where

element_type is any typearray_name is the name of the array — any valid identifierCAPACITY (a positive integer constant) is the number of

elements in the array

The compiler reserves a block of consecutive memory locations, enough to hold CAPACITY values of type element_type.

The elements (or positions) of the array are indexed 0, 1, 2, . . ., CAPACITY - 1.

e.g.

Better to use a named constant to specify the array capacity:

Can use typedef with array declarations; e.g., const int CAPACITY = 100;

.

.

.

Page 21: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

21

indices numbered 0, 1, 2, . . ., CAPACITY - 1

How well does C/C++ implement an array ADT?

As an ADT In C++

ordered

fixed size

same type elements

direct access

element_type is the type of elements

CAPACITY specifies the capacity of the array

subscript operator []

Page 22: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

22

Subscript operator

// Zero out all the elements of score

// Read values into the first numScores elements of score

// Display values stored in the first numScores elements

[] is an actual operator and not simply a notation/punctuation as in some other languages. Its two operands are an ___________________and an _________________(or subscript) and is written

array_name[i]

Here i is an integer expression with 0 < i <= CAPACITY – 1.

[] returns the ________________ of the element in location i in array_name; so array_name[i]is a ___________, called an _______________(or _________________) ______________, whose type is the specified element_type of the array.

This means that it can be used on the left side of an assignment, in input statements, etc. to store a value in a specified location in the array. For example:

Page 23: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

23

Array Initialization

In C++, arrays can be initialized when they are declared.Numeric arrays:element_type num_array[CAPACITY]={list_of_initial_values};

Note 1: If fewer values supplied than array's capacity, remaining elements assigned 0.

double rate[5] = {0.11, 0.13, 0.16};

Note 2: It is an error if more values are supplied than the declared size of the array. How this error is handled, however, will vary from one compiler to another.

Note 3: If no values supplied, array elements are undefined (i.e., garbage values).

rate

0 1 2 3 4

rate

0 1 2 3 4

Page 24: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

24

Note 1: If fewer values are supplied than the declared size of the array,

const int NAME_LENGTH = 10;char collegeName[NAME_LENGTH]={'C', 'a', 'l', 'v', 'i', 'n'};

Character arrays:

Character arrays may be initialized in the same manner as numeric arrays.

declares vowel to be an array of 5 characters and initializes it as

follows: vowel

0 1 2 3 4

Page 25: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

25

Note 2: Character arrays may be initialized using string constants. For example, the following declaration is equivalent to the preceding:

Note 3: The null character '\0' (ASCII code is 0) is used ________________________________________

Thus, character arrays used to store strings should be declared large enough to _____________________________. If it is not, one cannot expect some of the string functions and operations to work correctly.

If a character array is initialized with a string constant, the

Page 26: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

26

Initializations with no array size specified

The array capacity may be omitted in an array declaration with an initializer list.

In this case, the number of elements in the array will be ________________

____________________________________________

Example:

Note: This explains the brackets in constant declarations such as

const char INFILE[] = "employee.dat";

Page 27: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

27

AddressesWhen an array is declared, the address of the first byte (or word) in the block of memory associated with the array is called the ________________ of the array.

Each array reference must be translated into an ___________ from this base address.

For example, if each element of array score will be stored in 8 bytes and the base address of score is 0x1396. A statement such as

cout << score[3] << endl;requires that array reference score[3] be translated into a memory address:

The contents of the memory word with this address 0x13ae can then be retrieved and displayed.

An _______________________________ like this is carried out each time an array element is accessed.

score[3]

[0]

[1]

[2]

[3]

[99]

.

.

....

score 0x1396

Page 28: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

28

The value of array_name is actually the ___________________________________

________________ ___________is the address of array_name[index].

An array reference array_name[index]

is equivalent to

For example, the following statements are equivalent:

cout << ________________ << endl;

cout << ________________ << endl;

Note: No bounds checking of indices is done! (See pp. 50-51)

* is the ____________________ operator*ref returns the ______________________________________________

Page 29: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

29

C-Style Multidimensional Arrays

Example: A table of test scores for several different students on

several different tests.

Test 1 Test 2 Test 3 Test 4Student 1 99.0 93.5 89.0 91.0Student 2 66.0 68.0 84.5 82.0Student 3 88.5 78.5 70.0 65.0

: : : : :: : : : :

Student-n 100.0 99.5 100.0 99.0

For storage and processing, use a _______________________________.

Page 30: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

30

Declaring Two-Dimensional Arrays

Standard form of declaration:element_type array_name[NUM_ROWS][NUM_COLUMNS];

Example:const int NUM_ROWS = 30, NUM_COLUMNS = 5;

or

Initialization List the initial values in braces, row by row; May use internal braces for each row to improve readability.

Example: double rates[2][3] =

Page 31: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

31

Processing Two-Dimensional Arrays Remember: Rows (and) columns are numbered from zero!!

Use doubly-indexed variables:scoresTable[2][3] is the entry in row 2 and column

3

row index column index Use nested loops to vary the two indices, most often in a _________________ manner.

int numStudents, numTests, i, j;

cout >> "# students and # of tests? ";cin >> numStudents >> numTests;cout << "Enter test scores for students\n";

Page 32: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

32

Higher-Dimensional Arrays

The methods for two-dimensional arrays extend in the obvious way.

Example: To store and process a table of test scores for several different students on several different tests for several different semesters

const int RANKS = 10, ROWS = 30, COLUMNS = 5;typedef double ThreeDimArray[RANKS}[ROWS][COLUMNS;

ThreeDimArray gradeBook;

is the score on page 4 for student 2 on test 3

// number of pages, students and tests all counted from zero!!

Page 33: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

33

b. Still higher dimensionsExample like the automobile-inventory example on pp. 54-5

enum BrandType {Levi, Wrangler, CalvinKlein, Lee, BigYank, NUM_BRANDS};enum StyleType {baggy, tapered, straightleg, designer, NUM_STYLES};enum WaistType {w28, w29, w30, w31, w32, w33, w34, w35, w36, w37, w38, w39, w40, w41, w42, w43, w44, w45, w46, w47, w48, NUM_WAIST_SIZES};enum InseamType {i26, i27, i28, i29, i30, i31, i32, i33, i34, i34, i36, NUM_INSEAM_SIZES};

typdef intJeansArray[NUM_BRANDS][NUM_STYLES]

[NUM_WAIST_SIZES][NUM_INSEAM_SIZES];

JeansArray jeansInStock;

jeansInStock[b][s][w][I]++; // sale of 1 brand b, style s, waist w, inseam I jeans

Page 34: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

34

Arrays of Arraysdouble scoresTable[30][4];

Declares scoresTable a declaration of a one-dimensional array containing 30 elements, each of which is a one-dimensional array of 4 real numbers; that is, scoresTable is a one-dimensional array of rows , each of which has 4 real values. We could declare it as

or, since typedef is used once, why not use it twice:

Page 35: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

35

scoresTable[i] is __________________________________________

Address TranslationAddress Translation::

The array-of-arrays structure of multidimensional arrays explains address translation. Suppose the base address of scoresTable is 0x12348:

scoresTable[10][3]

In general, n n-dimensional array can be viewed (recursively) as a one-dimensional array whoseelements are (n - 1)-dimensional arrays.

In any case:

scoresTable[i][j] should be thought of as (scoresTable[i])[j] that is, as finding the j-th element of scoresTable[i].

Page 36: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

36

Passing an array to a function actually passes the base address of the array. This means:

1. The formal parameter has _______________________ as the actual argument. So ________________________________________________________________________________

2. Array capacity is not available to a function unless passed as a separate parameter.

The following function prototypes are all equivalent.

Arrays as Parameters

void Print(int A[100], int theSize);

void Print(_____________, int theSize);

void Print(_____________, int theSize);

f(array); void f(ArrayType param){ ... }

Page 37: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

37

Arrays as Parameters …Continued

Now, what about multidimensional arrays?

void Print(double table[][], int rows, int cols)

doesn't work

Use a typedef to declare a global type identifier and use it to declarethe types of the parameters.

For example:

______________________________________________________________

. . .______________________________________________________________

. . .______________________________________________________________

. . .

Page 38: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

38

Problems with C-Style Arrays

•Capacity cannot change.

Solution 1 (non-OOP) Use a run-time array— Construct B to have required capacity— Copy elements of A into B — Deallocate A

Solution 2 (OOP) Use vector

Page 39: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

39

Basic principle of OOP:

An object should be autonomousautonomous (self-contained); it should carry within itself all of the information needed to describe and operate upon itself.

Solution (OOP): Encapsulate array, capacity, size, and operations

in a _____________.

__________________.

The Deeper Problem:

•Virtually no predefined operations for non-char arrays. Basic reason: No character

to mark the end of a numeric sequence

— no numeric equivalent of the NUL character.

Solution 1(non-OOP): In addition to the array, pass its size and perhaps its capacity) to

functions.

J o h n D o e \0 \0

Start processing

here

Stop processing

here

6 2 0 1 5 0 2 0 0 0

Start processing

here

Stop processing where???

Page 40: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

40

Assignment #2

Be able to answer the questions in Quick Quiz 2.3.Write out the following to hand in.

Due Date:

Exercises 2.3 (p. 61)

1, 3, 5, 6, 8, 10, 11, 13, 15, 17, 19

Page 41: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

41

Why Needed?

Current OCD:

Aggregate Data Types

1. Identify the objects in the problem.1a. . . .

2. Identify the operations in the problem.2a. If the operation is not predefined, write a function to

perform it.2b. If the function is useful for other problems, store it in a

library.3. Organize the objects and operations into an algorithm.4. Code the algorithm as a program.5. Test, execute, and debug the program.6. Maintain the programBut, predefined types may not be adequate; so we add:

1a. If necessary, create a new data type to model it.

Page 42: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

42

Especially true if object being modeled has __________________________.Examples:A temperature has:

a degrees attribute a scale attribute (Fahrenheit, Celsius, Kelvin)

32 Fdegrees scale

A date has: a month attribute a day attribute a year attribute

September 29 1999 month day year

Page 43: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

43

C++ provides __________and __________ to create new types with multiple attributes.

So we add to our OCD methodology:

1a. If necessary, create a new data type to model it.

1b. If the object has multiple attributes, create a struct or class to represent objects of that type.

Page 44: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

44

struct TypeName{

TypeA data1;TypeB data2;… //member data of any type

};

Declaration (C-Style)

A structure (usually abbreviated to struct and sometimes called a record)

has a fixed size

is ordered

elements may be of ________________The basic operation is direct access to each element so that values can be stored in / retrieved from that element.

Only difference from an array

As an ADT:

Page 45: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

45

Examples:

32 Fdegrees scale

September 29 1999 month day year

Page 46: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

46

John Q. Doe 12345 Calvin Rd. Grand Rapids, MI 9571234 name street city & statephone #

Phone Listing:

struct DirectoryListing{ string name, // name of person street, // street address cityAndState; // city, state (no zip) unsigned phoneNumber; // 7-digit phone number};

DirectoryListing entry, // entry in phone book group[20]; // array of directory listings

Page 47: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

47

3.73 –2.51 x coord. y coord.

struct Point{ double xCoord, yCoord;};

Point p, q;

Coordinates of a point:

(Members need not have different types.)

Test scores:(Members may be structured types — e.g., arrays.)

012345 83 79 92 85id-number list of scoresstruct TestRecord{ unsigned idNumber, score[4];};

TestRecord studentRecord, gradeBook[30];

Page 48: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

48

struct PersonalInfo{ DirectoryListing ident; Date birth; double cumGPA, credits;};

PersonalInfo student;

John Q. Doe 123 Calvin Rd. Detroit, MI 95714 May 17 1975 3.95 92.5

name street city & state zip month day year gpa credits

Hierarchical (or nested) structs

Since the type of a member may be any type, it may be another struct.

DirectoryListing Date real real

Page 49: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

49

The scope of a member identifier is the struct in which it is defined.

Consequences:— A member identifier may be used outside the struct for some other purpose.e.g. int month; // legal declaration alongside Date

— A member cannot be accessed outside the struct just by giving its name.e.g. year will not yield anything unless there is a year declared outside the Date struct.

Some Properties:

Direct access to members of a struct (or class) is implemented using _______________________: one of these is the _______________

struct_var.member_name

Page 50: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

50

Examples:Input a value into the month member of birthday

Calculate y coordinate of a point on y = 1/xif (p.xCoord != 0) p.yCoord = 1.0 / p.xCoord;

Sum the scores in studentRecorddouble sum = 0;for (int i = 0; i < 4; i++)

Output the name stored in student

Page 51: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

51

A Quick Look at Unions (p. 68)

Declaration: Like a struct, but replace "struct" with "union":

union TypeName TypeName is optional{

declarations of members //of any types};

A union differs from a struct in that the members ____________

_____________. Memory is (typically) allocated for the largest member, and all the other members share this memory

Page 52: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

52

Unions can be used to define structs that have some common members — a fixed part — and a variant part that makes it possible for the fields of a struct to differ from one data value to the next. For example to process a file of information about various categories of people:John Doe 40 M <——— name, age, marital status (married)January 30 1980 <——— wedding dateMary Smith Doe 8 <——— spouse, # dependentsFred Jones 17 S <——— name, age, marital status (single)T <——— availableJane VanderVan 24 D <——— name, age, marital status (divorced)February 21 1998 N<——— divorce date, remarried (No)]Peter VanderVan 25 W <——— name, age, marital status (widower)February 22 1998 Y <——— date became a widower, remarried (Yes)

::

Page 53: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

53

struct Date { string month; short day, year;};struct MarriedInfo{ Date wedding; string spouse short dependents;};struct SingleInfo{ bool available;};

struct WasMarriedInfo{ Date divorceOrDeath; char remarried;};

struct PersonalInfo { string name; short age; char marStatus; // Tag: S = single, M = married, // W = was married union { MarriedInfo married; SingleInfo single; WasMarriedInfo wasMarried; };};

PersonalInfo person;

Page 54: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

54

n a m ea g em a r S t a t u s

w e d d i n gs p o u s ed e p e n d e n t s

a v a i l a b l e

P e r s o n a l I n f o

base class

M a r r i e d P e r s o n

derived classes

S i n g l e P e r s o nW a s M a r r i e d P e r s o n

n a m ea g em a r S t a t u s

d i v o r c e O r D e a t hr e m a r r i e d

n a m ea g em a r S t a t u s

n a m ea g em a r S t a t u s

Structs with variant parts aren't used much anymore. (p. 69)

Instead, in OOP languages:

Encapsulate the common information in a __________

Use _____________ to build ___________________ for the variants (Derived classes inherit all of the non-private members of the base class.)

Page 55: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

55

Address translation for structs and unions is similar to that for arrays, except that different field types require using a summation to calculate the offsets.

(p. 70)

If a struct s has fields f1, ..., fn, requiring w1, ..., wn cells of storage, respectively:

Address of s.fk = base address of s + offset

= base address of s +

1k

1iiw

For structs that contain unions: Allocate space for the largest variant, and then overlay variants in this space.

Page 56: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

56

A commercial for OOP: Two programming paradigms

•_______________ : ( C, FORTRAN, and Pascal )

– Action-oriented — concentrates on the verbs of a problem's specification

– Programmers:• Identify basic tasks to be performed to

solve problem • Implement the actions required to do

these tasks as sub-programs (procedures/functions/subroutines)

• Group these subprograms into programs/modules/libraries, which together make up a complete system for solving the problem

•_________________ : ( C++, Java, and Smalltalk)

– Focuses on the nouns of a problem's specification

– Programmers:• Determine what objects are needed for a

problem and how they should work together to solve the problem.

• Create types called classes made up ofdata members and function members to operate on the data. Instances of a type (class) are called objects.

Page 57: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

57

Creating a Data Type in a procedural (C-type) language(pp. 74-78)

Problem: Create a type Time for processing times in standard hh:mm

AM/PM form and in military-time form.

Data Members:Hours (1, ..., 12)Minutes (0, 1, 2, ..., 59)AM or PM indicator ('A' or 'P')MilTime (military time equivalent)

Some Operations :1. Set the time2. Display the time3. Advance the time4. Determine if one time is less than another time.

Implementation:1. Need __________ for the data members — use a

__________ 2. Need ____________ for the operations.3. "Package" declarations of these together in a

_____________.

Page 58: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

58

/** Time.h ---------------------------------------------------------- This header file defines the data type Time for processing time. Basic operations are: Set: To set the time Display: To display the time Advance: To advance the time by a certain amount LessThan: To determine if one time is less than another--------------------------------------------------------------------*/

#include <iostream>using namespace std;

struct Time{ unsigned hour, minute; char AMorPM; // 'A' or 'P' unsigned milTime; // military time equivalent};

Notice the documentation

!

Page 59: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

59

/* Set sets the time to a specified values. * * Receive: Time object t * hours, the number of hours in standard time * minutes, the number of minutes in standard time * AMPM ('A' if AM, 'P' if PM * Pass back: The modified Time t with data members set to * the specified values ******************************************************************/void Set(Time & t, unsigned hours, unsigned minutes, char AMPM);

/* Display displays time t in standard and military format using * output stream out.

* Receive: Time t and ostream out * Output: The time T to out * Pass back: The modified ostream out ******************************************************************/void Display(const Time & t, ostream & out);

/* Advance increments a time by a specified value. * * Receive: Time object t * hours, the number of hours to add * minutes, the number of minutes to add * Pass back: The modified Time t with data members incremented * by the specified values ******************************************************************/void Advance(Time & t, unsigned hours, unsigned minutes);

Notice the docu-mentatio

n!

Page 60: 1 2. Intro. To Data Structures & ADTs – C-Style Types Goal: to organize data Criteria: to facilitate efficient –storage of data –retrieval of data –manipulation

60

/* Determine if one time is less than another time. * * Receive: Times t1 and t2 * Return: True if t1 < t2, false otherwise. ******************************************************************/bool LessThan(const Time & t1, const Time & t2);

//========= Time.cpp -- implements the functions in Time.h =========

#include "Time.h"

/*** Utility functions -- might be added as basic operations later ***/

int ToMilitary(unsigned hours, unsigned minutes, char AMPM);

void ToStandard(unsigned military, unsigned & hours, unsigned & minutes, char& AMPM);

//... Definitions of Set, Display, Advance, LessThan, ToMilitary,// and ToStandard go here --- see the text.