week 12 lecture slides
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 PresentationTRANSCRIPT
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
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[])
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:
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
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++ )
COSC 1P02
Introduction to Computer Science 1.25
Good Luck on your Exam!!