COSC 1P03 - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

COSC 1P03

Description:

444444 Im Ok 70. 555555 Joe Average 60. COSC 1P02. Introduction ... Create the array, Size MAX_STD. Counter used to keep track of the number of students read. ... – PowerPoint PPT presentation

Number of Views:106
Avg rating:3.0/5.0
Slides: 10
Provided by: hug276
Category:

less

Transcript and Presenter's Notes

Title: COSC 1P03


1
Cosc 1P03
Example is not the main thing in influencing
others, it is the only thing. Albert
Schweitzer
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)

3
RainFall
  • private void display ( )
  • String year // year
  • double rainfall // rainfall for each
    month
  • double totRain // total rainfall
    for the year
  • double aveRain // average monthly
    rainfall
  • year in.readString()
  • rainfall new double12
  • totRain 0
  • for ( int i0 iltrainfall.length i )
  • rainfalli in.readDouble()
  • totRain totRain rainfalli
  • aveRain totRain / rainfall.length
  • writeHeader(year,aveRain)
  • for ( int i0 iltrainfall.length i )
  • if ( rainfalli gt aveRain )
  • writeDetail(i,year,rainfalli)

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

5
Data File
COSC 1P02 Term Test 111111 Ima First 100 222222 Ma
ry Marvelous 90 333333 Pretty Good 80 444444 Im
Ok 70 555555 Joe Average 60
6
Output
7
Memory Model
8
  • Reading data
  • traversal
  • Student constructor
  • termination
  • no more data
  • no more space
  • error
  • Computing standard deviation
  • array as parameter
  • second traversal
  • Traversal pattern

9
Traversal Pattern
10
Std. Dev
  • private void genReport ( )
  • String course // course name
  • String work // name of piece of
    work
  • Student students // the class of
    students
  • Student aStudent // one student
  • int numStd // number of
    students
  • double sum // sum of marks
  • double ave // average mark
  • double std // standard
    deviation
  • course stData.readString()
  • work stData.readString()
  • setUpReport(course,work)
  • students new StudentMAX_STD
  • numStd 0
  • sum 0
  • while ( true )
  • aStudent new Student(stData)

11
Calculating Std. Dev
  • private double computeStd ( Student students,
    int numStd, double ave )
  • double sum // sum of squares of
    deviations
  • double aMark // a student mark
  • sum 0
  • for ( int i0 iltnumStd i )
  • aMark studentsi.getMark()
  • sum sum pow(aMark-ave,2)
  • return sqrt(sum/numStd)
  • // computeStd

12
Multidimensional Arrays
  • e.g. tables
  • 2 or more dimensions
  • enrollment data by university and department
  • declaration
  • type ident or type ident
  • creation
  • new typeexpr
  • subscripting
  • identexpr
  • length
  • regular vs irregular arrays
  • variable-sized
  • fill upper left corner
  • one counter for each dimension

This form is more consistent with other type
declarations. Hence type ident.
13
Processing 2-Dimensional Arrays
  • random access
  • look-up table
  • sequential access
  • row-major order
  • lexicographic order
  • pattern
  • column-major order
  • pattern
  • regular arrays

14
E.g. Tabulating Enrolments
  • data
  • right-sized array
  • totals
  • readStats
  • row-major processing
  • sumRows
  • row processing
  • sumCols
  • column processing
  • sumAll
  • row-major processing
  • writeStats
  • row-major report generation

