CS 100 M Lecture 15 - 15. Structures Simple Structures...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
15. Structures Simple Structures Structure Arrays Structures with Array Fields Other Possibilities
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Data is Often Related A point in the plane has an x coordinate and y coordinate. If a program manipulates lots of points, there will be lots of x’s and y’s. Anticipate clutter. Is there a way to “package” the two coordinate values?
Background image of page 2
Packaging Affects Thinking Our Reasoning Level: P and Q are points. Compute the midpoint M of the connecting line segment. Behind the scenes we do this: M x = (P x + Q x )/2 M y = (P y + Q y )/2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Seen This Before Functions are used to ``package’’ calculations. Elevates the level of our reasoning. Critical for problem solving.
Background image of page 4
Simple Example P1 = struct(‘x’,3,’y’,4); P2 = struct(‘x’,-1,’y’,7); D = sqrt((p1.x-p2.x)^2 + (p1.y-p2.y)^2); Distance between two points. p1.x, p1.y, p2.x,p2.y participating as variables—because they are .
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Initialization P1 = struct(‘x’,3,’y’,4); p1 is a structure. The structure has two fields. Their names are x and y. They are assigned the values 3 and 4.
Background image of page 6
How to Visualize p1 p1 x y 3 4 P1 = struct(‘x’,3,’y’,4);
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Accessing a Field p1 x y 3 4 A = p1.x + p1.y Assigns the value 7 to A.
Background image of page 8
Assigning to a Field p1 x y 3 4 p1.x = p.y^2 Will assign the value 16 to p1.x
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Another Example
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/23/2008 for the course CS 100 taught by Professor Fan/vanloan during the Spring '07 term at Cornell University (Engineering School).

Page1 / 43

CS 100 M Lecture 15 - 15. Structures Simple Structures...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online