{"id":399,"date":"2012-08-19T01:54:51","date_gmt":"2012-08-19T08:54:51","guid":{"rendered":"http:\/\/blogs.unsw.edu.au\/comp1400\/?p=399"},"modified":"2012-08-19T01:54:51","modified_gmt":"2012-08-19T08:54:51","slug":"more-arrays","status":"publish","type":"post","link":"https:\/\/blogs.unsw.edu.au\/comp1400\/blog\/2012\/08\/more-arrays\/","title":{"rendered":"More arrays"},"content":{"rendered":"<p>This week&#8217;s tut covers two very common activities in programming: <em>mapping<\/em> and <em>filtering<\/em>.<\/p>\n<ul>\n<li>Write a method <code>mapDouble<\/code> which takes in an array of integers and returns a new array containing the double of each entry in the first array. Eg: <\/li>\n<p><code><br \/>\nint[] data = {1, 3, 4, 2, 0, 5, -2};<br \/>\nint[] doubled = mapDouble(data);<br \/>\n<\/code><br \/>\nThe returned array <code>doubled<\/code> should contain <code>{2, 6, 8, 4, 0, 10, -4}<\/code>.<\/p>\n<li>Write a method <code>filterEvens<\/code> which takes in an array of integers and returns a new array containing only the even values. Eg:<br \/>\n<code><br \/>\nint[] data = {1, 3, 4, 2, 0, 5, -2};<br \/>\nint[] evens = filterEvens(data);<br \/>\n<\/code><br \/>\nThe returned array <code>evens<\/code> should contain <code>{4, 2, 0, -1}<\/code>.<\/p>\n<p><strong>Hint 1:<\/strong> You can check if a number is even by testing if the remainder when dividing by 2 is zero.<\/p>\n<p><strong>Hint 2:<\/strong> You will probably need to do this in two stages. First count the number of even entries to work out how big the returned array should be. Second, go through the list copying entries. If you have trouble writing this in code, try working through examples by hand on paper.\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This week&#8217;s tut covers two very common activities in programming: mapping and filtering. Write a method mapDouble which takes in an array of integers and returns a new array containing the double of each entry in the first array. Eg: int[] data = {1, 3, 4, 2, 0, 5, -2}; int[] doubled = mapDouble(data); The [&hellip;]<\/p>\n","protected":false},"author":80,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[630],"tags":[],"class_list":["post-399","post","type-post","status-publish","format-standard","hentry","category-lab"],"_links":{"self":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/399","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/users\/80"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/comments?post=399"}],"version-history":[{"count":3,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/399\/revisions"}],"predecessor-version":[{"id":402,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/399\/revisions\/402"}],"wp:attachment":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/media?parent=399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/categories?post=399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/tags?post=399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}