The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by INTERTU℠ EDUCATION, 2022-08-19 15:41:13

AP Computer Science Principles

AP Computer Science Principles

Programs can be developed for creative expression, to satisfy personal
curiosity, to create new knowledge, or to solve problems (to help people,
organizations, or society).

Advances in computing have generated and increased creativity in other
fields. For example, new computer tools are being used to track dolphins and
to decode their vocalization. Understanding complex dolphin behavior would
not have been possible without the ability to data mine the acoustic dolphin
database (searching through large databases of information to pinpoint
relevant data).

Sometimes programs that are developed for personal use can be adapted to
serve a larger audience and purpose. In 2004, Mark Zuckerberg created
Facebook, a local platform for Harvard students to use to connect with each
other. Before adapting his program to apply to a larger audience, it totaled
about 1 million users by the end of 2004. Only eight years later, that number
had increased to over a billion active users. This widespread distribution was
made possible by changes to the program in its development.

Programs Can Be Flexible

When it was targeted to a smaller, local audience simply to satisfy
Zuckerberg’s personal curiosity and enjoyment, Facebook was held to much
different standards in its development. When it was made for billions of
people to use, changes had to be made to accommodate so many people and
to target it toward a wide market of people that might use it—making it
appealing to a wider audience.

The technologies and programs that are applied to Facebook could also be
applied to other fields. As well as inspiring the age of social media, the data-
mining programs used to understand and process the data that is “harvested”
from Facebook is the same type of program that allows researchers to make
sense of data gathered from the acoustic dolphin database.

Programming Design Steps

The first step in programming is planning and identifying programmer and
user concerns that can affect the solution. This consultation and

communication with program users are important aspects of program
development to solve problems.

When designing a large program an iterative process helps with correctly
coding. Checking for errors in small chunks can make isolating errors more
efficient. Once a small chunk of the program is error free, it can be combined
with already checked programs to help create a larger correct program. This
process of designing, implementing, testing, debugging, and maintaining
programs is repeated until the entire program is completed.

Program Documentation

Program documentation is helpful in all stages of program development.
Documentation does not slow down run speed and is necessary when
collaborating or programming alone. Documentation is useful during initial
program development and when modifications are made.

Flowcharts

A flowchart is a way to visually represent an algorithm. The flowchart below
uses the following building blocks.

Block Explanation
The start or end of the algorithm

Oval One or more processing steps, such
Rectangle as a statement that assigns a value to
a variable
Diamond
A conditional or decision step,
Parallelogram where execution proceeds to the side
labeled true if the condition is true
and to the side labeled false
otherwise

Displays a message

Example One

Step 1: Start the program
Step 2: Set num = 0
Step 3: Is n equal to 0 evaluates to true
Step 4: Display 0
Step 5: End the program

Example Two

Step 1: Start the program
Step 2: Set num = 0
Step 3: Is 0 greater than 2—evaluates to false
Step 4: Set num equal to 0 + 1, which equals 1
Step 5: Is 1 greater than 2—evaluates to false
Step 6: Set num equal to 1 + 1, which equals 2
Step 7: Is 2 greater than 2—evaluates to false
Step 8: Set num equal to 2 + 1, which equals 3
Step 9: Is 3 greater than 2—evaluates to true
Step 10: Display 3
Step 11: End the program

Example Three

Step 1: Puts the following numbers [ 1, 1, 35, 6, 76, 4, 98] into a data
structure called list1
Step 2: The data structure called list2 is set to empty
Step 3: This is a FOR EACH loop which will loop through each item
contained in list1.
Step 4: If the item is both even AND odd it will add the number to the end of
list2.

Item MOD 2 = 0 will evaluate to true if the number is even
Item MOD 2 = 1 will evaluate to true if the number is odd

Step 5: Since a number can never be both even AND odd no numbers will be
appended to the end of list2.
Step 6: Since list2 remains empty there is nothing to DISPLAY

Lists

Lists are an organized and formatted way of storing and retrieving data. Each
element in a list can be accessed by its index.

Example Four

Unlike some common programming languages, indexes start at 1, not 0.
animals[1] = Cow
animals[2] = Pig
animals[3] = Dog
animals[4] = Golden Bandicoot
animals[5] = Frog

