week 12 lecture slides

6
COSC 1P02 Introduction to Computer Science 1.1 Cosc 1P03 Week 12 Lecture slides “Example is not the main thing in influencing others, it is the only thing.” Albert Schweitzer

Upload: rowena

Post on 15-Jan-2016

34 views

Category:

Documents


0 download

DESCRIPTION

Cosc 1P03. “Example is not the main thing in influencing others, it is the only thing.” Albert Schweitzer. Week 12 Lecture slides. Arrays. collections of values (objects) elements use declare create process memory model length attribute Returns the size of the array. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Week 12 Lecture slides

COSC 1P02

Introduction to Computer Science 1.1

Cosc 1P03

Week 12 Lecture slides

“Example is not the main thing in influencing others, it is the only thing.”

Albert Schweitzer

Page 2: Week 12 Lecture slides

COSC 1P02

Introduction to Computer Science 1.2

Arrays collections of values (objects)

elements use

declare create process

memory model length attribute

Returns the size of the array. RainFall Example

Print out only those months with above average rainfall.

Palindrome Example Strings as array of char

toCharArray & String(char[])

Page 3: Week 12 Lecture slides

COSC 1P02

Introduction to Computer Science 1.4

Processing “Variable-sized” Arrays

Example: standard deviation of student marks data file

Array of Student objects Array size not known

choose arbitrary size (constant) keep count

Array organization not all elements used

Std Dev formula:

Page 4: Week 12 Lecture slides

COSC 1P02

Introduction to Computer Science 1.8

Reading data

traversal Student constructor termination

no more data no more space

error Computing standard deviation

array as parameter second traversal

Traversal pattern

Page 5: Week 12 Lecture slides

COSC 1P02

Introduction to Computer Science 1.12

Arrays vs Sounds

Array Sound declaration Student[] students; Sound aSound;

creation students = new Student[100]; aSound = new Sound(25000);

access element aStudent = students[i]; aSample = aSound.getSample(i);

modify element aStudent.updateMark(…); aSample.setValue(…);

modify element students[i].updateMark(…); aSound.getSample(i).setValue(…);

replace element students[i] = aStudent;

for-each for ( aStudent : students ) for ( aSample : aSound )

iteration for ( int i=0 ; i<students.length ; i++) for ( int i=0 ; i<aSound.getNumSamples() ; i++ )

Page 6: Week 12 Lecture slides

COSC 1P02

Introduction to Computer Science 1.25

Good Luck on your Exam!!