+ All Categories
Home > Documents > COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03 Audience planning to major in COSC...

COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03 Audience planning to major in COSC...

Date post: 01-Jan-2016
Category:
Upload: gerald-powell
View: 239 times
Download: 0 times
Share this document with a friend
Popular Tags:
33
COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03 · Audience - planning to major in COSC (prereq. 1P02 60%) · Lectures (AS202, AS217), labs (J301), tutorials (AS204), Help Desk (J328) · Web - COSC 1P03: https://lms.brocku.ca/portal/site/COSC1P03D03FW2014MAIN/ - COSC: http://www.cosc.brocku.ca/ · Course Outline - Textbook - Software - Marking scheme - Assignment submission/return · Plagiarism · Succeeding in COSC 1P03 - labs/assignments
Transcript
Page 1: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

COSC 1P03

Introduction to Data Structures 1.1

COSC 1P03· Audience

- planning to major in COSC (prereq. 1P02 60%)· Lectures (AS202, AS217), labs (J301), tutorials (AS204), Help Desk

(J328)· Web

- COSC 1P03: https://lms.brocku.ca/portal/site/COSC1P03D03FW2014MAIN/- COSC: http://www.cosc.brocku.ca/

· Course Outline- Textbook- Software- Marking scheme- Assignment submission/return

· Plagiarism· Succeeding in COSC 1P03

- labs/assignments

Page 2: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

COSC 1P03

Introduction to Data Structures 1.2

Chapter 1Arrays

Page 3: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

COSC 1P03

Introduction to Data Structures 1.3

Arrays

· Collection- like Pictures & Sounds

· Elements (components)- any type (primitive or object)

· Non-sequential processing- not just in order of occurrence- selective (random) processing

Page 4: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

COSC 1P03

Introduction to Data Structures 1.4

Above-average Rainfall· Months with rainfall above monthly average for year· Non-sequential processing

- must process all data to compute average- then process the data to list those above average

· 12 months- array of 12 double values- declare then create- memory model

· Average- loop to read and sum

° length attribute° setting element value° getting element value

- then compute average· Listing those above average

- second loop

Page 5: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

double[] rainfall;

rainfall = new double[12];

rainfall ? ? ? ? ? ? ? ? ? ?

0 1 2 3 4 5 6 7 8 9

? 10 ? 11

rainfall[i] = in.readDouble();

totRain = totRain + rainfall[i];

Page 6: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

COSC 1P03

Introduction to Data Structures 1.6

Class Statistics

· Report with average and standard deviation of student marks

- data file· Array of Student objects· Array size not known

- choose suitable size (constant)- keep count

· Array organization- not all elements used

· Standard Deviation formula:

Page 7: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

private static final int MAX_STD = 100 : Student[] students; int numStd; : students = new Student[MAX_STD];

Page 8: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),
Page 9: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

COSC 1P03

Introduction to Data Structures 1.9

· Reading data

- termination° no more data° no more space

×error message?· Computing standard deviation

- array as parameter- count also parameter- second traversal

Page 10: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

private double computeStd ( Student[] students, int numStd, double ave ) {

Page 11: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

COSC 1P03

Introduction to Data Structures 1.11

University Statistics

· University enrolments at universities in a number of departments

- produce summary table· Data

- Multi-dimensional° university° department

· Multi-dimensional array- declaration- rows are departments- columns are universities

Page 12: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),
Page 13: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

int nUniv; int nDept; int[][] enroll; : enrol = new int[nDept][nUniv];

Page 14: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

COSC 1P03

Introduction to Data Structures 1.14

· Processing- reading statistics- computing department sums

° processing by row- computing university sums

° processing by column- computing grand total

° processing all elements- producing summary table

Page 15: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

for ( int i=0 ; i<stats.length ; i++ ) { : for ( int j=0 ; j<stats[i].length ; j++ ) { : …enroll[i][j]… : }; : };

for ( int j=0 ; j<stats[0].length ; j++ ) { : for ( int i=0 ; i<stats.length ; i++ ) { : …enroll[i][j]… : }; : };

Page 16: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

Consolidation

Page 17: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

COSC 1P03

Introduction to Data Structures 1.17

Single-dimensio

nal Arrays

· Collection· Use

- declaration- creation- subscripting (indexing)

° zero-based° NullPointerException° ArrayIndexOutOfBoundsException

- length attribute· Memory model· Reference type

