Iterating thru a Collection

Home Forums IB Pseudo Code Iterating thru a Collection

Viewing 10 posts - 1 through 10 (of 10 total)
• Author
Posts
• #6281
Eamonn
Keymaster

A geography teacher is searching CITIES. The city names are stored in a collection called CITES. The teacher just wishes to print out the names of the cities beginning with D or B.

If no cities begin with C or D then inform the end user.

TASK1: Construct pseudo code algorithm ( using correct IB notation ) to indicate how this can be achieved.

• This topic was modified 11 months, 4 weeks ago by Eamonn.
• This topic was modified 11 months, 4 weeks ago by Eamonn.
#6335
girwan
Participant

cities = […., …., …..]
found = False // to check later if any cities were found or not
loop COUNT from 0 to len(cities)-1
cities[COUNT] = city // to search an individual element from the collection
if city[0] = “D” OR “B” then // checks the element of index 0 of city
output city
found = True
if found = False then
output “No cities with D or B found”

#6337
joshua7
Participant

cities = […., …., …..]
loop COUNT from 0 to (cities)-1 // iterates through the list
while COUNT = “D” OR “B” //checks through list
output cities
else
output “No cities begin with D or B”

#6338
Eamonn
Keymaster

Hi Girwan

Logic looks good, but the data is held in a collection you are using an array access methods … http://ib.compscihub.net/wp-content/uploads/2016/05/4.2.2.pdf

#6339
Eamonn
Keymaster

Hi Joshu

#6360
Shiva Chandnani
Participant

CITES = [ ]
found = false
loop i from o to n-1 //used to loop or iterate through the list
if firstletter(CITIES[i]) = “D” or “B” then //checks if any of the cities begin with d or b
output CITIES[i] and found = true //if a city begins with d or b it is outputted and false is now true
end if
end loop
if found = false then //if found remains false, which means no cities were found that begins with d or b
output “no cities begin with D or B” //telling the end-user that no cities began with d or b if found remains false
end if

#6364
Eamonn
Keymaster

Please redo with correct notation for accessing collections..

#6499
girwan
Participant

cities = […., …., …..]
found = False // to check later if any cities were found or not
loop while cities.hasNext() //to continue the loop until there is no next element in the collection
city= cities.getNext() // to select an individual element from the collection
if city[0] = “D” or “B” then // checks the element of index 0 of city
output city
found=True
end if
end loop
if found = False then
Output “No cities with D or B found”
end if

#6544
joshua7
Participant

cities = […., …., …..]
loop while cities.hasNext() // iterates through the loop until the list is over
city = cities.getNext() // to select the next element from the collection
if city = “D” OR “B” then //checks through list
output city
else
output “No cities begin with D or B”
end if
end loop

#6545
Shiva Chandnani
Participant

CITIES = [ ]
found = false
CITIES.restNext()
loop while CITIES.hasNext()
i = cities.getNext()
if firstletter(CITIES[i]) = “D” or “B” then
output city AND found = True
end if
end loop
if found = False then
Output “No cities with D or B found”
end if

Viewing 10 posts - 1 through 10 (of 10 total)
• You must be logged in to reply to this topic.
Scroll to Top