{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS 100 M Lecture 15

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

Info icon This 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
Image of page 1

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

View Full Document Right 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?
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
Image of page 3

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

View Full Document Right Arrow Icon
Seen This Before Functions are used to ``package’’ calculations. Elevates the level of our reasoning. Critical for problem solving.
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 .
Image of page 5

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

View Full Document Right 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.
Image of page 6
How to Visualize p1 p1 x y 3 4 P1 = struct(‘x’,3,’y’,4);
Image of page 7

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

View Full Document Right Arrow Icon
Accessing a Field p1 x y 3 4 A = p1.x + p1.y Assigns the value 7 to A.
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
Image of page 9

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern