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.

4.2.A4.2.A.14.2.A.24.2.A.3

Reviewed against the CED on

Know this already? Jump to the checkpoint.

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:

  1. Set a counter to 0.
  2. Look at the next day’s distance.
  3. If that distance is greater than 5, add 1 to the counter.
  4. Repeat steps 2 and 3 until every day has been looked at.
  5. 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

  1. A data set is a collection of specific pieces of information or data.
  2. Data sets are manipulated and analyzed to solve a problem or answer a question.
  3. Values in a data set are accessed and used one at a time, then processed toward the outcome you want.
  4. 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.

  1. 1 Which of the following is best described as a data set? 4.2.A.1
    Show answer

    Answer: B. A data set is a collection of specific pieces of information or data, such as many finishing times. One variable holds a single value, not a collection; a rule and a program are ways to process data, not the data itself.

  2. Data set
    Position12345
    Value385610
    2 The following algorithm is applied to the data set shown. "Set total to 0. Look at each value one at a time. If the value is even, add it to total." What is the value of total after every value has been processed? 4.2.A.2
    Show answer

    Answer: C. The values are processed one at a time; the even values are 8, 6 and 10, so total is 8 + 6 + 10 = 24. 32 is the sum of all values, 3 is how many values are even, and 14 stops before the last value (8 + 6).

  3. Trace table
    Step12345
    Value7124159
    Largest so far712???
    3 A student plans an algorithm that keeps the largest value seen so far, using the trace table shown. Which of the following correctly completes the last three cells of the "Largest so far" row? 4.2.A.3
    Show answer

    Answer: D. The largest so far only changes when a bigger value appears: after 4 it stays 12, after 15 it becomes 15, and after 9 it stays 15. "4, 15, 9" and "12, 15, 9" copy the current value instead of keeping the largest, and "7, 15, 15" goes back to the first value.

  4. 4 A program must decide whether any reading in a week of temperatures was below 0. Which of the following written algorithms is correct? 4.2.A.2
    Show answer

    Answer: A. The values must be accessed one at a time, and one reading below 0 is enough for "yes", but "no" is only certain after every reading is checked. Checking only the first reading ignores the rest, stopping at the first reading of 0 or more answers too early, and a total can be positive even when one day was below 0.

  5. Quiz scores
    StudentABCDEF
    Score9781068
    5 The quiz scores of six students are shown in the table. An algorithm counts how many scores are at least 8 by checking each score one at a time. What is the final count? 4.2.A.2, 4.2.A.3
    Show answer

    Answer: B. The scores that are at least 8 are 9, 8, 10 and 8, so the count is 4. 3 counts only one of the two 8s, 5 also counts a score below 8, and 2 counts only the scores greater than 8 (treating "at least" as "greater than").

3PrevailLock it in for exam day

By the numbers

Name each list from memory, then reveal it to check. Each list shows how many items to recall.

  • Steps applied to every value in a data set three steps CED 4.2.A.2
    1. access the next value
    2. use itcompare, add, test
    3. update the running result
  • Diagrams that represent data for planning two diagrams CED 4.2.A.3
    1. a tablerows and columns of values
    2. 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.

Was this page helpful?

No names are ever shown, and every public comment is checked by us first. Please do not include personal details. To get a reply, email [email protected].

↑ Top

Enlarged view of the selected figure