Tallying
Tallying
Chapter 7
Lecture 7-3: Arrays for Tallying; Text Processing
age 20 cats 3
index 0 1 2
grades
value 89 78 93
5
Copyright 2010 by Pearson Education
A multi-counter problem
Problem: Write a method mostFrequentDigit that returns
the digit value that occurs most frequently in a number.
6
Copyright 2010 by Pearson Education
A multi-counter problem
We could declare 10 counter variables ...
int counter0, counter1, counter2, counter3, counter4,
counter5, counter6, counter7, counter8, counter9;
inde 0 1 2 3 4 5 6 7 8 9
x
valu 1 0 2 0 0 0 4 1 0 0
e
inde 0 1 2 3 4 5 6 7 8 9
x
valu 1 0 2 0 0 0 4 1 0 0
e
8
Copyright 2010 by Pearson Education
Tally solution
// Returns the digit value that occurs most frequently in n.
// Breaks ties by choosing the smaller value.
public static int mostFrequentDigit(int n) {
int[] counts = new int[10];
while (n > 0) {
int digit = n % 10; // pluck off a digit and tally it
counts[digit]++;
n = n / 10;
}
// find the most frequently occurring digit
int bestIndex = 0;
for (int i = 1; i < counts.length; i++) {
if (counts[i] > counts[bestIndex]) {
bestIndex = i;
}
}
return bestIndex;
}
9
Copyright 2010 by Pearson Education
Section attendance question
Read a file of section attendance (see next slide):
yynyyynayayynyyyayanyyyaynayyayyanayyyanyayna
ayyanyyyyayanaayyanayyyananayayaynyayayynynya
yyayaynyyayyanynnyyyayyanayaynannnyyayyayayny
And produce the following output:
Section 1
Student points: [20, 16, 17, 14, 11]
Student grades: [100.0, 80.0, 85.0, 70.0, 55.0]
Section 2
Student points: [16, 19, 14, 14, 8]
Student grades: [80.0, 95.0, 70.0, 70.0, 40.0]
Section 3
Student points: [16, 15, 16, 18, 14]
Student grades: [80.0, 75.0, 80.0, 90.0, 70.0]
• Students earn 3 points for each section attended up to 20.
10
Copyright 2010 by Pearson Education
Section input file
student 123451234512345123451234512345123451234512345
week 1 2 3 4 5 6 7 8 9
section 1 yynyyynayayynyyyayanyyyaynayyayyanayyyanyayna
section 2 ayyanyyyyayanaayyanayyyananayayaynyayayynynya
section 3 yyayaynyyayyanynnyyyayyanayaynannnyyayyayayny
11
Copyright 2010 by Pearson Education
Section attendance answer
import java.io.*;
import java.util.*;
public class Sections {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("sections.txt"));
int section = 1;
while (input.hasNextLine()) {
String line = input.nextLine(); // process one section
int[] points = new int[5];
for (int i = 0; i < line.length(); i++) {
int student = i % 5;
int earned = 0;
if (line.charAt(i) == 'y') { // c == 'y' or 'n' or 'a'
earned = 3;
} else if (line.charAt(i) == 'n') {
earned = 1;
}
points[student] = Math.min(20, points[student] + earned);
}
double[] grades = new double[5];
for (int i = 0; i < points.length; i++) {
grades[i] = 100.0 * points[i] / 20.0;
}
System.out.println("Section " + section);
System.out.println("Student points: " + Arrays.toString(points));
System.out.println("Student grades: " + Arrays.toString(grades));
System.out.println();
section++;
}
}
} 12
Copyright 2010 by Pearson Education
Data transformations
In many problems we transform data between forms.
Example: digits count of each digit most frequent digit
Often each transformation is computed/stored as an array.
For structure, a transformation is often put in its own method.
15
Copyright 2010 by Pearson Education