Presentation is loading. Please wait.

Presentation is loading. Please wait.

DATA STRUCTURES 5/3/2019© 2006 ITT Educational Services Inc.

Similar presentations


Presentation on theme: "DATA STRUCTURES 5/3/2019© 2006 ITT Educational Services Inc."— Presentation transcript:

1 DATA STRUCTURES 5/3/2019© 2006 ITT Educational Services Inc.
Structured Programming: Unit 7 Slide 1

2 Objectives Choose the data structure that best represents a type of data. Data structures include: Use a single-dimensional array. Use a two-dimensional array. Describe a collection. Describe user-defined types. Declare and use enumerations. 5/3/2019© 2006 ITT Educational Services Inc. Structured Programming: Unit 7 Slide 2

3 Array Storage 5/3/2019© 2006 ITT Educational Services Inc.
Structured Programming: Unit 7 Slide 3

4 Array Program Example Declare Rain[12] Of Reals
Declare Sum, Average As Real Declare K As Integer Set Sum = 0 For K=1 Step 1 to 12 Write “Enter rainfall for month”, K Input Rain[K] Set Sum = Sum + Rain[K] End For Set Average = Sum /12 5/3/2019© 2006 ITT Educational Services Inc. Structured Programming: Unit 7 Slide 4

5 Multiple Operations on an Array
Declare X[100] Set Sum = 0 Set Count = 0 Input Num While Num <> 0 //Fill array Set Count = Count+1 Set X[Count] = Num End While Set Average = Sum/Count Set OverAverage = 0 For K = 1 Step 1 to Count If X[K] > Average Then Set OverAverage = OverAverage+1 End If End For 5/3/2019© 2006 ITT Educational Services Inc. Structured Programming: Unit 7 Slide 5

6 Displaying Items in Reverse Order
Declare Names[100] Set Count = 0 Input TempName While TempName<> “*” Set Count = Count+1 Set Names[Count] = TempName End While For K = Count Step -1 To 1 Write Names[K] End For 5/3/2019© 2006 ITT Educational Services Inc. Structured Programming: Unit 7 Slide 6

7 Parallel Arrays Declare Names[100], Sales[100] Set Max = 0 Set K = 1
Write “Enter name and sales” Write “Enter *, 0 when done.” Input Names[K], Sales[K] While Names[K] <> “*” If Sales[K] > Max then Set Index = K Set Max = Sales[Index] End If Set K = K + 1 End While 5/3/2019© 2006 ITT Educational Services Inc. Structured Programming: Unit 7 Slide 7

8 Parallel Arrays Flowchart
5/3/2019© 2006 ITT Educational Services Inc. Structured Programming: Unit 7 Slide 8

9 Serial Search of an Array
5/3/2019© 2006 ITT Educational Services Inc. Structured Programming: Unit 7 Slide 9

10 Serial Search Example Declare IDNumbers[100], Names[100] Input IDKey
Set Index = 0 Set Found = 0 While (Found = 0) And (Index < N) Set Index = Index + 1 If IDNumbers[Index] = IDKey Then Set Found = 1 End If If Found = 0 Then Write “Not found” Else Write “ID Number:”, IDKey Write “Student Name:”, Names[Index] 5/3/2019© 2006 ITT Educational Services Inc. Structured Programming: Unit 7 Slide 10

11 Bubble Sort Pseudocode
While the array A is not sorted For K = 1 Step 1 to N-1 If A[K]>A[K+1] Then Interchange A[K] and A[K+1] End If End For End While 5/3/2019© 2006 ITT Educational Services Inc. Structured Programming: Unit 7 Slide 11

12 How a Bubble Sort Works 5/3/2019© 2006 ITT Educational Services Inc.
Structured Programming: Unit 7 Slide 12

13 Bubble Sort Flowchart 5/3/2019© 2006 ITT Educational Services Inc.
Structured Programming: Unit 7 Slide 13

