Now that we have table counting, the natural thing to want to do is count multiple things to compare them.
Count multiple things in the loop
Have multiple counters:
count1 = 0; // boy counter count2 = 0; // girl counter
Series of if-statements inside the loop (our official form)
--x = x + 1; -- within if-statement, correct variable
--Note the if-statements are not nested (more complex)
After the loop, print both counters
Alternative: could use more mnemonic variable names, like countBoy and countGirl
Do more boy or girl names end with "y"? We want a program which by the end prints "girl count:nn" and "boy count:nn", counting whatever characteristic we are looking for. This is beginning to look like an actual, practical use of computers to sift through data.
One approach is to use two (or more) counters, one for each case we want to count. Here we'll have one boy counter (count1) and one girl counter (count2). Initialize both counters to 0 before the loop. Then in the loop, have two if statements, one for each case we want to count. Inside each if-statement, increment the appropriate counter. At the end of the loop, print the counters.
It's possible to write the above code in different ways, but we will use that simple pattern: one if-statement for each case we want to count, and the if-statements are all in the loop, one after the other.
Names ending in a, i o?
Code example with 3 counters.
Class Survey
As another example of a table, we have the class survey of data from the live class at Stanford with people's favorite movies and what have you. The questions for this survey were:
Gender: male/female
What is your favorite color?
What is your favorite current TV show?
What is your favorite current movie?
What is your favorite sport to play?
What is your favorite current book?
What is your favorite soda to drink?
The survey answers are automatically translated to a google spreadsheet which can be exported in csv table. This data is available in the file "survey-2012.csv". This also illustrates that .csv format's role as an interchange format between systems.
The field names in the table are: gender, color, tv, movie, sport, book, soda. The convertToLowerCase() function of the table changes all the text the table contains to lower case. This simplifies our logic, so we don't have to worry if someone typed in "Blue" or "blue" .. in the table it will always be the lowercase "blue" form. For consistency in the data, I also removed all periods, so soda is "Dr Pepper" not "Dr. Pepper", and I changed the various spellings "coca-cola" to just "coke".
Survey Code Example Problems
These can all be written using our "loop containing series of if-statements" form.
1. Write code to just print the soda field of each row, so we can get a feel for what people typed in.
2. Count soda favorites: sprite, dr pepper, coke
3. Variant on above: use || to lump together "dr pepper" and "diet dr pepper" for each of the three drinks above.
4. Count sports: football and soccer
5. Variant on above: change the code to only count soccer answers, however keep separate counts for gender male and female.
Table code solutions:
// 1. just print the soda field
table = new SimpleTable("survey-2012.csv");
table.convertToLowerCase();
for (row: table) {
print(row.getField("soda"));
}