Example Five

scores [11, 35, 6, 75, 37]
scores[1] = 11
scores[2] = 35
scores[3] = 6
scores[4] = 75
scores[5] = 37

Example Six

words ← [“The”, “Little”, “Frog”, “Jumping”]
words[2] = “Little”
words[4] = “Jumping”

Example Seven

words ← [“The”, “Little”, “Frog”, “Jumping”]
INSERT (words, 3, “Green”)
words[“The”, “Little”, “Green”, “Frog”, “Jumping”]

Example Eight

words ← [“The”, “Little”, “Frog”, “Jumping”]
INSERT (words, 2, “Green”)
APPEND (words, “Fox”)
words[“The”, “Green”, “Little”, “Frog”, “Jumping”, “Fox”]

Example Nine

words ← [“The”]
INSERT (words, 1, “Green”)
APPEND (words, “Fox”)
APPEND (words, “Pig”)
APPEND (words, “Rhino”)
INSERT (words, 1, “Elephant”)
words[“Elephant”,“Green”, “The”, “Fox”, “Pig”, “Rhino”, “Fox”]

Example Ten

Line 1: words ← [“Elephant”,“Green”, “The”, “Fox”, “Pig”, “Rhino”,
“Fox”]
Line 2: DISPLAY (LENGTH (words) ) // answer 7
Line 3: REMOVE (words, 1)
Line 4: DISPLAY (LENGTH (words) ) // answer 8

Traversing a List

Traversing a list means that you are checking all the elements of the list one
by one.

Procedures

A procedure is a set of code that is referred to by name and can be called
(invoked) at any point in a program simply by utilizing the procedure’s name.
In other languages, a procedure could be called a method or subroutine. In
the Create Performance Task, a procedure is referred to as an abstraction.

Example Fourteen

Example Fifteen

Example Sixteen

Finding Average Text-Based Coding

Line 1: PROCEDURE findAverage (list)
Line 2: {
Line 3: sum ← 0
Line 4: count ← 0
Line 5: n ← LENGTH (list)
Line 6: REPEAT n TIMES
Line 7: {
Line 8: sum ← sum + list(count)
Line 9: count ← count + 1
Line 10: }

Line 11: RETURN (sum / n)
Line 12: }

Lines 3 and 4: Initialize the variables sum and count and set them equal to
zero.
Line 5: Set the variable n equal to the number of items in the list.
Line 6: Repeat the next segment n times (for every item in the list).
Line 8: Add the item in position count from the list to the sum.
Line 9: Increment, count by one (so that the next time you loop through you
will add the item in the next spot in the list).
Line 11: Take the sum of all the items and divide by n, the number of items
in the list.

Example Seventeen

Finding Maximum Number in a Text-Based Coding List

Line 1: PROCEDURE findMaximum (list)
Line 2: {
Line 3: max ← list[1]
Line 4: n ← LENGTH (list)
Line 5: count ← 1
Line 6: REPEAT n TIMES
Line 7: {
Line 8: if( list[count] > max)
Line 9: max = list[count]
Line 10: count ← count + 1
Line 11: }
Line 12: RETURN (max)
Line 13: }

Line 3: Initialize the variable max to the first element in the list.
Line 4: Initialize the variable n to the number of items in the list.
Line 5: Initializes count to 1.
Line 6: Repeats the loop n number of times.
Lines 7–9: If the current value is greater than max then the current value is
set to max.

Line 10: Increases the value of count.
Line 12: Returns the maximum value in the data structure.

Example Eighteen

Finding Minimum Number in a Text-Based Coding List

Line 1: PROCEDURE findMinimum (list)
Line 2: {
Line 3: min ← list[1]
Line 4: n ← LENGTH (list)
Line 5: count ← 1
Line 6: REPEAT n TIMES
Line 7: {
Line 8: if( list[count] < min)
Line 9: min = list[count]
Line 10: count ← count + 1
Line 11: }
Line 12: RETURN (min)
Line 13: }
Line 3: Initialize the variable min to the first element in the list.
Line 4: Initialize the variable n to the number of items in the list.
Line 5: Initializes count to 1.
Line 6: Repeats the loop n number of times.
Lines 7–9: If the current value is greater than min then the current value is set
to min.
Line 10: Increases the value of count.
Line 12: Returns the minimum value in the data structure.

