Home › Forums › IB Pseudo Code › Iterating thru a Collection
- This topic has 9 replies, 4 voices, and was last updated 4 years ago by
Shiva Chandnani.
- AuthorPosts
- August 17, 2021 at 2:43 am #6281
Eamonn
KeymasterA 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.
Please add comments
Link to https://ibcomputerscience.xyz/ib-pseudo-code-flowcharts/
August 18, 2021 at 10:32 am #6335girwan
Participantcities = […., …., …..]
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”August 18, 2021 at 5:48 pm #6337joshua7
Participantcities = […., …., …..]
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”August 19, 2021 at 1:21 am #6338Eamonn
KeymasterHi 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
August 19, 2021 at 1:24 am #6339Eamonn
KeymasterHi Joshu
Same comments made to Giraan apply…
please redo and post.
http://ib.compscihub.net/wp-content/uploads/2016/05/4.2.2.pdf
August 19, 2021 at 2:24 am #6360Shiva Chandnani
ParticipantCITES = [ ]
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 ifAugust 19, 2021 at 3:32 am #6364Eamonn
KeymasterPlease redo with correct notation for accessing collections..
August 25, 2021 at 6:15 pm #6499girwan
Participantcities = […., …., …..]
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 ifAugust 29, 2021 at 10:21 am #6544joshua7
Participantcities = […., …., …..]
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 loopAugust 29, 2021 at 3:38 pm #6545Shiva Chandnani
ParticipantCITIES = [ ]
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 - AuthorPosts
- You must be logged in to reply to this topic.