Arrays Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is an array?

A

A data structure that allows you to hold many items of data which is referenced by one identifier
e.g. array usernames[10]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do I add to an array?

A

array usernames[10] // 10 is the fixed number in this case
usernames[0] = “psmith”
usernames[1] = “ltorvalds”
usernames[2] = “pwatts”
usernames[3] = “sjones”
usernames[4] = “mpatel”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do I find an array’s length?

A

usernames.length //this returns 10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How would you write a FOR loop to output all the usernames in the array?

A

for i = 0 to usernames.length
print(username[i])
endfor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a linear search?

A

Each item is examined in sequence until the item is found or the end of the list is reached

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Adapt code so that the user can enter a username and find out if it is present in the array (Linear search)

A

search = input(“Type in username: “)
for i in range(usernames.length)
if usernames[i] == search
print(“Name found”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is bubble sort?

A

Each item in a list is compared to the one next to it, and if it is greater, they swap places.
At the end of one pass through the list, the largest item is at the end of the list
This is repeated until the items are sorted

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How could you swap items using an array (Bubble sort)

A

names = [“Sam”, “Ron”, “Tom”, “Bob”, “Mo”]
numItems = len[names]
for i = 0 to numItems - 2
for j = 0 to numItems - i - 2
if names[j] > names[j + 1] then
temp = names[j]
names[j] = names[j+1]
names[j+1] = temp
endif
next j
next i

How well did you know this?
1
Not at all
2
3
4
5
Perfectly