{"id":342,"date":"2022-05-16T16:45:33","date_gmt":"2022-05-16T15:45:33","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phsn5computingscience\/?page_id=342"},"modified":"2022-05-16T16:59:23","modified_gmt":"2022-05-16T15:59:23","slug":"traversing-a-1-d-array","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phsn5computingscience\/traversing-a-1-d-array\/","title":{"rendered":"Traversing a 1-D Array"},"content":{"rendered":"<p>To traverse an array means to access each element (item) stored in the array so that the data can be checked or used as part of a process.<\/p>\n<p>In most high-level languages, it is necessary to create a variable that will track the position of the element currently being accessed. This variable is often referred to as a loop counter.<\/p>\n<p>When used in high-level languages, the name given to the loop counter is usually \u2018i\u2019 or \u2018counter\u2019.<\/p>\n<h3 style=\"color: red\">Traversing an array within upper and lower bounds<\/h3>\n<p>If a loop is set to repeat ten times (from 0 to 9) then:<\/p>\n<ul>\n<li>during the first iteration the loop counter will hold the value 0<\/li>\n<li>during the second iteration it will hold 1<\/li>\n<li>during the third iteration it will hold 2 and so on<\/li>\n<\/ul>\n<p>This lets the program access the elements stored in the array in order<\/p>\n<h2 style=\"color: red\"><strong>Python Code Example<\/strong><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-345\" src=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16164518\/Screenshot-2022-05-16-164452-300x74.png\" alt=\"\" width=\"921\" height=\"227\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16164518\/Screenshot-2022-05-16-164452-300x74.png 300w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16164518\/Screenshot-2022-05-16-164452-1024x253.png 1024w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16164518\/Screenshot-2022-05-16-164452-768x190.png 768w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16164518\/Screenshot-2022-05-16-164452-624x154.png 624w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16164518\/Screenshot-2022-05-16-164452.png 1260w\" sizes=\"auto, (max-width: 921px) 100vw, 921px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-347\" src=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16165814\/Screenshot-2022-05-16-165800-300x56.png\" alt=\"\" width=\"755\" height=\"141\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16165814\/Screenshot-2022-05-16-165800-300x56.png 300w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16165814\/Screenshot-2022-05-16-165800-1024x191.png 1024w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16165814\/Screenshot-2022-05-16-165800-768x143.png 768w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16165814\/Screenshot-2022-05-16-165800-624x116.png 624w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phsn5computingscience\/uploads\/sites\/10033\/2022\/05\/16165814\/Screenshot-2022-05-16-165800.png 1260w\" sizes=\"auto, (max-width: 755px) 100vw, 755px\" \/><\/p>\n<p>The output for the above program would be:<\/p>\n<p><strong>9.87<\/strong><br \/>\n<strong>8.76<\/strong><br \/>\n<strong>9.65<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>To traverse an array means to access each element (item) stored in the array so that the data can be checked or used as part of a process. In most high-level languages, it is necessary to create a variable that will track the position of the element currently being accessed. This variable is often referred [&hellip;]<\/p>\n","protected":false},"author":79984,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-342","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phsn5computingscience\/wp-json\/wp\/v2\/pages\/342","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phsn5computingscience\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phsn5computingscience\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phsn5computingscience\/wp-json\/wp\/v2\/users\/79984"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phsn5computingscience\/wp-json\/wp\/v2\/comments?post=342"}],"version-history":[{"count":3,"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phsn5computingscience\/wp-json\/wp\/v2\/pages\/342\/revisions"}],"predecessor-version":[{"id":349,"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phsn5computingscience\/wp-json\/wp\/v2\/pages\/342\/revisions\/349"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phsn5computingscience\/wp-json\/wp\/v2\/media?parent=342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}