



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CMPU-101: Problem-Solving and Abstraction Marc Smith Vassar College Spring 2007 2
Our programs have worked with sequentially processing a fixed number of items: E.g., Read and print the first three lines in a file. Sum the first five numbers in a file. 3
Programs work with a variable number of items: Read & print all the lines in a file. Read, print & sum all the numbers in a file. The number of iterations depends on the data. 4
for — when we know in advance how many times to loop while — when we don't Looping is often done with collections of data.
Where stored? Disk vs memory. Homogeneous vs heterogeneous. Duplicates allowed? Is order of elements important? Access? Random vs just from the front. These collections are “data structures”, the subject of CMPU 102
string file array vector linked list set tree, heap, hash table, … We will see all of these in 101
7
Looping allows us to answer questions such as... Given a file of ints, how many are negative? We can vary the collection, but the pattern remains the same … 8
Given a file of ints, how many are negative? Given an array of ints, how many are negative? Given a set of ints, how many are negative? Given a tree of ints, how many are negative? 9
Given a file of ints, how many are negative? Given a file of ints, how many are positive? Given a file of Strings, how many are the empty String? Given a file of Strings, how many are purely upper case? Given a file of Complex, how many are 0? Given a file of Students, how many got As? Given a file of Students, how many failed? 10
All variants of a pattern use the same algorithm: Loop until done with data.
Not random access. Read from top to bottom. (from first line to last line) Contain Strings, that we can convert to objects or read as ints — using nextInt()
cond? stmt false true Normally, we intend that cond becomes false eventually, causing execution to continue with the code after the loop
19
Given a file of ints, compute the product. int product = 1; while ( sc.hasNext() ) { int n = sc.nextInt(); product *= n; } 20 Accumulating Particular Items Given a file of ints, compute the sum of the odd ints. int sum = 0; while ( sc.hasNext() ) { int n = sc.nextInt(); if ( n%2 != 0 ) { sum += n; } } 21
Given a file of ints, compute both the sum and the product. int sum = 0; int product = 1; while ( sc.hasNext() ) { int n = sc.nextInt(); sum += n; product *= n; } 22
int sumE = 0; // the sum of the even ints int sumO = 0; // the sum of the odd ints while ( sc.hasNext() ) { int n = sc.nextInt(); if ( n%2 == 0 ) { sumE += n; } else { sumO += n; } } This solves: Given a file of ints, compute the sum of the even ints and the sum of the odd ints.
int count = 0; int sum = 0; while ( sc.hasNext() ) { int n = sc.nextInt(); count++; sum += n; } System.out.println("The average is " + 1.0*sum/count); Note how the average is computed without using integer division, multiplying by 1. (same as casting as a double)
Scanner sc = new Scanner(new File(filename)); int count = 0; while ( sc.hasNext() ) { int n = sc.nextInt(); count++; } sc.close();
25
sc = new Scanner(new File(filename)); int sum = 0; while ( sc.hasNext() ) { int n = sc.nextInt(); sum += n; } System.out.println("The average is " + 1.0*sum/count); Closing the file before creating a new file Scanner resets the Scanner to the top line of the file. 26
…as in a course section at college. -- A collection of Students. In this chapter, it’s implemented as a file of Students.
In Chapter 14, we'll change the implementation — to an array of Students — but we won't change the interface.