Example Nineteen

Searching for a Word in a Text-Based Coding List

Line 1: PROCEDURE findNumber (list, word)
Line 2: {
Line 3: index ← 1

Line 4: FOR EACH item IN list
Line 5: {
Line 6: if (item = word)
Line 7: {
Line 8: RETURN index
Line 9: }
Line 10: index ← index + 1
Line 11: }
Line 12: RETURN (“Word not in list”)
Line 13: }

Line 3: Initialize the variable index to equal one, since that is where to begin
the search for “word.”
Line 4: This For Each loop will loop for every item in the array. Each
iteration then looks at the next object in the array, calling it item.
Line 6 to Line 9: If the current item is the same as the word being searched
for, it will return that given index.
Line 10: If the word did not match with the item, continue with the program
and increment the index.

Example Twenty

Searching for a Word in a Text-Based Coding Alternate-Solution
List

Line 1: PROCEDURE findNumber (list, word)
Line 2: {
Line 3: index ← 1
Line 4: FOR EACH item IN list
Line 5: {
Line 6: if (item = word)
Line 7: {
Line 8: RETURN index
Line 9: }
Line 10: else
Line 11: {
Line 12: index ← index + 1

Line 13: }
Line 14: }
Line 15: RETURN (“Word not in list”)
Line 16: }

Line 3: Initialize the index equal to 1 because that is the starting point.
Line 4 to Line 7: Make a For Each loop to look at every item in the list.
Line 8: If the item being looked for is the same as the word, it will return the
index currently being looked at.
Line 12: Otherwise, add one to the index for the next iteration.

PROGRAMMING QUESTIONS

DIRECTIONS: Each of the questions or incomplete statements
below is followed by four suggested answers or completions. Select
the one that is best in each case.

1. What will the following program display?

A. 0
B. 4
C. 13
D. 35

2. What will the following program display?

A. 0
B. 12
C. 16
D. 35

3. What will the following program display?

A. [6, 76, 4, 98]
B. [1, 1, 35]
C. [1, 1, 35, 6, 76, 4, 98]
D. [ ]

4. What will the following program display?

Line 1: list ← [11, 35, 6, 2]

Line 2: DISPLAY (mystery (list))

Line 3:

Line 4: PROCEDURE mystery (list)

Line 5: {

Line 6: sum ← list [1]
Line 7: count ← 1
Line 8: n ← LENGTH (list)
Line 9: REPEAT n TIMES
Line 10: {
Line 11: sum ← sum + list (count)
Line 12: count ← count + 1
Line 13: }
Line 14: RETURN (sum / count)
Line 15: }
A. 0
B. 13
C. 16
D. 35

5. What will the following program display?

A. [6, 76, 4, 98]
B. [1, 1, 35]
C. [1, 1, 35, 6, 76, 4, 98]
D. [ ]

6. What will the following program display?

A. [0]
B. [-11]
C. [-35]
D. [ ]

7. What will the following program display?

A. [6, 76, 4, 98]
B. [1, 1, 35]
C. [1, 1, 35, 6, 76, 4, 98]
D. [ ]

8. What will the following program display?

A. 1
B. 98
C. 0
D. 76

9. What will the following program display?

A. 0
B. 98
C. 1
D. -98

10. The following question uses a robot in a grid of squares. The robot is
represented as a triangle, which is initially in the top-left square of the
grid and facing toward the top of the grid.

Code for the procedure Mystery is shown here. Assume that the
parameter p has been assigned a positive integer value (e.g., 1, 2, 3, . . .
).

Which of the following shows a possible result of calling the
procedure?

A.
B.
C.

D.
11. The following question uses a robot in a grid of squares. The robot is

represented as a triangle, which is initially in the top-left square of the
grid and facing toward the top of the grid.

Code for the procedure Mystery is shown below. Assume that the
parameter p has been assigned a positive integer value (e.g., 1, 2, 3, . .
.).

Which of the following shows the result of calling the procedure when
p = 4?

A.

B.

