{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS 100 M Lecture 15

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

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

15. Structures Simple Structures Structure Arrays Structures with Array Fields Other Possibilities

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

View Full Document
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 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

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

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

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

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

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

View Full Document
Accessing a Field p1 x y 3 4 A = p1.x + p1.y Assigns the value 7 to A.
Assigning to a Field p1 x y 3 4 p1.x = p.y^2 Will assign the value 16 to p1.x

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

View Full Document
Another Example
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern