{"id":2137,"date":"2014-08-28T14:08:20","date_gmt":"2014-08-28T21:08:20","guid":{"rendered":"https:\/\/blog.digilentinc.com\/?p=2137"},"modified":"2025-06-03T06:44:41","modified_gmt":"2025-06-03T13:44:41","slug":"arrays-theyre-not-so-scary","status":"publish","type":"post","link":"https:\/\/digilent.com\/blog\/arrays-theyre-not-so-scary\/","title":{"rendered":"What are Arrays? Different Dimensions Explained"},"content":{"rendered":"<p>Some of you may have wished at one point that there was a way to conveniently keep track of a series of inputs that you are providing to your system. Luckily, an array is an excellent way to solve this predicament, and it has the added bonus of being easy to use!<\/p>\n<p>&nbsp;<\/p>\n<p>In essence, an array is a set or group of values of a particular data type, such as an integer. However, rather than having to declare each and every integer by name, we only have to declare the array itself; the compiler will assign a location number to the elements of the array based on where they are located within the array. To initialize an array (i.e., declaring\u00a0an array with starting values), we use the following format:<\/p>\n<figure id=\"attachment_2138\" aria-describedby=\"caption-attachment-2138\" style=\"width: 592px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/array-initialization.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-2138\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/array-initialization.jpg\" alt=\"Generic array initialization\" width=\"592\" height=\"43\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/array-initialization.jpg 757w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/array-initialization-300x21.jpg 300w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/array-initialization-225x16.jpg 225w\" sizes=\"auto, (max-width: 592px) 100vw, 592px\" \/><\/a><figcaption id=\"caption-attachment-2138\" class=\"wp-caption-text\">Generic array initialization.<\/figcaption><\/figure>\n<p>For example, if we wanted to declare an array of integers that is five values long with 2, 4, and 6 as the first three integers, we would type:<\/p>\n<figure id=\"attachment_2139\" aria-describedby=\"caption-attachment-2139\" style=\"width: 368px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/array-initializing-values.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2139\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/array-initializing-values.jpg\" alt=\"Partial initialization of an array.\" width=\"368\" height=\"40\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/array-initializing-values.jpg 368w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/array-initializing-values-300x32.jpg 300w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/array-initializing-values-225x24.jpg 225w\" sizes=\"auto, (max-width: 368px) 100vw, 368px\" \/><\/a><figcaption id=\"caption-attachment-2139\" class=\"wp-caption-text\">Partial initialization of an array.<\/figcaption><\/figure>\n<p>Any part of the array that is not assigned values when the array is declared, such as our last two values in the set of five, are assigned a value of zero by the compiler. As I mentioned earlier, each value within the array is assigned a location number with the first value in the array receiving a &#8220;0&#8221;, the second value receiving a &#8220;1&#8221;, and so on until all values within the array have been assigned a sequential location number. We can access individual values within the array to utilize or change them by using their location number. So, if we wanted to increase the third integer of myFirstArray by 10 and then delay the system for the new value of the third integer we would type:<\/p>\n<figure id=\"attachment_2140\" aria-describedby=\"caption-attachment-2140\" style=\"width: 502px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/accessing-specific-values.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2140\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/accessing-specific-values.jpg\" alt=\"Changing and using a specific value within an array.\" width=\"502\" height=\"75\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/accessing-specific-values.jpg 502w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/accessing-specific-values-300x44.jpg 300w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/accessing-specific-values-225x33.jpg 225w\" sizes=\"auto, (max-width: 502px) 100vw, 502px\" \/><\/a><figcaption id=\"caption-attachment-2140\" class=\"wp-caption-text\">Changing and using a specific value within an array.<\/figcaption><\/figure>\n<p>While the single dimension arrays that we have discussed are incredibly useful, it would be a shame if we did not also talk about multi-dimensional arrays.<\/p>\n<p>&nbsp;<\/p>\n<p>At their core, multi-dimensional arrays are groups of single dimension arrays, much like single dimension array is a group of values. With this in mind, you\u00a0can think of a two-dimensional array as multiple single dimensional arrays (of the same length) aligned so that you have a 2D plane of values. A three-dimensional array will (not surprisingly) add a third dimension to create a cube of values (or, in a sense, a plane of arrays).<\/p>\n<figure id=\"attachment_2141\" aria-describedby=\"caption-attachment-2141\" style=\"width: 601px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/3D-array.gif\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-2141\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/3D-array.gif\" alt=\"Visualization of a 3D array from somebody.\" width=\"601\" height=\"398\" \/><\/a><figcaption id=\"caption-attachment-2141\" class=\"wp-caption-text\">Visualization of a 3D array by <a title=\"Kyle's C++ tutorial page\" href=\"http:\/\/sp-productions.zxq.net\/c++%20tutorial2.html\" target=\"_blank\" rel=\"noopener\">Kyle<\/a>.<\/figcaption><\/figure>\n<p>As we continue to add\u00a0extra dimensions, we start getting into some interesting perspectives. A 4D array could be thought of as a line of cubes and 6D array could be thought of as a cube of cubes. And so on.<\/p>\n<figure id=\"attachment_2144\" aria-describedby=\"caption-attachment-2144\" style=\"width: 599px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/6D-array.gif\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-2144\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/6D-array.gif\" alt=\"Visualization of a six dimensional array by Kyle.\" width=\"599\" height=\"319\" \/><\/a><figcaption id=\"caption-attachment-2144\" class=\"wp-caption-text\">Visualization of a six dimensional array by <a title=\"Kyle's C++ tutorial page\" href=\"http:\/\/sp-productions.zxq.net\/c++%20tutorial2.html\" target=\"_blank\" rel=\"noopener\">Kyle<\/a>.<\/figcaption><\/figure>\n<p>You will rarely ever need an array that is larger than three dimensions, since you start to rapidly eat up the memory you have available to you as dimensions are added. \u00a0Luckily, this can be remedied by making switches in your choice of microcontroller&#8211;like from the <a title=\"chipKIT Uno32\" href=\"https:\/\/digilent.com\/blog\/Products\/Detail.cfm?NavPath=2,892,893&amp;Prod=CHIPKIT-UNO32\" target=\"_blank\" rel=\"noopener\">chipKIT Uno32<\/a> to the <a title=\"chipKIT uC32\" href=\"https:\/\/digilent.com\/blog\/Products\/Detail.cfm?NavPath=2,892,1035&amp;Prod=CHIPKIT-UC32\" target=\"_blank\" rel=\"noopener\">chipKIT uC32<\/a>, which has notably more memory.<\/p>\n<figure id=\"attachment_2148\" aria-describedby=\"caption-attachment-2148\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/chipKIT-uC32.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2148\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/chipKIT-uC32.jpg\" alt=\"Digilent's chipKIT uC32 with 512K bytes of flash memory and 32K bytes of static ram.\" width=\"600\" height=\"524\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/chipKIT-uC32.jpg 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/chipKIT-uC32-300x262.jpg 300w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/chipKIT-uC32-225x196.jpg 225w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-2148\" class=\"wp-caption-text\">Digilent&#8217;s chipKIT uC32 with 512K bytes of flash memory and 32K bytes of static RAM.<\/figcaption><\/figure>\n<p>Naturally, as we start adding more dimensions to create our groups of arrays, the way that we initialize the arrays will change slightly. Keeping in line with our &#8220;group of arrays&#8221; theme, we can initialize a 2D array with three single-dimension arrays of four elements each by typing:<\/p>\n<figure id=\"attachment_2145\" aria-describedby=\"caption-attachment-2145\" style=\"width: 325px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/initialize-myFirst2DArray.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2145\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/initialize-myFirst2DArray.png\" alt=\"Initializing a two dimensional array.\" width=\"325\" height=\"207\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/initialize-myFirst2DArray.png 325w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/initialize-myFirst2DArray-300x191.png 300w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/initialize-myFirst2DArray-225x143.png 225w\" sizes=\"auto, (max-width: 325px) 100vw, 325px\" \/><\/a><figcaption id=\"caption-attachment-2145\" class=\"wp-caption-text\">Initializing a two dimensional array.<\/figcaption><\/figure>\n<p>Similarly, we can also initialize a three-dimensional array by adding the &#8220;number of layers&#8221; as the first bracketed element in the declaration of the array, with the &#8220;number of rows&#8221; of single dimension arrays as the second bracketed element and the last bracketed element as number of values in each single dimension array. \u00a0A visually condensed version of a 3D array could be be initialized by:<\/p>\n<figure id=\"attachment_2146\" aria-describedby=\"caption-attachment-2146\" style=\"width: 438px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/initialize-myFirst3DArray.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2146\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/initialize-myFirst3DArray.png\" alt=\"Visually condensed version of a 3D array with the entire &quot;plane&quot; of values shown in each &quot;layer&quot;.\" width=\"438\" height=\"205\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/initialize-myFirst3DArray.png 438w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/initialize-myFirst3DArray-300x140.png 300w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/initialize-myFirst3DArray-225x105.png 225w\" sizes=\"auto, (max-width: 438px) 100vw, 438px\" \/><\/a><figcaption id=\"caption-attachment-2146\" class=\"wp-caption-text\">Visually condensed version of a 3D array with the entire &#8220;plane&#8221; of values shown in each &#8220;layer&#8221;.<\/figcaption><\/figure>\n<p>But what can we do with arrays? One of the more entertaining\u00a0things (in my opinion) that you can do with an array is to use it as a recording device. \u00a0A\u00a0two-dimensional\u00a0array could be set up to hold two single-dimension arrays: one array to record if a button was being pressed or not and a second array to record the length of time that the button was held in each state, with an incrementer to be able to store the values in the appropriate number location within the arrays. Another function could also be created to access and &#8220;play&#8221; each of those stored values within the two dimensional array. You can check out the YouTube video of a <a title=\"video of a recording device\" href=\"https:\/\/www.youtube.com\/watch?v=DHY_imvLcDU\" target=\"_blank\" rel=\"noopener\">recording device<\/a> that does just that.<\/p>\n<p>&nbsp;<\/p>\n<p>Check back every day to see what other cool things Digilent is up to!<\/p>\n<div class='watch-action'><div class='watch-position align-left'><div class='action-like'><a class='lbg-style6 like-2137 jlk' data-task='like' data-post_id='2137' data-nonce='0f678f749c' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Like' \/><span class='lc-2137 lc'>+3<\/span><\/a><\/div><div class='action-unlike'><a class='unlbg-style6 unlike-2137 jlk' data-task='unlike' data-post_id='2137' data-nonce='0f678f749c' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Unlike' \/><span class='unlc-2137 unlc'>-1<\/span><\/a><\/div><\/div> <div class='status-2137 status align-left'><\/div><\/div><div class='wti-clear'><\/div>","protected":false},"excerpt":{"rendered":"<p>Some of you may have wished at one point that there was a way to conveniently keep track of a series of inputs that you are providing to your system. Luckily, an array is an excellent way to solve this predicament, and it has the added bonus of being easy to use!<\/p>\n","protected":false},"author":17,"featured_media":2141,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1563],"tags":[],"ppma_author":[4469],"class_list":["post-2137","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-guide"],"jetpack_featured_media_url":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/08\/3D-array.gif","jetpack_sharing_enabled":true,"authors":[{"term_id":4469,"user_id":17,"is_guest":0,"slug":"jamescolvin","display_name":"James Colvin","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/8aa85f7d11711acc7e571e1ed26c901b614a7064a2e15e522f54d9f26792ea9e?s=96&d=mm&r=g","1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":"","9":"","10":""}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/2137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/comments?post=2137"}],"version-history":[{"count":2,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/2137\/revisions"}],"predecessor-version":[{"id":31626,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/2137\/revisions\/31626"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media\/2141"}],"wp:attachment":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media?parent=2137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/categories?post=2137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/tags?post=2137"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=2137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}