14 Bubble Sort Using a Flag
Set Flag = 0 For K = 1 Step 1 To Count-1 If A[K] > A[K+1] Then Set Temp = A[K] Set A[K] = A[K+1] Set A[K+1] = Temp End If End For 5/3/2019© 2006 ITT Educational Services Inc. Structured Programming: Unit 7 Slide 14

15 Strings as Arrays 5/3/2019© 2006 ITT Educational Services Inc.
Structured Programming: Unit 7 Slide 15

16 Array of Scores 5/3/2019© 2006 ITT Educational Services Inc.
Structured Programming: Unit 7 Slide 16

17 Two-Dimensional Array Example
Declare Scores[30,5] For Student = 1 Step 1 to 30 For Test = 1 Step 1 to 5 InputScores[Student, Test] End For(Test) End For(Student) 5/3/2019© 2006 ITT Educational Services Inc. Structured Programming: Unit 7 Slide 17

18 Focus on Problem Solving: Sample Invoice
5/3/2019© 2006 ITT Educational Services Inc. Structured Programming: Unit 7 Slide 18

19 Hierarchy Chart 5/3/2019© 2006 ITT Educational Services Inc.
Structured Programming: Unit 7 Slide 19

20 Load Price List Module Open “PriceList” For Input as DataFile
Set ListCount = 0 While Not EOF(DataFile) Set ListCount = ListCount + 1 Read DataFile, Numbers[ListCount], Names[ListCount], Prices[ListCount] End While Close Data File 5/3/2019© 2006 ITT Educational Services Inc. Structured Programming: Unit 7 Slide 20

21 Input Parts Order Module
Declare Num, Amt As Integer Set OrderCount = 0 Input Num, Amt While Num <> 0 Set OrderCount = OrderCount + 1 Set OrderNums[OrderCount] = Num Set OrderAmts[OrderCount] = Amt End While 5/3/2019© 2006 ITT Educational Services Inc. Structured Programming: Unit 7 Slide 21

22 Sort Parts Order Module
Declare Flag, K, Temp As Integer Set Flag = 0 While Flag = 0 Set Flag = 1 For K=1 Step 1 to OrderCount -1 If OrderNums[K] > OrderNums[K+1] Then Set Temp=OrderNums[K] Set OrderNums[K] = OrderNums[K+1] Set OrderNums[K+1] = Temp Set Temp=OrderAmts[K] Set OrderAmts[K] = OrderAmts[K+1] Set OrderAmts[K+1] = Temp End If End For End While 5/3/2019© 2006 ITT Educational Services Inc. Structured Programming: Unit 7 Slide 22

23 Search For Parts Order Module
Set Index = 0 Set Found = 0 While (Found = 0) And (Index < ListCount) Set Index = Index+1 If Numbers[Index] = OrderNums[K] Then Set Found = 1 End If End While 5/3/2019© 2006 ITT Educational Services Inc. Structured Programming: Unit 7 Slide 23

24 Collections Like an array that holds objects
Must implement an iterator 5/3/2019© 2006 ITT Educational Services Inc. Structured Programming: Unit 7 Slide 24

25 User-defined Types Stores attributes of various data types
Most commonly encountered in legacy applications (like VB 6) Usually use a class instead of a user-defined type 5/3/2019© 2006 ITT Educational Services Inc. Structured Programming: Unit 7 Slide 25

26 Enumerations Used to provide constants with a meaningful name Example:
Enum Measurements InchesInFoot = 12 FeetInYard = 3 InchesInYard = 36 End Enum 5/3/2019© 2006 ITT Educational Services Inc. Structured Programming: Unit 7 Slide 26

27 Summary In this unit, students learned:
To use single-dimensional arrays To use parallel arrays To perform a serial search To perform a bubble sort To store strings as arrays of characters To use two-dimensional arrays To design collections To use enumerations To represent data with a user-defined type 5/3/2019© 2006 ITT Educational Services Inc. Structured Programming: Unit 7 Slide 27


Download ppt "DATA STRUCTURES 5/3/2019© 2006 ITT Educational Services Inc."

Similar presentations


Ads by Google