L 17 Structures Simple Structures Structure Arrays Structures

  • Slides: 38
Download presentation
L 17. Structures Simple Structures Structure Arrays Structures with Array Fields Other Possibilities

L 17. Structures Simple Structures Structure Arrays Structures with Array Fields Other Possibilities

Data is Often Related A point in the plane has an x coordinate and

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?

Packaging Affects Thinking Our Reasoning Level: P and Q are points. Compute the midpoint

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: Mx = (Px + Qx)/2 My = (Py + Qy)/2

Seen This Before Functions are used to ``package’’ calculations. Elevates the level of our

Seen This Before Functions are used to ``package’’ calculations. Elevates the level of our reasoning. Critical for problem solving.

Simple Example P 1 = struct(‘x’, 3, ’y’, 4); P 2 = struct(‘x’, -1,

Simple Example P 1 = struct(‘x’, 3, ’y’, 4); P 2 = struct(‘x’, -1, ’y’, 7); D = sqrt((p 1. x-p 2. x)^2 + (p 1. y-p 2. y)^2); Distance between two points. p 1. x, p 1. y, p 2. x, p 2. y participating as variables—because they are.

Initialization P 1 = struct(‘x’, 3, ’y’, 4); p 1 is a structure. The

Initialization P 1 = struct(‘x’, 3, ’y’, 4); p 1 is a structure. The structure has two fields. Their names are x and y. They are assigned the values 3 and 4.

How to Visualize p 1 3 4 x y p 1 P 1 =

How to Visualize p 1 3 4 x y p 1 P 1 = struct(‘x’, 3, ’y’, 4);

Accessing a Field 3 4 x y p 1 A = p 1. x

Accessing a Field 3 4 x y p 1 A = p 1. x + p 1. y Assigns the value 7 to A.

Assigning to a Field 3 4 x y p 1. x = p. y^2

Assigning to a Field 3 4 x y p 1. x = p. y^2 Will assign the value 16 to p 1. x

Another Example A = struct(‘name’, ’New York’, … ‘capital’, ‘Albany’, … ‘Pop’, 15. 5)

Another Example A = struct(‘name’, ’New York’, … ‘capital’, ‘Albany’, … ‘Pop’, 15. 5) Can have combinations of string fields and numeric fields.

Legal/Illegal Maneuvers P = struct(‘x’, 3, ’y’, 4) Q = struct(‘x’, 5, ’y’, 6)

Legal/Illegal Maneuvers P = struct(‘x’, 3, ’y’, 4) Q = struct(‘x’, 5, ’y’, 6) R = Q % Legal. R is % copy of Q S = (Q+R)/2 % Illegal.

Legal/Illegal Maneuvers % Illegal… P = struct(‘x’, 3, ’y’) P. y = 4 %

Legal/Illegal Maneuvers % Illegal… P = struct(‘x’, 3, ’y’) P. y = 4 % Legal P = struct(‘x’, 3, ’y’, []) P. y = 4 Using the Empty array as a place holder

Functions and Structures function d = dist(P, Q) % P and Q are points.

Functions and Structures function d = dist(P, Q) % P and Q are points. % d is the distance between them D = sqrt((P. x-Q. x)^2 + … (P. y-Q. y)^2);

Sample “Make” Function function P = Make. Point(x, y) % P is a point

Sample “Make” Function function P = Make. Point(x, y) % P is a point with P. x and P. y % assigned the values x and y. P = struct('x', x, 'y', y); Good Style. Highlights the structure’s definition.

Functions and Structures function Draw. LS(P, Q, c) % P and Q are points.

Functions and Structures function Draw. LS(P, Q, c) % P and Q are points. % Draws a line segment connecting % P and Q. Color specified by c plot([P. x Q. x], [P. y Q. y], c)

Pick Up Sticks Script s = 'rgbmcy'; for k=1: 100 P = Make. Point(randn(1),

Pick Up Sticks Script s = 'rgbmcy'; for k=1: 100 P = Make. Point(randn(1), randn(1)); Q = Make. Point(randn(1), randn(1)); c = s(ceil(6*rand(1))); Draw. LS(P, Q, c) end Generates two random points and chooses one of six colors randomly.

Structure Arrays An array whose components are structures. And all the structures are the

Structure Arrays An array whose components are structures. And all the structures are the same. Example: A point array…

An Array of Points . 50 . 86 x y P(1) = Make. Point(.

An Array of Points . 50 . 86 x y P(1) = Make. Point(. 50, . 86)

An Array of Points -. 50 . 86 x y P(2) = Make. Point(-.

An Array of Points -. 50 . 86 x y P(2) = Make. Point(-. 50, . 86)

An Array of Points -1. 0 0. 0 x y P(3) = Make. Point(-1.

An Array of Points -1. 0 0. 0 x y P(3) = Make. Point(-1. 0, 0. 0)

An Array of Points -. 5 -. 86 x y P(4) = Make. Point(-.

An Array of Points -. 5 -. 86 x y P(4) = Make. Point(-. 50, -. 86)

An Array of Points . 5 -. 86 x y P(5) = Make. Point(.

An Array of Points . 5 -. 86 x y P(5) = Make. Point(. 50, -. 86)

An Array of Points 1. 0 0. 0 x y P(6) = Make. Point(1.

An Array of Points 1. 0 0. 0 x y P(6) = Make. Point(1. 0, 0. 0)

An Array of Points function P = Circle. Points(n) theta = 2*pi/n; for k=1:

An Array of Points function P = Circle. Points(n) theta = 2*pi/n; for k=1: n c = cos(theta*k); s = sin(theta*k); P(k) = Make. Point(c, s); end

Problem Place n points uniformly around the unit circle. Draw all possible triangles obtained

Problem Place n points uniformly around the unit circle. Draw all possible triangles obtained by connecting these points 3 -at-a-time.

Will Need This… function Draw. Triangle(P, Q, R, c) % P, Q, and R

Will Need This… function Draw. Triangle(P, Q, R, c) % P, Q, and R are points and c % is a color, e. g. , 'w', 'r', 'c', etc % Draws a colored triangle with those vertices. fill([P. x Q. x R. x P. x], … [P. y Q. y R. y P. y], c)

These triangles are the same: ( 1, 4, 6), (1, 6, 4), (4, 1,

These triangles are the same: ( 1, 4, 6), (1, 6, 4), (4, 1, 6), (4, 6, 1), (6, 1, 4), (6, 4, 1)

No! for i=1: n for j=1: n for k=1: n Draw a triangle with

No! for i=1: n for j=1: n for k=1: n Draw a triangle with Vertices P(i), P(j), and P(k) End end i, j, and k should be different

Question Time What is the 7 th line of output: for i=1: 5 for

Question Time What is the 7 th line of output: for i=1: 5 for j=i+1: 5 x = 10*i + j end A. 7 B. 21 C. 22 D. 23 E. Other

All Possible (i, j, k) with i < j < k for i=1: n

All Possible (i, j, k) with i < j < k for i=1: n for j=i+1: n for k=j+1: n disp([i j k]) end end

123 124 125 126 134 135 136 145 146 156 i = 1 234

123 124 125 126 134 135 136 145 146 156 i = 1 234 235 236 245 246 256 i = 2 345 346 356 i = 3 456 i = 4

Triangle Solution! for i=1: n for j=i+1: n for k=j+1: n Draw. Triangle( P(i),

Triangle Solution! for i=1: n for j=i+1: n for k=j+1: n Draw. Triangle( P(i), P(j), P(k), 'm') Draw. Points(P, 'w') pause Draw. Triangle(P(i), P(j), P(k), 'k') end end

Structures with Array Fields Let’s develop a structure that can be used to represent

Structures with Array Fields Let’s develop a structure that can be used to represent a colored disk. Four fields: xc: x-coordinate of center yc: y-coordinate of center r: radius c: rgb color vector

Problem Lets compute the “average” of D 1 and D 2: r xc yc

Problem Lets compute the “average” of D 1 and D 2: r xc yc c = = (D 1. r (D 1. xc (D 1. yc (D 1. c + + D 2. r) /2; D 2. xc)/2 D 2. yc)/2 D 2. c) /2; D = struct(‘xc’, xc, ’yc’yc, ’r’, r, ’c’, c)

Example D

Example D