L20post - Previous Lecture: Structure Todays Lecture:...

This preview shows pages 1–19. Sign up to view the full content.

± Previous Lecture: ± Structure ± Today’s Lecture: ± Structure arrays ± Working with large data files ± Built-in sort function ± Read Chapter 11 to learn about the .bin file format ± Announcement: ± P5 due tonight at 11pm ± Review session Sunday 1:30-3pm, Upson B17 ± Several office hour changes—check website ± Prelim 2 Tuesday, Nov 10 th

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

View Full Document
November 5, 2009 Lecture 20 2 Structure Arrays ± An array whose components are structures ± All the structures must be the same (have the same fields) in the array ± Example: an array of points (point structures) .86 y .5 x .91 y 1.5 x .28 y .4 x 1.8 y 2.5 x P P(1) P(2) P(3) P(4)
November 5, 2009 Lecture 20 3 P(1) x y .50 An Array of Points .86 P(1) = MakePoint(.50,.86)

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

View Full Document
November 5, 2009 Lecture 20 4 P(2) x y -.50 An Array of Points .86 P(2) = MakePoint(-.50,.86)
November 5, 2009 Lecture 20 5 P(3) x y -1.0 An Array of Points 0.0 P(3) = MakePoint(-1.0,0.0)

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

View Full Document
November 5, 2009 Lecture 20 6 P(4) x y -.5 An Array of Points -.86 P(4) = MakePoint(-.50,-.86)
November 5, 2009 Lecture 20 7 P(5) x y .5 An Array of Points -.86 P(5) = MakePoint(.50,-.86)

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

View Full Document
November 5, 2009 Lecture 20 8 P(6) x y 1.0 An Array of Points 0.0 P(6) = MakePoint(1.0,0.0)
November 5, 2009 Lecture 20 9 function P = CirclePoints(n) theta = 2*pi/n; for k=1:n c = cos(theta*k); s = sin(theta*k); P(k) = MakePoint(c,s); end Function returning an array of points (point structures)

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

View Full Document
November 5, 2009 Lecture 20 10 ± Place n points uniformly around the unit circle. ± Draw all possible triangles obtained by connecting these points 3-at-a-time. Example: all possible triangles ( i , j , k ) = ( 1 , 2 , 4 ) 1 2 3 45 6 ( i , j , k ) = ( 1 , 2 , 6 ) 1 2 3 6
November 5, 2009 Lecture 20 11 function DrawTriangle(P,Q,R,c) % Draw c-colored triangle; % triangle vertices are points P, % Q, and R. fill([P.x Q.x R.x P.x], . .. [P.y Q.y R.y P.y], c)

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

View Full Document
November 5, 2009 Lecture 20 12 ( i , j , k ) = ( 1 , 3 , 6 ) 1 2 3 45 6 ( i , j , k ) = ( 1 , 4 , 5 ) 1 2 3 6 The following triangles are the same: (1,4,6), (1,6,4), (4,1,6), (4,6,1), (6,1,4), (6,4,1)
November 5, 2009 Lecture 20 13 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 end Bad! i, j, and k should be different, and there should be no duplicates

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

View Full Document
November 5, 2009 Lecture 20 14 1 2 3 1 2 4 1 2 5 1 2 6 1 3 4 1 3 5 1 3 6 1 4 5 1 4 6 1 5 6 2 3 4 2 3 5 2 3 6 2 4 5 2 4 6 2 5 6 3 4 5 3 4 6 3 5 6 4 5 6 i = 1 i = 4 i = 3 i = 2 All possible (i,j,k) combinations but avoid duplicates : i < j < k for i= 1 :n-2 for j= i+1 :n-1 for k= j+1 :n disp([i j k]) end end end i j k
November 5, 2009 Lecture 20 15 for i= 1 :n-2 for j= i+1 :n-1 for k= j+1 :n % Draw triangle with % vertices P(i),P(j),P(k) end end end All possible (i,j,k) combinations but avoid duplicates : i < j < k

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

View Full Document
November 5, 2009 Lecture 20 16 for i= 1 : n-2 for j= i+1 : n-1 for k= j+1 : n % Draw triangle with % vertices P(i),P(j),P(k) end end end All possible (i,j,k) combinations but avoid duplicates : i < j < k
November 5, 2009 Lecture 20 17 % Drawing on a black background for i=1:n-2 for j=i+1:n-1 for k=j+1:n DrawTriangle( P(i),P(j),P(k),'m') DrawPoints(P) pause DrawTriangle(P(i),P(j),P(k),'k') end end end All possible triangles

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

View Full Document
November 5, 2009 Lecture 20 18 1 2 3 1 2 4 1 2 5 1 2 6 1 3 4 1 3 5 1 3 6 1 4 5 1 4 6 1 5 6 2 3 4 2 3 5 2 3 6 2 4 5 2 4 6 2 5 6 3 4 5 3 4 6 3 5 6 4 5 6 i = 1 i = 4 i = 3 i = 2 All possible (i,j,k) combinations but avoid duplicates: i < j < k for i=1:n-2 for j=i+1:n-1 for k=j+1:n disp([i j k]) end end end i j k
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/06/2010 for the course CS 9339 taught by Professor Gries during the Fall '09 term at Cornell University (Engineering School).

Page1 / 60

L20post - Previous Lecture: Structure Todays Lecture:...

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

View Full Document
Ask a homework question - tutors are online