What is a data set?
A data set is a collection of specific pieces of information. A week of daily rainfall readings is a data set. So is a list of quiz marks, a table of customers and what they spent, or the high scores in a game. Data sets are everywhere, and a great deal of programming consists of manipulating and analysing them to solve a problem or answer a question: how many days were wet? Who scored highest? What was the total?
Topic 4.1 asked whether a data set is the right one to use. This topic asks how to think about processing one, and how to represent the pattern or algorithm on paper before writing any Java. The Java for arrays comes in Topics 4.3 to 4.5; here we concentrate on the thinking that makes that code easy.
Values are used one at a time
When a program analyses a data set, the values within the set are accessed and used one at a time and then processed according to the desired outcome. Even if you can see a whole table at a glance, a program cannot: it reads one value, does something, then moves to the next. That is why nearly every plan for a data set has the same three-part shape:
- Set up anything you must remember between values: a counter, a running total, the best value so far.
- Repeat for each value: look at it and update what you remember.
- Report the result once every value has been seen.
Representing a data set and a plan
Data can be shown in a diagram as a chart or table. This visual can also be used to plan the algorithm that will be used to manipulate the data. Here is a fictional week of rainfall.
| Day | Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|---|---|---|---|---|---|---|---|
| Rainfall (mm) | 0 | 12 | 5 | 20 | 0 | 8 | 15 |
Suppose the question is “how many days had at least 10 mm of rain?” In written language, the plan is:
- Set
wetDaysto 0. - For each day’s reading, from Monday to Sunday: if the reading is 10 or more, add 1 to
wetDays. - Report
wetDays.
The same plan as a flowchart:
Writing the plan in two forms is not repetition: the flowchart makes the loop and the choice visible, and the written steps make you decide the exact test and what to report.
Worked example 1: planning, tracing and then coding a count
Task. Plan and trace “number of days with at least 10 mm of rain” on the rainfall table.
Step 1: choose what to remember. One thing: the count of wet days so far, starting at 0.
Step 2: trace the plan with a table. Each row is one value being processed.
| Day | Reading | At least 10? | wetDays after |
|---|---|---|---|
| (start) | 0 | ||
| Mon | 0 | no | 0 |
| Tue | 12 | yes | 1 |
| Wed | 5 | no | 1 |
| Thu | 20 | yes | 2 |
| Fri | 0 | no | 2 |
| Sat | 8 | no | 2 |
| Sun | 15 | yes | 3 |
Step 3: report. wetDays is 3.
Step 4: check against the data. Scanning the table by eye, the readings that are 10 or more are 12, 20 and 15, three of them. The trace and the scan agree.
Step 5: preview the code. Once you know arrays (Topic 4.3), the plan turns almost word for word into Java. You do not need to understand every symbol yet; notice how each line of code matches a line of the plan.
int[] mm = {0, 12, 5, 20, 0, 8, 15};
int wetDays = 0;
for (int i = 0; i < mm.length; i++)
{
if (mm[i] >= 10)
{
wetDays++;
}
}
System.out.println(wetDays);
The program printed 3, matching the trace.
Check. A trace table forces you to process the values one at a time, which is exactly how the program will behave.
Worked example 2: planning the largest value, and finding a flaw
Task. Plan “which day had the most rain, and how much?” Then examine a plan with a flaw.
Step 1: what to remember. The best value so far and where it was (its position). Start with the first reading as the best, not with 0: starting at 0 would fail if every reading could be negative (for a temperature, say), and starting from the first value always works.
Step 2: the plan.
- Let
bestbe the first position. - For each later position: if its reading is greater than the reading at
best, letbestbecome that position. - Report the day name and the reading at
best.
Step 3: trace. (Readings 0, 12, 5, 20, 0, 8, 15 on Mon to Sun.)
| Position examined | Reading | Compare with best reading | best after |
|---|---|---|---|
| start | Mon (0) | ||
| Tue | 12 | 12 > 0, so yes | Tue (12) |
| Wed | 5 | 5 > 12, no | Tue (12) |
| Thu | 20 | 20 > 12, yes | Thu (20) |
| Fri | 0 | no | Thu (20) |
| Sat | 8 | no | Thu (20) |
| Sun | 15 | 15 > 20, no | Thu (20) |
Step 4: report. Thursday, 20 mm. The code version:
String[] days = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
int bestIndex = 0;
for (int i = 1; i < mm.length; i++)
{
if (mm[i] > mm[bestIndex])
{
bestIndex = i;
}
}
System.out.println(days[bestIndex] + " " + mm[bestIndex]);
It printed Thu 20.
Step 5: a flawed plan. A student writes: “For each reading: set count to 0; if the reading is 10 or more, add 1 to count; at the end report count.” Trace it:
| Day | Reading | count after the reset |
count after the test |
|---|---|---|---|
| Mon | 0 | 0 | 0 |
| Tue | 12 | 0 | 1 |
| Wed | 5 | 0 | 0 |
| Thu | 20 | 0 | 1 |
| Fri | 0 | 0 | 0 |
| Sat | 8 | 0 | 0 |
| Sun | 15 | 0 | 1 |
On every pass the count goes back to 0, so the final value depends only on the last reading. Since Sunday’s reading is 15, the program reports 1, not 3. The coded version of the flaw:
int wrong = 0;
for (int i = 0; i < mm.length; i++)
{
wrong = 0; // reset on every pass: a flaw in the plan
if (mm[i] >= 10)
{
wrong++;
}
}
System.out.println(wrong);
printed 1. The set-up step belongs before the repetition; only the update belongs inside it.
Check. A plan can look right and still be wrong; tracing it on a small data set with known answer (3 here) exposes the flaw straight away.
Common misconceptions
- “A data set is a program feature.” It is just information. A program can store it in many ways.
- “The program can see the whole table at once.” It processes values one at a time.
- “Plans do not need to be traced.” A trace table is the cheapest test you have.
- “The count can be set to 0 inside the loop.” Set-up steps go before the loop, or the count is lost every pass.
- “Start the maximum at 0.” That is wrong for data that can be below 0; start from the first value.
- “Flowcharts and written steps are interchangeable with no loss.” Either is fine, but whichever you choose must show the loop, the choice and the final report.
Where this leads
Next, Array Creation and Access gives you the Java structure for holding a data set, and Array Traversals and Implementing Array Algorithms turn plans like these into code. First practise with the practice questions, read the revision notes and tick the topic checklist. The ethical questions that precede any data work are in Ethical and Social Issues Around Data Collection.
Quick questions
Is a data set the same as an array?
No. A data set is the information itself, such as a week of rainfall readings. An array is one way a Java program can store a data set. You plan with the data set and implement with arrays or other collections (Topic 4.3 onwards).
Do I have to draw flowcharts?
No particular diagram is required. The course asks you to be able to represent patterns and algorithms in written language or diagrams; use whichever makes your plan clear, as long as it shows each step and the order of repetition and selection.
Why plan before coding?
A plan shows what you need to remember between values (a count, a total, the best so far), what is done to each value and what is reported at the end. Problems are far cheaper to fix on paper than in finished code.
What does processing values one at a time mean?
The algorithm looks at the first value, does something with it, then the second, and so on, rather than looking at the whole collection at once. A loop is the usual way to express this.