4.2_-_4.3_TraversingAListGuidedNotes

docx

School

Concordia University Portland *

*We aren’t endorsed by this school

Course

3446225VDR

Subject

Computer Science

Date

Dec 6, 2023

Type

docx

Pages

5

Uploaded by DukeUniverse12889

Report
A+CR APCSP Name: ______________ Traversing a List: What can we use to find the position of an element on the list? index What is the result of executing this code? changes the value stored in index 1 to 7 Can we use an operation or statement to move from index 1 to index 2? Yes, change index value by 1 Consider this loop - how many times will this loop execute? 8 times Does this code utilize abstraction? yes, it allows us to execute the code no matter what the length of the list is Pseudocode the algorithm to go to each item on the list and have it increase the value stored by 2 1
A+CR APCSP Name: ______________ Index = 1 REPEAT (length of practiceList) replace item (INDEX) of practiceList with item (INDEX) + 2 Index = Index + 1 Now open Snap! and create a list and implement the following code: This algorithm is commonly used to traverse a list, where the “doSomething” does something with elements of the list. Create your own custom “doSomething” block to do something like the value of each element in the list. https://snap.berkeley.edu/snap/snap.html#present:Username=jasminefivey&ProjectName=4.2 Write the pseudocode to look at each item in the list and count the number of EVEN numbers stored in the list. First, write the operation using MOD that we can use to determine if a number is even Now in Snap! - implement the algorithm Initial here when implemented successfully: _______________ Combine Traversing a list with Conditional Statements Now we are going to create a few more algorithms. First, pseudocode the algorithm, then program in Snap - write the code to do the following: Report the last value stored in the list 2
A+CR APCSP Name: ______________ Calculate the Sum of the Values stored on the list, and report Calculate the Average Value stored on the list, and report Traversing a List: https://snap.berkeley.edu/snap/snap.html#present:Username=carolaplus&ProjectName=4.2TraversingListQue stion - evaluate the program code What is the Length of the List? ___10_______ Explain in Natural Language the purpose of this procedure It finds the odd numbers of a list by asking if the remainder does not equal 0. If it does it is even and does not count towards the odd list. Value of i Element Evaluated Is Odd Value of numOdds 0 - - 0 1 5 true 1 2 10 false 1 3 15 true 2 4 20 false 2 5 25 true 3 6 30 false 3 7 35 true 4 8 40 false 4 9 45 true 5 3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
A+CR APCSP Name: ______________ 10 50 false 5 11 - - 5 Explain why the ending value of i = 11 The ending value is i=11 because the repeat block states “repeat until i>length of list” so the code goes through until i is larger than the list and in this case it is 11 so it just repeats the amount of odd numbers Predict the output of these two code segments given this list of names . It will say the names that begin with the letter S and go through the index It will add an extra step since index starts at 0 and not 1 Now input this code in Snap! And verify your predictions. Were you correct? Yes and No, the second one stops before it reaches the name Stella What is the difference between these two code segments? One goes through the names starting at index 1 and going to 7 the other only goes to the 6 since it begins at 0. Does it change the output? Yes it does 4
A+CR APCSP Name: ______________ How? It misses the last name, Stella, from being spoken and stops at 6 not 7. 5