Title: Oct 67
1Oct 6-7
- loops revisited
- I/O
- arrays
2Loops 1
Write a program to calculate and print the
average of all the multiples of 3 from 3 up to
(and including) 33333. Invariant s contains 3
6 9 ... i and i is a multiple
of 3 c is the number of values
that have been summed in
s Initalization i 3 s 3 c 1 Stopping
condition when i 33333 So, continue while i
! 33333 Body make progress i i3
maintain invariant s si c c1
3public static void main (String args)
int i 3 int s 3 int c 1 //
invariant ... while (i ! 33333)
i i3 s si c c1
System.out.println("average " s/c)
4Loops 2
Read in a sequence of positive integers (followed
by a 0) and print the biggest together with its
frequency of occurrence. Invariant x the
last number read max the largest
value read in so far freq the
number of times max has appeared
5Basic input class
import java.io. public static void main
(String args) int a // initialize
TokenReader object in to read // from
standard input. TokenReader in new
TokenReader(System.in) // Read one number
into a System.out.print("Please enter an
integer ") System.out.flush( )
a in.readInt( ) System.out.println(a
a)
6Some input methods
// read and return a value of type int public
int readInt( ) // read and return a value of type
double public double readDouble( ) // read and
return a value of class String public String
readString( ) // Skip the rest of the input line
and return as a string // all the characters on
the next line public String readLine( )
7import java.io. public static void main
(String args) // initialize TokenReader
object in to read from standard input.
TokenReader in new TokenReader(System.in)
int max 0 //maximum integer read in so far
int freq 0 //frequency of max // read in
integers until a 0 int x in.readInt()
while (x ! 0) if (x gt max) //x is a
new max value max x freq 1
else if (x max) //x is the same as the current
max value freq freq 1 x
in.readInt() //read in next value from standard
input
8Array Rules
- When an array is allocated
- int A new intsize
- size may be any integer expression.
- When using Aexp to refer to an element, the
subscript exp may be any integer expression. - The elements in an array are numbered A0, A1,
, Asize-1, where size is the number of
elements in the array. - In a reference to an array element Aexp, it is
an error if explt0 or expsize. - Be sure to be careful to distinguish the
subscript (or location) of an array element from
the data that is actually stored in that location.
9Arrays 1
- Write a program that takes an array x of
double's (assume that it is already initialized)
and create a new array s of the same length such
that si is the average of xi and its circular
neighbors - For j in the range 0 lt j lt x.length-1, we say
xj1 is the neighbor to the right of xj. - For j in the range 0 lt j lt x.length-1, we say
xj-1 is the neighbor to the left of xj. - We say x0 is the neighbor to the right of
xx.length-1, and xx.length-1 is the neighbor
to the right of x0. (This is the circular part
of "circular neighbors" think of joining the
ends of x together, forming a circle, making
the original ends into neighbors.) - For example, here are some values for x and the
corresponding outcomes s - x --gt s
- x 2 --gt s 2 since (222)/3 2
- x 0 9 --gt s 6 3 since (909)/3 6
and (090)/3 3
10 //code here initializes x int n
x.length double s new doublen int
i 0 // invariant 0 lt i lt n x.length,
// s0..i-1 has been computed, // and
si..n-1 remains to be done while (i lt n)
int left i-1 int right i1
if (left lt 0)
left n-1
if (right n)
right 0
si (xleft
xi xright) / 3.0 i i1
...
11 // code here initializes x int n
x.length() double s new doublen
int i 0 // invariant 0 lt i lt n
x.length, // s0..i-1 has been computed,
// and si..n-1 remains to be done while
(i lt n) si (x(i-1n) n xi
x(i1) n) / 3.0 i i1
This solution uses the remainder operation to
wrap values. We use (i-1n) n instead of just
(i-1) n to be safe in Java, can/will return
negative values if given negative dividends.
12Arrays 2
// Move top card k0 down into position j, //
thereby pushing cards k1..j into positions
0..j-1 public static void cycleTopDown(int k,
int j) int top k0 int i 0
// invariant 0 lt i lt j, and k1..i has been
moved to k0..i-1 while (i lt j)
ki ki1 i i1 kj
top // Create an array k and // repeatedly
cycle k0 down into position k0 until k0 is
0 public static void topCycles () int k
2, 4, 3, 1, 0 // Repeatedly cycle k0
into position k0 until k0 is 0 while
(k0 ! 0) cycleTopDown(k, k0)
13Carefully trace a call to topCycles() a) Show
EVERY step -- ALL of them -- until one iteration
of the loop in topCycles has completed. Remember
to trace execution of the statements within
method calls. You will have to draw diagrams
showing the state of affairs at about 10 places.
Do not worry about where to place a frame for a
method-call. Place all frames outside of all
other boxes. b) Show us the contents of k
after each iteration of the loop in topCycles
(there are at most 7 iterations).
14Solution (w/o the trace)
Here are the contents of k after each iteration
of the loop in topCycles (we include the contents
before entering the loop) 0 1 2 3 4 lt-
positions - - - - - 2 4 3 1 0 lt-
contents 4 3 2 1 0 3 2 1 0 4 2 1 0 3
4 1 0 2 3 4 0 1 2 3 4