Y1 Flashcards
(235 cards)
1What is a variable?
A variable is a named container, held by the computer in a memory location
What is variable scope?
The block of code where the variable can be declared, used and modified
What is a local variable?
variable that is given local scope. A local variable reference in the function or block in which it is declared overrides the same variable name in the larger scope
When should you use local variables and when should you use global variables?
If you feel that a certain variable will take multiple values by passing through various functions then use local variables and pass them in function calls. If you feel that a certain variable you need to use will have constant value, then declare it as a global variable
Why do computers use binary to store data?
Easier to manufacture, therefore cheaper and it is more reliable
Examples of ways to store data using binary?
RAM, hard disk, optical disk and flash memory
What is a data structure?
A way of storing and managing data
What is a data type?
A classification that specifies which type of value a variable has and what type of mathematical, relational or logical operations can be applied to it without causing an error
What is an array?
A set of data items of the same type grouped together using a single identifier. Each of the data items or elements are addressed by the variable name eg myArray and an index. It is static - size is fixed when the structure is created/size cannot change at runtime.
What is a dimension?
The dimension of an array is the number of indices needed to select an element. A one-dimensional has one index. A 1D array is like a column of data. A two-dimensional has two indexes, one for the row and another for the column. A 2D array is like a table of data.
What is a list?
Ordered set of data accessed by index, not fixed size
What is a record?
think database, a record comprises multiple, related attributes (fields/columns) of different data types
What is a tuple?
immutable list (cannot be changed)
How do you declare a 1d array pseudocode(first way)?
array names[3]
names[0] = “Ahmad”
names[1] = “Ben”
names[2] = “Catherine”
How do you declare a 1d array pseudocode(second way)?
array names[3] = [“Ahmad”, “Ben”, “Catherine”]
Show the pseudocode for a linear search
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
First way to declare 2d array
array scores[2][2]
scores[0][0] = 14
scores[0][1] = 80
scores[1][0] = 20
Scores[1][1] = 82
Second way to declare 2d array
array scores[2][2] = [[14, 80],[20, 82]]
Example of data that could be stored in a 2d array?
Scores for a class of students for multiple tests
How many elements would be in the array chores[3][3]
9
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
Disadvantages of bubble sort
In standard form, it’s inefficient as it may continue to makes passes even though the list is sorted (this can be overcome by setting a swaps variable – if swaps are made continue, otherwise stop)
Advantages of bubble sort?
Easy to implement
Good for small data sets that are almost sorted
Thinking concurrently?
What can be done at the same time