COSC 1P03 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 Introduction to Data Structures 1.1 COSC 1P03 Chapter 1 Arrays Introduction to Data Structures 1.2 COSC 1P03 Arrays Collection like Pictures & Sounds Elements (components) any type (primitive or object) Non-sequential processing not just in order of occurrence selective (random) processing Introduction to Data Structures 1.3 COSC 1P03 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 Introduction to Data Structures 1.4 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 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: Introduction to Data Structures 1.6 private static final int MAX_STD = 100 : Student[] students; int numStd; : students = new Student[MAX_STD]; COSC 1P03 Reading data termination no more data no more space error message? Computing standard deviation array as parameter count also parameter second traversal Introduction to Data Structures 1.9 private double computeStd ( Student[] students, int numStd, double ave ) { COSC 1P03 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 Introduction to Data Structures 1.11 int int int[][] nUniv; nDept; enroll; : enrol = new int[nDept][nUniv]; COSC 1P03 Processing reading statistics computing department sums processing by row computing university sums processing by column computing grand total processing all elements producing summary table Introduction to Data Structures 1.14 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 Single-dimensional Arrays Collection Use declaration creation subscripting (indexing) zero-based NullPointerException ArrayIndexOutOfBoundsException length attribute Memory model Reference type array assignment element assignment Introduction to Data Structures 1.17 type [ ] name; double[] Student[] rainfall; 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; rainfall double nrf; rainfall = new double[12]; rainfall[5] = 5; nrf = rainfall; nrf[7] = rainfall[5]; nrf ? ? ? ? ? ? ? ? ? ? ? ? 0 1 2 3 4 5 6 7 8 9 10 11 COSC 1P03 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 Introduction to Data Structures 1.20 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 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 Introduction to Data Structures 1.23 type [ ]… name; int[][] enrol; new type [ expr ]… enrol = new int[nDept][nUniv]; name [ expr ]… enrol[i][j] = … …enrol[i][j]… name.length name[expr].length …enrol.length… …enrol[i].length… COSC 1P03 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 Introduction to Data Structures 1.25 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 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 Introduction to Data Structures 1.28 COSC 1P03 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 Introduction to Data Structures 1.29 Row-major Column-major COSC 1P03 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 Introduction to Data Structures 1.31