INFSCI 0015 Data Structures Lecture 16: Structures - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

INFSCI 0015 Data Structures Lecture 16: Structures

Description:

INFSCI 0015 Data Structures Lecture 16: Structures – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 14
Provided by: peterbru
Category:

less

Transcript and Presenter's Notes

Title: INFSCI 0015 Data Structures Lecture 16: Structures


1
INFSCI 0015 - Data StructuresLecture 16
Structures
  • Peter Brusilovsky
  • http//www2.sis.pitt.edu/peterb/0015-011/

2
Structures keeping data together
  • / definition /
  • struct point
  • int x
  • int y
  • / var. declaration /
  • struct point pt1
  • struct point pt2
  • 1, 3

x
y
pt1
pt2
x
x
1
y
y
3
3
Structures Keeping data together
  • struct student
  • char name
  • short ybirth
  • float height
  • struct student st1
  • "John Doe",
  • 1989,
  • 172.43

name
ybirth
height
st1
name
1989
ybirth
172.3
height
John Doe
4
Structures What we can do?
  • Assign field
  • p1.x 20
  • p1.y 0
  • Use field
  • if(p1.x lt 30) p1.y
  • Assign structure
  • p2 p1
  • Take address
  • pointptr p1

x
p1
20
y
0
x
p1
20
y
1
x
p2
20
y
1
pointptr
5
Structures What else we can do?
  • Input a field of a structure
  • scanf("d (p1.x))
  • Pass the whole structure as a parameter to a
    function
  • myfunc(p1)
  • Return the whole structure from a function
  • struct point f(int a)
  • ....
  • pt3 f(3)

6
Example 15.1 Above or Under
  • main()
  • struct point pbase, p
  • int i
  • pbase readpoint("Enter base point") / get
    base point /
  • / get and check points /
  • for (i 1 i lt 5 i)
  • p readpoint("Enter point")
  • if (p.y gt pbase.y)
  • printf("The point (d, d) is above (d,
    d)\n",
  • p.x, p.y, pbase.x, pbase.y)
  • else if (p.y lt pbase.y)
  • printf("The point (d, d) is under (d,
    d)\n",
  • p.x, p.y, pbase.x, pbase.y)
  • else
  • printf("(d, d) is on the same level with
    (d, d)\n",
  • p.x, p.y, pbase.x, pbase.y)

7
Example 15.1 Above or Under
  • / readpoint reads a point and returns it /
  • struct point readpoint(char prompt)
  • struct point input
  • printf("s (x y)", prompt)
  • scanf("d d", (input.x), (input.y))
  • return input

8
Structures vs. Arrays
  • Structures
  • Heterogeneous data
  • Each element has to be defined
  • struct point
  • int x int y
  • Elements are accessed by name top.x, mypt.y
  • Arrays
  • Homogeneous data
  • Only overall number of elements has to be
    defined char a5
  • Elements are accessed by index ar7, line23
  • Index can be computed ari-1

9
Nested Structures
  • struct rect
  • struct point pt1
  • struct point pt2
  • struct rect field
  • 10, 10,
  • 20, 30
  • dist 800 - field.pt1.x

pt2
pt1
dist
10
Pointers to structures
  • Pointers to structures are used often
  • struct point pt1
  • How we can address fields having a pointer to a
    structure?
  • (pt1).x 3 (pt1).y 5
  • Special way to access fields using a pointer to
    the structure
  • pt1-gtx 3 pt1-gty 5

pt1
x
3
y
5
11
Example 16.2 Distance 7 (Part 2)
  • Lets modify example 16.1 so that for each point
    entered the program prints its distance from the
    base point (instead of saying whether the point
    is above or under the base).
  • As you know, distance between points (a, b) and
    (c, d) is sqrt ((a-c)2 (b-d)2 )
  • We use a different kind of point
  • struct point
  • float x float y

12
Example 16.2 Main part
  • struct point readpoint(char prompt)
  • double distance(struct point p1, struct point
    p2)
  • void main()
  • struct point pbase, p
  • int i
  • / get base point /
  • pbase readpoint("Enter base point")
  • / get and check points /
  • for (i 1 i lt 5 i)
  • p readpoint("Enter point")
  • printf("Distance from base point is .2f\n",
    distance(pbase, p))

13
Example 16.2 Functions
  • / readpoint reads a point and returns it /
  • struct point readpoint(char prompt)
  • struct point input
  • printf("s (x y)", prompt)
  • scanf("f f", (input.x), (input.y))
  • return input
  • / distance returns distance between points /
  • double distance(struct point pptr1, struct point
    pptr2)
  • double xdiff, ydiff
  • xdiff pptr1-gtx - pptr2-gtx
  • / same as (pptr1).x - (pptr2).x /
  • ydiff pptr1-gty - pptr2-gty
  • return(sqrt(xdiffxdiff ydiffydiff))
Write a Comment
User Comments (0)
About PowerShow.com