15
Arrays vs Sounds
16
(No Transcript)
17
(No Transcript)
18
(No Transcript)
19
(No Transcript)
20
(No Transcript)
21
(No Transcript)
22
Reversing a char array
O L I V E R
R L I V E O
R E I V L O
R E V I L O
23
Palindrome Revisited
private void checkPalindromes ( ) int
button String str // string to be checked as
palindrome String reversed // reversed version
of str while ( true ) button
form.accept() if ( button1 ) break str
form.readString("in") reversed
reverse(str) if ( str.equalsIgnoreCase(reversed
) ) form.writeString("out","This is a
palindrome") else form.writeString("ou
t","This is not a palindrome") //
checkPalindromes
24
Palindrome Revisited.
private String reverse ( String str ) char
theString // string as array of
characters char c int i theString
str.toCharArray() for ( i0
ilttheString.length/2 i ) c
theStringi theStringi theStringtheString
.length-1-i theStringtheString.length-1-i
c return new String(theString) //
reverse
25
(No Transcript)
26
Universities
Departments
27
(No Transcript)
28
(No Transcript)
29
Enrollments
public class UStats private final String
UNIVS " Adams"," Beacon"," Madison","
Lexington" private final String DEPTS
"Math.","Business","Comp. Sci.","Biology","French
" private ASCIIDataFile dataFile // file
for input private ASCIIReportFile report //
file for report private ASCIIDisplayer msg //
displayer for messages public UStats ( )
dataFile new ASCIIDataFile() report new
ASCIIReportFile() msg new ASCIIDisplayer()
display() dataFile.close() report.close()
msg.close() // constructor private
void display ( ) int enrol //
enrolment stats int uTotals // University
totals int dTotals // Department
totals int total // grand total msg.writ
eLabel("Processing.....") enrol new
intDEPTS.lengthUNIVS.length readStats(enrol
) uTotals sumRows(enrol) dTotals
sumCols(enrol) total sumAll(enrol) writeSt
ats(enrol,uTotals,dTotals,total) msg.writeLabel
(".....complete") msg.writeEOL() //
display
30
Enrollments.
private void readStats ( int stats )
int i, j for ( i0 iltstats.length
i ) for ( j0 jltstatsi.length j )
statsij dataFile.readInt()
// readStats / This method sums the
enrollments by row./ private int sumRows (
int stats ) int sums int i,
j sums new intstats.length for ( i0
iltstats.length i ) sumsi 0 for (
j0 jltstatsi.length j ) sumsi
sumsi statsij return
sums // sumRows
31
Enrollments..
//This method sums the enrollments by
column. private int sumCols ( int stats
) int sums int i, j sums new
intstats0.length for ( j0
jltstats0.length j ) sumsj
0 for ( i0 iltstats.length i )
sumsj sumsj statsij
return sums // sumCols //This method
computes the grand sum of the enrollment
statistics. private int sumAll ( int
stats ) int sum int i, j sum
0 for ( i0 iltstats.length i ) for
( j0 jltstatsi.length j ) sum sum
statsij return sum //
sumAll
32
Enrollments...
//This method computes the grand sum of the
enrollment statistics. private int sumAll (
int stats ) int sum int i, j sum
0 for ( i0 iltstats.length i )
for ( j0 jltstatsi.length j )
sum sum statsij return
sum // sumAll Alternate Form of
sumAll private int sumAll ( int stats )
int sum int i, j sum 0 for ( int
u stats ) for ( int d u ) sum
sum d return sum // sumAll
Can be read as For each array slice u in stats,
and For each element d in u.
Important to realise that each time the loop
starts u and d represent the next value.
There are no indices, thus they do not need to be
updated. No i or j
33
Enrollments.
/ This method initializes the enrolment
report. /
private void initReport ( )
report.setTitle("Enrolment Report",getDateInstance
().format(new Date()))
report.addField("dept","",10) for ( int
i0 iltUNIVS.length i )
report.addField("u"i,UNIVSi,getIntegerInstance(
),10) report.addField("total",
" Total",getIntegerInstance(),10)
// initReport
34
Enrollments..
/This method displays the enrollment stats in a
tabular format with labels and totals for each
row and column and a grand total. / private
void writeStats( int stats, int rSums, int
cSums, int sum ) int i, j for ( i0
iltstats.length i ) report.writeString(DEP
TSi) for ( j0 jltstatsi.length j )
report.writeInt(statsij) repor
t.writeInt(rSumsi) report.writeString("T
otal") for ( j0 jltcSums.length j )
report.writeInt(cSumsj) report.writ
eInt(sum,10) // writeStats
35
What is a Palindrome?
  • A string which reads the same forward and
    reverse.
  • E.g.
  • This is not a palindrome
  • Happy like a fox
  • These are all palindromes
  • No devil lived on
  • I did did I
  • Plum was I ere I saw mulp
  • Sleep on no peels
  • Zeus sees Suez

36
Array Declarations
37
Good Luck on your Exam!!
Write a Comment
User Comments (0)
About PowerShow.com