Docsity

Loop and Files - Computer Science I - Fall 2007 | CMPU 101, Study notes of Computer Science

Toggle sidebar
bg1
Chapter 13
Loops & Files
CMPU-101: Problem-Solving and Abstraction
Marc Smith
Vassar College
Spring 2007
2
Previously
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
Chapter 13
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
Loops
for — when we know in advance how many times to loop
while — when we don't
Looping is often done with collections of data.
5
Characteristics of Collections
Where stored? Disk vs memory.
Homogeneous vs heterogeneous.
Duplicates allowed?
Is order of elements important?
Access? Random vs just from the front.
6
These collections are “data structures”,
the subject of CMPU 102
Some Collections
string
file
array
vector
linked list
set
tree, heap, hash table, …
We will see all of these in 101
pf3
pf4
pf5

Partial preview of the text

Download Loop and Files - Computer Science I - Fall 2007 | CMPU 101 and more Study notes Computer Science in PDF only on Docsity!

Chapter 13

Loops & Files

CMPU-101: Problem-Solving and Abstraction Marc Smith Vassar College Spring 2007 2

Previously

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

Chapter 13

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

Loops

for — when we know in advance how many times to loop while — when we don't Looping is often done with collections of data.

Characteristics of Collections

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

Some Collections

string file array vector linked list set tree, heap, hash table, … We will see all of these in 101

7

Pattern

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

Vary the Type of the Collection

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

Vary the Datatype and/or Criterion

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

Algorithm

All variants of a pattern use the same algorithm: Loop until done with data.

Files as Collections

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()

while ( cond ) stmt

cond? stmt false true Normally, we intend that cond becomes false eventually, causing execution to continue with the code after the loop

19

Prog 1306

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

Prog 1307

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

Prog 1308

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.

Prog 1309

Average of a File of 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)

Prog 1310

Average — Reading the File Twice

Scanner sc = new Scanner(new File(filename)); int count = 0; while ( sc.hasNext() ) { int n = sc.nextInt(); count++; } sc.close();

25

Prog 1310

Average (cont.) — Reading the File Again

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

Section

…as in a course section at college. -- A collection of Students. In this chapter, it’s implemented as a file of Students.

Ancient Geek Proverb:

“Change the implementation,

but don’t change the interface.”

In Chapter 14, we'll change the implementation — to an array of Students — but we won't change the interface.