Unit 4 · 10 min
4.2 Introduction to Using Data Sets
Every data set question is answered the same way: visit each value once and update a running result.
Summary, objectives and course and exam description (CED) codes
A data set is a collection of specific pieces of information, or data. You manipulate and analyze a data set to solve a problem or answer a question by accessing its values one at a time and processing each one. A table or chart of the data helps you plan that algorithm before you write any code.
By the end of this session you can
- 4.2.A Represent patterns and algorithms that involve data sets found in everyday life, using written language or diagrams.
Reviewed against the CED on
1LearnThe notes, the traps and how to remember them
1. What a data set is
A data set is a collection of specific pieces of information, or data 4.2.A.1. You meet data sets everywhere:
| Everyday data set | The pieces of data |
|---|---|
| A week of temperatures | 18.5, 21.0, 19.5, … |
| A class’s quiz scores | 7, 9, 10, 6, … |
| A playlist | song titles |
| A step counter | steps per day |
2. Processing values one at a time
You can manipulate and analyze a data set to solve a problem or answer a question 4.2.A.2. The key pattern: the values are accessed and used one at a time, and each one is processed according to the outcome you want 4.2.A.2.
Problem: “On how many days this week did a runner run more than 5 km?” Written in plain language, the algorithm is:
- Set a counter to 0.
- Look at the next day’s distance.
- If that distance is greater than 5, add 1 to the counter.
- Repeat steps 2 and 3 until every day has been looked at.
- The counter is the answer.
This “visit each value and update a running result” pattern is called a traversal in code, and it is the heart of the rest of Unit 4.
3. Plan with a table or chart
Data can be represented in a diagram such as a chart or table, and that visual can be used to plan the algorithm 4.2.A.3. Here is the runner’s week as a table, with an extra row that traces the counter after each value is processed:
| Day | Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|---|---|---|---|---|---|---|---|
| Distance (km) | 4.0 | 6.5 | 5.0 | 8.0 | 3.5 | 10.0 | 5.5 |
| Greater than 5? | no | yes | no | yes | no | yes | yes |
| Counter after this day | 0 | 1 | 1 | 2 | 2 | 3 | 4 |
The trace shows the answer is 4, and it also catches a common trap: 5.0 is not greater than 5, so Wednesday does not count.
A chart works too. A bar chart of the distances with a line drawn at 5 km lets you count the bars above the line, which is exactly what the algorithm does one bar at a time.
4. From plan to code (preview)
Once the table works on paper, each written step becomes a line of code. Topic 4.3 shows how to store the data in an array; this preview shows how closely the code follows the plan.
double[] distances = {4.0, 6.5, 5.0, 8.0, 3.5, 10.0, 5.5};
int count = 0; // step 1
for (double d : distances) // steps 2 and 4: each value, one at a time
{
if (d > 5) // step 3
{
count++;
}
}
System.out.println("Days over 5 km: " + count); // step 5
Output:
Days over 5 km: 4
Watch out: the trace table is also your test. If the code prints something different from the last cell of your trace, either the plan or the code has a mistake.
Lock it in: memory hooks
- Processing a data setOne at a time: look, decide, update, move on.
- PlanningTable first, code second: if you can trace it on paper, you can code it.
Common misconceptions
"A program looks at a whole data set at once."
Not quite. An algorithm accesses the values one at a time, processes each one (compare, add, count), and keeps a running result.
"Planning with a table is a waste of time; just start coding."
Not quite. A table of the data and a trace of the running result show the exact steps the code needs, including the starting value, which is where most bugs hide.
"A data set has to be numbers."
Not quite. A data set is any collection of specific pieces of information: names, words, true or false answers, temperatures or scores.
Exam traps
- When you plan a running result, choose the starting value carefully: 0 for a sum or count, the first value (not 0) for a maximum or minimum.
- Read what the question wants returned: a count, a total, a single value or a true or false answer.
2PracticeRecall it, then check yourself
In 30 seconds
- A data set is a collection of specific pieces of information or data.
- Data sets are manipulated and analyzed to solve a problem or answer a question.
- Values in a data set are accessed and used one at a time, then processed toward the outcome you want.
- A chart or table of the data is a diagram you can use to plan the algorithm.
Cover this list, say the points out loud, then take the checkpoint without scrolling back up.
Checkpoint · 5 questions
Checkpoint: Introduction to Using Data Sets
Pick an answer to mark it. Every answer is explained, and you can try any question again. Your saved score counts first tries only, and stays in this browser.
-
Data set Position 1 2 3 4 5 Value 3 8 5 6 10 -
Trace table Step 1 2 3 4 5 Value 7 12 4 15 9 Largest so far 7 12 ? ? ? -
Quiz scores Student A B C D E F Score 9 7 8 10 6 8
3PrevailLock it in for exam day
By the numbers
- Steps applied to every value in a data set three steps CED 4.2.A.2
- access the next value
- use itcompare, add, test
- update the running result
- Diagrams that represent data for planning two diagrams CED 4.2.A.3
- a tablerows and columns of values
- a charta picture of the values, such as a bar chart
Key terms: flip the cards
Read the term, say the definition out loud, then flip to check. Struggling a little to remember is what makes it stick.
-
Data settap to flip
A collection of specific pieces of information or data, such as a week of temperatures or a list of scores.
Example and CED codes -
Traversaltap to flip
Using repetition (or recursion) to access all elements of an array or ArrayList, or an ordered sequence of them.
Example and CED codes -
Algorithmtap to flip
A step-by-step process for completing a task or solving a problem, written in words or drawn as a diagram such as a flowchart.
Example and CED codes
Questions students ask
What is a data set in AP® Computer Science A?
A data set is a collection of specific pieces of information or data, such as daily temperatures, test scores or a list of names.
How do you plan an algorithm for a data set?
Write the data in a table, describe in words what happens to each value, and trace a running result through the table. Then turn each written step into code.
Do I need arrays for topic 4.2?
No. Topic 4.2 is about describing patterns and algorithms in words or diagrams. Topic 4.3 shows how to store a data set in an array.