- array assignment- element assignment

Page 18: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

type [ ] name; double[] rainfall; Student[] students;

new type [ expr ] rainfall = new double[12]; students = new Student[MAX_STD];

name [ expr ] rainfall[i] = in.readDouble(); totRain = totRain + rainfall[i]; student[i] = aStudent; sum = sum + student[i].getTestMark();

Page 19: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

double rainfall; double nrf;

rainfall ? ? ? ? ? ? ? ? ? ?

0 1 2 3 4 5 6 7 8 9

? 10 ? 11

nrf

rainfall = new double[12];

nrf[7] = rainfall[5];

rainfall[5] = 5;

nrf = rainfall;

Page 20: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

COSC 1P03

Introduction to Data Structures 1.20

Processing One-dimensional Arrays

· “Right-Sized”- size known a priori or computable- create with known size- all elements have values- traversal patterns

· “Variable-sized”- array size not known

° choose arbitrary size (constant)° keep count in ancillary variable

- not all elements have values- traversal pattern

Page 21: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

for ( int i=0 ; i<a.length ; i++ ) { process a[i] };

for ( int i=0 ; i<students.length ; i++ ) { total = total + students[i].getMark(); };

for ( type e : a ) { process e };

for ( std : students ) { total = total + std.getMark(); };

Page 22: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

for ( int i=0 ; i<numberOfElements ; i++ ) { process a[i] };

for ( int i=0 ; i<numStd ; i++ ) { total = total + stds[i].getMark(); };

Page 23: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

COSC 1P03

Introduction to Data Structures 1.23

Multidimensional Arrays

· 2 or more dimensions- e.g. table- like Picture

· Declaration· Creation· Subscripting· length· “Variable-sized”

- fill upper left corner- one counter for each dimension

Page 24: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

type [ ]… name; int[][] enrol;

new type [ expr ]… enrol = new int[nDept][nUniv];

name [ expr ]… enrol[i][j] = … …enrol[i][j]…

name.length …enrol.length… name[expr].length …enrol[i].length…

Page 25: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

COSC 1P03

Introduction to Data Structures 1.25

Processing 2-Dimensional Arrays

· Random access- a[i][j]- look-up table

· Sequential access- row-major order

° lexicographic order° algorithm

- column-major order° algorithm° regular arrays

· Enrolment system example

Page 26: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

for ( int i=0 ; i<a.length ; i++ ) { preprocessing for row i for ( int j=0 ; j<a[i].length ; j++ ) { process a[i][j] }; postprocessing for row i };

total = 0; for ( int i=0 ; i<enrol.length ; i++ ) { for ( int j=0 ; j<enrol[i].length ; j++ ) { total = total + enrol[i][j]; }; };

Page 27: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

for ( int j=0 ; j<a[0].length ; j++ ) { preprocessing for column j for ( int i=0 ; i<a.length ; i++ ) { process a[i][j] }; postprocessing for column j };

total = 0; for ( int j=0 ; j<enrol[0].length ; j++ ) { for ( int i=0 ; i<enrol.length ; i++ ) { total = total + enrol[i][j]; }; };

Page 28: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

COSC 1P03

Introduction to Data Structures 1.28

Array Representation

· single dimensional- contiguous allocation

° value/object/array is sequence of consecutive cells- address(a[i]) = address(a) + i s

° where×s = size of element type

- if not zero-based subscripting° address(a[i]) = address(a) + (i-l) s° where

×l = lower bound

Page 29: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

COSC 1P03

Introduction to Data Structures 1.29

· Multi-dimensional

- row-major vs column-major- lexicographic (row-major) ordering- consider as array of rows- address(a[i]) = address(a) + i S

° where×S = size of row (S=a[0].length s)

° start of ith row- address(a[i][j]) = address(a[i]) + j s- substitution gives

° address(a[i][j]) = address(a) + i S + j s- for non-zero based

° address(a[i][j]) = address(a) + (i-lr) S + (j-lc) s

Page 30: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

Row-major

Column-major

Page 31: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

COSC 1P03

Introduction to Data Structures 1.31

Arrays of Arrays

· Non-contiguous allocation- each row contiguous

· Memory model· Addressing

- address(a[i][j] = content(address(a)+i4) + j s· Access

- row-major order more efficient· Java uses array-of-arrays allocation· Ragged array

Page 32: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),
Page 33: COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),

Recommended