Chapter 5.6 - Quiz App Projects Loops with Lists
Time Estimate: 90 minutes
5.6.1. Introduction and Goals
In this lesson, you will complete several small programming projects that add enhancements to the Quiz App. You are encouraged to discuss your ideas for how to solve these problems with the instructor and with your partner and other students. Hints and suggestions are provided.
Learning Objectives: I will learn to
count right and wrong answers using a list to keep track of which questions have already been answered
use loops with lists and standard algorithms to enhance my app
manipulate lists in pseudocode using the insert, append, and length procedures
design and implement my own custom quiz app features
Language Objectives: I will be able to
use target vocabulary, such as insert, append, and length while improving app features and User Interface with the support of concept definitions and vocabulary notes from this lesson
5.6.2. Learning Activities
Loops with Lists:
Activity: 5.6.2.1 YouTube (zEZ3F9SgfPE)
Here is a quick review of comparing AP pseudocode and App Inventor blocks for loops with list:
Basic operations on lists include:
Accessing an element by index: list[i] where i is an index from 1 to the length of the list.
Saving an element of a list into a variable like x: x ← list[i]
Assigning a value to an element of a list:
list[i] ← x assigns the value of x to list[i].
list[i] ← list[j] assigns the value of list[j] to list[i].
Some other list operations in AP-style questions are:
INSERT(list, i, value) : inserts value into the list at index i, moving down all other items at and after i in the list.
APPEND(list, value): adds value to the end of the list.
REMOVE(list, i): removes the item at index i and moves up all items after the ith item.
LENGTH(list): evaluates to the number of elements currently in the given list.
Programming and Creative Projects
For this lesson you can start up App Inventor and open the project you created in the previous lesson. After opening your Quiz project, rename it QuizProjects2, for Quiz Version 2 -- or something similar to that. Then complete the programming exercises described below.
If/else Scoring Algorithm: Modify your app to keep score of how many questions are answered correctly or incorrectly. Be sure to restrict it so that the quiz taker can only receive credit for answering each question once (i.e., if there are three questions, the quiz taker can only be credited with three correct answers). Use this short handout to guide you with this project.
Loop Algorithm for Searching: Add a keyword search capability to your app. For example, if the user types in NASA and clicks on the search button, you should find the question or answer with the word NASA in it and show that question. This will be a linear search through the parallel question and answer lists using a loop. Use this short handout to guide you with this project.
Your Own Quiz App: Use the Quiz App as a template to create a quiz on a topic of your own choosing. Besides changing the questions, answers, and pictures, add at least one enhancement to the app. Be creative!
5.6.3. Summary
In this lesson, you learned how to:
Learning Objective AAP-2.N.a: For list operations: a. Write expressions that use list indexing and list procedures.
Learning Objective AAP-2.N.b: For list operations: b. Evaluate expressions that use list indexing and list procedures.
The exam reference sheet provides basic operations on lists, including: - accessing an element by index o aList[i] accesses the element of aList at index i. The first element of aList is at index 1 and accessed using the notation aList[1]. - assigning a value of an element of a list to a variable o x ← aList [i] assigns the value of aList[i] to the variable x. - assigning a value to an element of a list o aList[i] ← x assigns the value of x to aList[i]. o aList[i] ← aList[j] assigns the value of aList[j] to aList[i]. - inserting elements at a given index o INSERT(aList, i, value) shifts to the right any values in aList at indices greater than or equal to i. The length of the list is increased by 1, and value is placed at index i in aList. -adding elements to the end of the list o APPEND(aList, value) increases the length of aList by 1, and value is placed at the end of aList. - removing elements o REMOVE(aList, i) removes the item at index i in aList and shifts to the left any values at indices greater than i. The length of aList is decreased by 1. - determining the length of a list o LENGTH(aList) evaluates to the number of elements currently in aList.
List procedures are implemented in accordance with the syntax rules of the programming language.
Learning Objective AAP-2.O.a: For algorithms involving elements of a list: a. Write iteration statements to traverse a list.
Learning Objective AAP-2.O.b: For algorithms involving elements of a list: b. Determine the result of an algorithm that includes list traversals.
Traversing a list can be a complete traversal, where all elements in the list are accessed, or a partial traversal, where only a portion of elements are accessed. EXCLUSION STATEMENT (EK AAP-2.O.1): Traversing multiple lists at the same time using the same index for both (parallel traversals) is outside the scope of this course and the AP Exam.
Iteration statements can be used to traverse a list.
The exam reference sheet provides FOR EACH item IN aList { } The variable item is assigned the value of each element of aList sequentially, in order, from the first element to the last element. The code in block of statements is executed once for each assignment of item.
Knowledge of existing algorithms that use iteration can help in constructing new algorithms. Some examples of existing algorithms that are often used with lists include: - determining a minimum or maximum value in a list - computing a sum or average of a list of numbers
5.6.4. Self-Check
Vocabulary
Here is a table of the technical terms we've introduced in this lesson. Hover over the terms to review the definitions.
insert
append
length
Check Your Understanding
You can practice more algorithms with loops and lists below. It is useful to know standard algorithms that use loops like searching for an item in a list, finding the minimum or maximum value in a list, computing the sum or average of a list of values, etc. Using existing algorithms as building blocks for constructing new algorithms has benefits such as reducing development time, reducing testing, and simplifying the identification of errors.
Rearrange the scrambled blocks below to create a loop that iterates through the list of numbers and adds them to the sum variable.
Q-3:
What does the following code do?
A. Displays 0 which is the minimum (lowest) value in the list.
B. Displays -1 which is the value of x.
C. Displays 4 which is the maximum (largest) value in the list.
D. Displays 1 which is the first item in the list.
Q-4:
Which code below could be placed in the following loop to print out the item in a list that has the lowest (minimum) value?
list ← [1, 0, 4, 2]
x ← 99
FOR EACH item IN list
{
<MISSING CODE>
}
DISPLAY(x)
A. IF (item < 99)
x ← item
B. IF (item > x)
x ← item
C. IF (item > 99)
x ← item
D. IF (item < x)
x ← item
Q-5:
What are the values in the list after executing the following code:
list ← [ 0, 3, 5 ]
APPEND( list, 4 )
INSERT( list, 2, 1 )
REMOVE( list, 1 )
A. [0, 3, 4, 5]
B. [1, 2, 3, 4]
C. [1, 3, 5, 4]
D. [0, 3, 5, 4]
Sample AP CSP Exam Questions
Q-6:
A summer camp offers a morning session and an afternoon session.
The list morningList contains the names of all children attending the morning session, and the list afternoonList contains the names of all children attending the afternoon session.
Only children who attend both sessions eat lunch at the camp. The camp director wants to create lunchList, which will contain the names of children attending both sessions.
The following code segment is intended to create lunchList, which is initially empty. It uses the procedure IsFound (list, name), which returns true if name is found in list and returns false otherwise.
FOR EACH child IN morningList
{
<MISSING CODE>
}
Which of the following could replace <MISSING CODE> so that the code segment works as intended?
A. IF (IsFound (morningList, child) {
APPEND (lunchList, child)
}
B. IF ((IsFound (morningList, child)) OR
(IsFound (afternoonList, child)))
{
APPEND (lunchList, child)
}
C. IF (IsFound (lunchList, child))
{
APPEND (afternoonList, child)
}
D. IF (IsFound (afternoonList, child))
{
APPEND (lunchList, child)
}
Q-7:
A teacher uses the following program to adjust student grades on an assignment by adding 5 points to each student’s original grade. However, if adding 5 points to a student’s original grade causes the grade to exceed 100 points, the student will receive the maximum possible score of 100 points. The students’ original grades are stored in the list gradeList, which is indexed from 1 to n.
i ← 1
REPEAT n TIMES
{
<MISSING CODE>
i ← i + 1
}
The teacher has the following procedures available.
Which of the following code segments can replace <MISSING CODE> so that the program works as intended?
Select two answers.
A. gradeList[i] ← gradeList[i] + 5
IF (gradeList[i] > 100)
{
gradeList[i] ← gradeList[i] - 5
}
B. gradeList[i] ← gradeList[i] + 5
IF (gradeList[i] > 100)
{
gradeList[i] ← 100
}
C. gradeList[i] ← max (gradeList[i] + 5, 100)
D. gradeList[i] ← min (gradeList[i] + 5, 100)
5.6.5. Reflection: For Your Portfolio
Answer the following portfolio reflection questions as directed by your instructor. Questions are also available in this Google Doc where you may use File > Make a Copy to make your own editable copy.
Consider your solution for the second project that added a search button.
Provide a screenshot of the search button procedure.
Provide a screenshot of the code that calls the search procedure.
Describe what the search procedure does.
Describe how the search procedure contributes to the overall functionality of the program.
Explain in detailed steps how the search procedure works in enough detail that someone else could recreate it.
Consider testing your search procedure.
Describe two possible calls to the search procedure that pass a different argument and cause a different segment of the search procedure to execute.
Describe the condition(s) being tested by each call you described.
Identify the results of each call you described.
Write AP text-style pseudocode for a linear search that searches through a list to find an item x. It should display found if the x is equal to an item in the list.
Give brief descriptions of the enhancements you added to your app for the third project, a quiz topic of your own choosing. Provide screenshots of important blocks and describe how you used them to solve certain programming problems.
Portfolio Reflection Questions
Make a copy of this document in your Portfolio Assignments folder and answer these questions in the spaces below. Once complete, turn in this assignment according to the steps given by your teacher.
5.6 Quiz App Projects Curriculum Page
Answer the following questions:
1. Consider your solution for the second project that added a search button.
a. Provide a screenshot of the search button procedure.
b. Provide a screenshot of the code that calls the search procedure.
c. Describe what the search procedure does.
d. Describe how the search procedure contributes to the overall functionality of the program.
e. Explain in detailed steps how the search procedure works in enough detail that someone else could recreate it.
Answer
<Insert First Screenshot Here>
<Insert Second Screenshot Here>
Answer c-e here
2. Consider testing your search procedure.
a. Describe two possible calls to the search procedure that pass a different argument and cause a different segment of the search procedure to execute.
b. Describe the condition(s) being tested by each call you described.
c. Identify the results of each call you described.
Answer
3. Write AP text-style pseudocode for a linear search that searches through a list to find an item x. It should display found if the x is equal to an item in the list.
Answer
4. Give brief descriptions of the enhancements you added to your app for the third project, a quiz topic of your own choosing. Provide screenshots of important blocks and describe how you used them to solve certain programming problems.
Answer
Last updated