C.
D. Error. The robot will be out of the grid.
12. The following question uses a robot in a grid of squares. The robot is
represented as a triangle, which is initially in the top-left square of the
grid and facing toward the top of the grid.

Code for the procedure Mystery is shown below. Assume that the
parameter p has been assigned a positive integer value (e.g., 1, 2, 3, . . .
).

Which of the following shows the result of calling the procedure when
p = 4?

A.
B.
C.

D.

13. The program below is intended to find the highest non-negative
number in list.

Does the program work as intended? If so, what is the result? If not,
why?
A. Yes, the program works as intended; it displays 1.
B. No, the program does not work as intended; result starts at 0.
C. Yes, the program works as intended; it displays 3.
D. No, the program does not work as intended; the if conditional

should be “item > result.”
14. The following program is intended to find the lowest number in list.

Does the program work as intended? If so, what is the result? If not,
why?
A. Yes, the program works as intended; it displays -7.
B. No, the program does not work as intended; result is always list[1].
C. Yes, the program works as intended; it displays 1.
D. No, the program does not work as intended; runtime error.

15. The following program is intended to find the lowest number in list.

Does the program work as intended? If so, what is the result? If not,
why?
A. Yes, the program works as intended and returns 10.
B. No, the program does not work as intended; while this code will

run, it would return a logical error of the value 0.
C. Yes, the program works as intended and would return the value 1.
D. No, the program does not work as intended and would result in a

runtime error not returning a number.
16. The following program is intended to find the greatest number in list.

Does the program work as intended? If so, what is the result? If not,
why?

A. Yes, the program works as intended, returning -200.
B. No, the program does not work as intended; result never changes

and returns -3.
C. Yes, the program works as intended and returns -1.
D. No, the program does not work as intended; runtime error.

17. The following question uses a robot in a grid of squares. The robot is
represented as a triangle, which is initially in the top-left square of the
grid and facing toward the top of the grid.

Code for the procedure Mystery is shown below. Assume that the
parameter p has been assigned a positive integer value (e.g., 1, 2, 3, . . .
).

Which of the following shows the result of calling the procedure when
p = 6?

A.
B.
C.

D.
18. The following question uses a robot in a grid of squares. The robot is

represented as a triangle, which is initially in the top-left square of the
grid and facing toward the top of the grid.

Code for the procedure Mystery is shown below. Assume that the
parameter p has been assigned a positive integer value (e.g., 1, 2, 3, . . .
).

Which of the following shows the result of calling the procedure when
p = 6?

A.

B.

C.
D. Error. The robot moves off the grid.
19. The following question uses a robot in a grid of squares. The robot is
represented as a triangle, which is initially in the bottom-left square of
the grid and facing toward the top of the grid.

Which of the following code segments produces the result above?

A. n ← 2
REPEAT n TIMES{
ROTATE_RIGHT
MOVE_FORWARD
ROTATE_LEFT
MOVE_FORWARD
}

B. MOVE_FORWARD
MOVE_FORWARD
ROTATE_RIGHT
MOVE_FORWARD

C. n ← 2
ROTATE_RIGHT
REPEAT n TIMES
{
MOVE_FORWARD
}
ROTATE_LEFT
MOVE_FORWARD

D. n ← 2
REPEAT n TIMES

{
ROTATE_RIGHT
MOVE_FORWARD
ROTATE_LEFT
MOVE_FORWARD
}
ROTATE_RIGHT
20. The following question uses a robot in a grid of squares. The robot is
represented as a triangle, which is initially in the top-left square of the
grid and facing toward the top of the grid.

Code for the procedure Mystery is shown below. Assume that the
parameter p has been assigned a positive integer value (e.g., 1, 2, 3, . . .
).

Which of the following shows the result of calling the procedure for
any value of p? Select two answers.

A.

B.
C.
D.

21. The following question uses a robot in a grid of squares. The robot is
represented as a triangle, which is initially in the top-left square of the
grid and facing toward the top of the grid.

Code for the procedure Mystery is shown below. Assume that the
parameter p has been assigned a positive integer value of either 0 or 1.

Which of the following shows the result of calling the procedure for
the value of p equal to 0 or 1?

Select two answers.

A.
B.
C.

D.

22. The following program is intended to find the average of a class’s
scores.


Click to View FlipBook Version