COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03 Audience planning to major in COSC...
-
Upload
gerald-powell -
Category
Documents
-
view
240 -
download
0
Transcript of 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 (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
COSC 1P03
Introduction to Data Structures 1.2
Chapter 1Arrays
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
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
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];
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:
private static final int MAX_STD = 100 : Student[] students; int numStd; : students = new Student[MAX_STD];
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
private double computeStd ( Student[] students, int numStd, double ave ) {
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
int nUniv; int nDept; int[][] enroll; : enrol = new int[nDept][nUniv];
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
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]… : }; : };
Consolidation
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
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();
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;
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
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(); };
for ( int i=0 ; i<numberOfElements ; i++ ) { process a[i] };
for ( int i=0 ; i<numStd ; i++ ) { total = total + stds[i].getMark(); };
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
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…
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
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]; }; };
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]; }; };
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
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
Row-major
Column-major
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