data structures [1]

10
Computing CP1 Data Structures 1. The Teacher Data Structures [1] CP1

Upload: tanisha-blackwell

Post on 31-Dec-2015

16 views

Category:

Documents


2 download

DESCRIPTION

Data Structures [1]. CP1. Arrays. All data must be the same data type…. Eg . (Daily sales figures for John - to nearest £) Mon 240 Tue 230 Wed 180 Thu 270 Fri 120 (All of data type Integer ). In Visual Basic…. Dim Sales(5) As Integer - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Data Structures [1]

Computing CP1 Data Structures 1. The Teacher

Data Structures [1]

CP1

Page 2: Data Structures [1]

Computing CP1 Data Structures 1. The Teacher

Arrays

• All data must be the same data type….

• Eg. (Daily sales figures for John - to nearest £)

Mon 240

Tue 230

Wed 180

Thu 270

Fri 120

(All of data type Integer)

Page 3: Data Structures [1]

Computing CP1 Data Structures 1. The Teacher

In Visual Basic…Dim Sales(5) As Integer

A one-dimensional array has one subscript to identify the element of the array.

Eg.Mon 240

Tue 230Wed 180Thu 270Fri 120

Sales(4)

Page 4: Data Structures [1]

Computing CP1 Data Structures 1. The Teacher

Two-dimensional Arrays• Data must be the same type.• A two-dimensional array has 2 subscripts.

Eg. Sales figures for John, Mary and Sue:

John Mary SueMon 240 420 140Tue 230 380 190Wed 180 400 210Thu 270 360 240Fri 120 320 190

Sales(4,2)

Page 5: Data Structures [1]

Computing CP1 Data Structures 1. The Teacher

Three-dimensional Arrays

• Data must be the same type.• A two-dimensional array has 3 subscripts.

Eg. Sales figures for John, Mary and Sue for each of three different items…

Item 1 John Mary SueMon 240 420 140Tue 230 380 190Wed 180 400 210Thu 270 360 240Fri 120 320 190

Item 2 John Mary SueMon 440 440 120Tue 530 380 390Wed 480 440 280Thu 670 365 540Fri 150 310 490

Item 3 John Mary SueMon 210 430 120Tue 130 280 190Wed 120 100 220Thu 270 320 143Fri 120 220 295

Page 6: Data Structures [1]

Computing CP1 Data Structures 1. The Teacher

• …and YES! In programming you can have Arrays with more than three dimensions…

• All data must be of the same data type

• An n-dimensional array would use n subscripts to identify each element.

Page 7: Data Structures [1]

Computing CP1 Data Structures 1. The Teacher

Records

• Data may be of different types• Eg. (Name, Form, DOB, Exam mark)

John Smith

12 T

12/04/85

65

(Text strings, a date and a number)

Page 8: Data Structures [1]

Computing CP1 Data Structures 1. The Teacher

In Visual Basic…

Structure PupilType

Dim PupilName As String

Dim Form As String

Dim DOB As Date

Dim ExamMark As Integer

End Structure

Page 9: Data Structures [1]

Computing CP1 Data Structures 1. The Teacher

Structure PupilType

Dim PupilName As String

Dim Form As String

Dim DOB As Date

Dim ExamMark As Integer

End Structure

A variable Pupil can be declared as being of type PupilType, and the fields assigned…

Dim Pupil As PupilType

Pupil.PupilName = “John Smith”

Pupil.Form = “12T”

Pupil.DOB=“12.04.85”

Pupil.ExamMark = 65

Page 10: Data Structures [1]

Computing CP1 Data Structures 1. The Teacher

Arrays of Records

PupilName Form DOB ExamMark

1 John Smith 12T 12/04/85 65

2 Sally Jones 12G 15/05/85 78

3 Tom Jenkins 12G 19/11/84 52

4 Ruby Day 12T 06/01/85 44

5 Jenny Thomas 12B 07/12/84 60

An array of records is sometimes called a TABLE.

Each record has a subscript…If this table was called Markbook, then

Markbook(4).ExamMark is 44.