DOC PREVIEW
UW CSE 142 - Data Structures

This preview shows page 1-2-14-15-29-30 out of 30 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

University of Washington Computer Programming IPowerPoint PresentationSlide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32S-1University of WashingtonComputer Programming ILecture 18: Structures© 2000 UW CSES-2Concepts this lectureReview: Data structuresHeterogenous structures (structs, records)struct type definitions (typedef)Field selection (. operator)Structs as parametersCall by valuePointer parameters and -> operatorS-4Review: Data StructuresFunctions give us a way to organize programs.Data structures are needed to organize data, especially:1. large amounts of data2. variable amounts of data3. sets of data where the individual pieces are related to one anotherArrays helped with points 1 and 2, but not with point 3Example: the data describing one house in a neighborhood: x, y, color, # windows, etc.Example: information about one student: name, ID, GPA, etc. etc.S-5Problem: Account RecordsThe Engulf & Devour Credit Co. Inc., Ltd. needs to keep track of insurance policies it has issued.Information recorded for each policyAccount number (integer)Policy holder’s age (integer) and sex (‘m’ or ‘f’)Monthly premium (double)At E&G, customers are only known by their account #, so there is no need to store their names.S-6Structs: Heterogeneous StructuresC expressions:alice.age is 23alice.sex is ‘f’2*alice.premium is 84.34“alice”account 9501234age 23sex ‘f’premium 42.17 Collection of values of possibly differing types. Name the collection; name the components (fields). Example: Insurance policy information for Alice (informally)S-7Defining structsThere are several ways to define a struct in a C program. For this course:Define a new type specifying the fields in the structDeclare variables as needed using that new typeThe type is defined only once at the beginning of the programVariables with this new type can be declared as needed.S-8Defining struct typestypedef struct { /* record for one policy:*/int account ; /* account number */int age ; /* policy holder’s age */char sex; /* policy holder’s sex */double premium; /* monthly premium */} account_record ;Defines a new data type called account_record. Does not declare (create) any variables. No storage is allocated.S-9Style Points in struct typesIn a type definition, use comments to describe the fields, not the contents of the fields for any particular variableI.e.. describe the layout of an account_record, not information about Alice’s account.typedefs normally are placed at the top of the program fileS-11/*typedef students_record goes at top of program */...account_record alice ;account_record bob;account_record is a type; alice and bob are variables.Both variables have the same internal layoutDeclaring struct VariablesaccountagesexpremiumaliceaccountagesexpremiumbobS-12A fundamental operation on struct variables is field access: struct_name.field_nameselects the given field (variable) from the struct alice.age = 23;alice.premium = 12.20;alice.premium = 2 *alice.premium;Field accessaccountagesexpremiumalice2312.20-------- 24.40S-13A selected field is an ordinary variable - it canbe used in all the usualways alice.age++;printf(“Alice is %d years old\n”,alice.age);scanf(“%lf”, &alice.premium);Field accessaccountagesexpremiumaliceS-14TerminologyThe terms “struct”, “record” and “structure” mean the same thing “fields” are often called “components” or "members".S-15Why use structs?Collect together values that are treated as a unit (for compactness, readability, maintainability).typedef struct { int hours, minutes ; double seconds ; } time ;typedef struct { int dollars, cents ; } money ;This is an example of “abstraction”S-16Structs as User-Defined TypesC provides a limited set of built-in types: int, char, double (and variants of these not discussed in these lectures)Pointers introduced some new types Arrays further enrich the possible types availableBut... the objects in the real world and in computer applications are often more complex than these types allowWith structs, we’re moving toward a way for programmers to define their own types.S-17Some LimitationsLike arrays, there are some restrictions on how a struct can be used compared to a simple variable (int, double, etc.)Can’t compare (==, !=) two structs directlyCan’t read or write an entire struct with scanf/printfBut you can do these things on individual fieldsS-18struct AssignmentUnlike arrays, entire structs can be copied in a single operation. Don’t need to copy field-by-field.Can assign struct values with =Can have functions with struct result types, and can use struct values in a return statementS-19A struct assignment copies all of the fields. If dilbert is another account_record, then dilbert = bob;is equivalent to dilbert.account = bob.account; dilbert.age = bob.age; dilbert.sex = bob.sex; dilbert.premium =bob.premium;struct Assignmentaccountagesexpremiumdilbertbobaccountagesexpremium1246532m12.951246532m12.95S-20structs as Parametersstructs behave like all other non-array values when used as function parametersCan be call-by-value (copied)Can use as pointer parametersS-21struct initializersA struct can be given an initial value when it is declared. List initial values for the fields in the same order they appear in the struct typedef.account_recordratbert = { 970142, 6, ‘?’, 99.95 } ;S-22/* Given 2 endpoints of a line, “return” coordinates of midpoint */void midpoint( double x1, double y1, double x2, double y2, double *midxp, double *midyp ) { *midxp = (x1 + x2) / 2.0; *midyp = (y1 + y2) / 2.0;}double ax, ay, bx, by, mx, my;midpoint(ax, ay, bx, by, &mx, &my);Midpoint Example Revisited(x1, y1)(x2, y2)(x1+x2) (y1+y2)2 2,( )S-23Better: use a struct to make the concept of a “point” explicit in the code typedef struct { /* representation of a point */ double x, y ; /* x and y coordinates */} point ;...point a = {0.0, 0.0}, b = {5.0, 10.0} ;point m ;m.x = (a.x + b.x) / 2.0 ;m.y = (a.y + b.y) / 2.0 ;Points as structsS-24Midpoint with points/* return point whose coordinates are the center of the line segment with endpoints pt1 and pt2. */point midpoint (point pt1, point pt2) {point mid;mid.x = ( pt1.x +


View Full Document

UW CSE 142 - Data Structures

Download Data Structures
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Data Structures and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Data Structures 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?