2. Flashcards
What are the three programming constructs?
Sequence - executing one statement after another.
Iteration - repeating a section of code while a Boolean expression is true. This is with a for loop, while loop, do… while loop.
Branching (selection) - branching to a different place in the program according to the value of a Boolean expression. This is with if statements or switch/case.
What is the Linear Search in pseudocode?
array names[6] = [“Dave”, “Richard”, “Astrid”, “Dima”, “Charlie”, “Paul”]
found = false
for i = 0 to names.length - 1
if (names[i] == “Roberto”) then
print(“Found at index “ + i)
found = true
break // stops the for loop
endIf
next i
if (found == false) then
print(“No matching item is found”)
endIf
How do you use sign and magnitude to represent numbers?
Positive numbers - Same as unsigned binary except the left most bit has to be a 0.
Negative numbers - Same as unsigned binary except the most significant digit has to be 1.
How do you use 2s compliment to represent number?
Positive numbers - Same as unsigned binary except the left most bit has to be a 0.
Negative numbers - Take the positive version of the number and then keep it same up to and keep the first 1, then start to flip the rest. Right to Left.
When subtracting numbers what can you do?
Convert the number to a negative number and then add them and IGNORE ANY OVERFLOW.
What is the Binary Search in pseudocode?
array myIntegers[5] = [12, 16, 19, 22, 25]
start = 0
end = 4
found = false
input searchItem(“Please enter search item”)
midPoint = 0
while (found == false AND START <= END)
MIDPOINT = (START + END) DIV 2
if (myIntegers[midPoint] == searchItem) then
found = true
elseif (myIntegers[midPoint] > searchItem)
end = midPoint - 1
else
start = midPoint +1
endif
endwhile
if (found == true) then
print(“Item found at “ + midPoint)
endif
What are the pros and cons of the binary search?
+ Efficient on large sorted data.
+ Can use strings.
- Slow on small list that is unlikely to grow.
What are the positives of global scope?
Once defined once, it can be used anywhere again and again.
If the value is changed somewhere it is changed for every place in the program (might be desired).
What are the negatives of global scope?
Makes code harder to read as it is harder to follow the data between subroutines. Memory for the variable is locked up for the entire time the program is running.
What are the positives of local scope?
Can reuse the variable name in other subroutines.
Once the subroutine has finished the local variable is no longer needed so the memory is freed up.
This also makes the subroutine more reusable.
Harder to make a mistake by changing the value accidently.
What are the negatives of local scope?
Need to be passed into subroutines or you need to redefine the variable.
Can’t be used outside of scope, needs knowledge of the scope.
What is the difference between passing in the variable ByRef and ByVal?
ByRef or By Reference passes in the memory address, updaring it in memory.
ByVal or By Value copies the value and allows a new variable to be used. It doesn’t change the value if used outside it’s scope. Acts as a local variable then.
When do you not pass in the variable as a parameter?
It is a global variable.
What is a bubble sort?
A sorting algorithm that orders data in a list or an array in any order that you desire by moving through the lists from end to end swapping in necessary.
What is the pseudocode for bubble sort?
array myNumbers[6]
for (i = 0 to myNumbers.length - 2)
swapped = false
for (j = 1 to myNumbers.length - 1 - i)
if (myNumbers[j-1] > myNumbers[j])
temp = myNumbers[j-1]
myNumbers[j-1] = myNumbers[j]
myNumbers[j] = temp
swapped = true
endif
next j
if (swapped == false) then
break //breaks out of loop
endif
next i