
{"id":538,"date":"2018-10-17T06:49:24","date_gmt":"2018-10-17T06:49:24","guid":{"rendered":"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/?page_id=538"},"modified":"2018-10-17T06:49:24","modified_gmt":"2018-10-17T06:49:24","slug":"array-glossary-entry-2","status":"publish","type":"page","link":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/glossary-2\/array-glossary-entry-2\/","title":{"rendered":"Array &#8211; (Glossary Entry)"},"content":{"rendered":"<p>In C and C++, and array is a block of memory reserved to hold an ordered list of data of the same type. This could be a list of type int, float, double etc.. It can also be a list of other arrays, or more complex types such as structures and unions.<\/p>\n<p>For example:<\/p>\n<pre class=\"lang:c++ decode:true\">#include &lt;stdio.h&gt;\r\nint primes[] = {2,3,5,7,11,13,17,19,23,29};\r\nint main() {\r\n   for (unsigned int n=0; n&lt;10; n++) {\r\n      printf(\"%dth Prime is %d\\n\", n, primes[n]);\r\n   }\r\n   return 0;\r\n}<\/pre>\n<p>Let\u2019s look at this code. First we see the array \u201cprimes\u201d. It is a list of integers (type int). This array is already initialized with values.<\/p>\n<pre class=\"lang:c++ decode:true\"> int primes[] = {2,3,5,7,11,13,17,19,23,29};<\/pre>\n<p>These integer values will be stored adjacent to each other in memory. If the size of an int is 4 bytes (on a 32bit microcontroller), the the array will occupy a block of 40 bytes.<\/p>\n<p>Next, we iterate through and read the values in the array using a for-loop:<\/p>\n<pre class=\"lang:c++ decode:true\">for (unsigned int n=0; n&lt;10; n++)  {\r\n   printf(\"%dth Prime is %d\\n\", n, primes[n]);\r\n}<\/pre>\n<p>For each value of n, we read the <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/ql-cache\/quicklatex.com-2abf0b71b2242df3a72e948f3e5c9ea0_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#110;&#94;&#123;&#116;&#104;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"15\" width=\"24\" style=\"vertical-align: 0px;\"\/> value primes[n].<\/p>\n<p>Consider a more complex example now:<\/p>\n<pre class=\"lang:c++ decode:true\">#include &lt;stdio.h&gt;\r\n#include &lt;math.h&gt;\r\n\r\n#define N 1000\r\n\r\ndouble x1[N];\r\ndouble x2[N];\r\ndouble delta[N];\r\n\r\n\/\/Initial values - make these very similar\r\ndouble initialValue1 = 0.3;\r\ndouble initialValue2 = 0.299999999;\r\n\r\n\/\/The parameter r, set to generate a chaotic sequence\r\ndouble r= 4.0;\r\n\r\nint main() {\r\n   \r\n   \r\n   \/\/******************************************\r\n   \/\/Let's generate a chaotic sequence of data!\r\n   \/\/******************************************\r\n   \/\/ https:\/\/en.wikipedia.org\/wiki\/Logistic_map\r\n   \r\n   \/\/Start with very similar initial conditions\r\n   x1[0] = initialValue1;\r\n   x2[0] = initialValue2;\r\n   delta[0] = 0.0;\r\n   \r\n   \/\/Calculate values for n=1..99 and any differences \r\n\t\/\/using the same \"logisic map\" function\r\n   for (unsigned int n=0; n&lt;(N-1); n++) {\r\n      x1[n+1]    = r*x1[n]*(1.0-x1[n]);\r\n      x2[n+1]    = r*x2[n]*(1.0-x2[n]);\r\n      delta[n+1] = x1[n+1] - x2[n+1];\r\n   }\r\n   \r\n   \/\/Write results\r\n   for (unsigned int n=0; n&lt;N; n++) {\r\n      printf(\"n=%d\\t%8.6f\\t%8.6f\\tDIFFERENCE=%8.6f\\n\", n, x1[n], x2[n], delta[n]);\r\n   }\r\n   \r\n   return 0;\r\n}<\/pre>\n<p>First of all, we define N as an alias for 1000<\/p>\n<pre class=\"lang:c++ decode:true \">#define N 1000<\/pre>\n<p>Now we create three arrays of type <span class=\"lang:c++ highlight:0 decode:true  crayon-inline \">double<\/span>\u00a0.<\/p>\n<pre class=\"lang:c++ decode:true \">double x1[N];\r\ndouble x2[N];\r\ndouble delta[N];<\/pre>\n<p>Each of these is a distinct block of memory, <span class=\"lang:c++ decode:true  crayon-inline \">size N*sizeof(double)<\/span>\u00a0. The first values each array is then initialised:<\/p>\n<pre class=\"lang:c++ decode:true \">x1[0] = initialValue1;\r\nx2[0] = initialValue2;\r\ndelta[0] = 0.0;<\/pre>\n<blockquote><p>Note that the index (value in square brackets) starts at zero for C and C++ arrays.<\/p><\/blockquote>\n<p>For the curious, we see <span class=\"lang:c++ decode:true  crayon-inline \">initialValue1<\/span>\u00a0 and <span class=\"lang:c++ decode:true  crayon-inline \">initialValue2<\/span>\u00a0 have very similar values as this is a demonstration of \u201cchaotic behavior\u201d).<\/p>\n<p>Next we once again use a for-loop to iterate, calculate some values and store results in the arrays:<\/p>\n<pre class=\"lang:c++ decode:true \">   for (unsigned int n=0; n&lt;(N-1); n++) {\r\n      x1[n+1]    = r*x1[n]*(1.0-x1[n]);\r\n      x2[n+1]    = r*x2[n]*(1.0-x2[n]);\r\n      delta[n+1] = x1[n+1] - x2[n+1];\r\n   }<\/pre>\n<p>The value of n ranges from 0 to 98. Once the arrays are full, we write the values to the terminal:<\/p>\n<pre class=\"lang:c++ decode:true \">for (unsigned int n=0; n&lt;N; n++) {\r\n   printf(\"n=%d\\t%8.6f\\t%8.6f\\tDIFFERENCE=%8.6f\\n\", n, x1[n], x2[n], delta[n]);\r\n}<\/pre>\n<p>Note the data types:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-539\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/arraysDataType.png\" alt=\"\" width=\"600\" height=\"134\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/arraysDataType.png 1546w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/arraysDataType-300x67.png 300w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/arraysDataType-768x171.png 768w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2018\/10\/arraysDataType-1024x228.png 1024w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<blockquote><p>When we use square brackets to access the <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/ql-cache\/quicklatex.com-2abf0b71b2242df3a72e948f3e5c9ea0_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#110;&#94;&#123;&#116;&#104;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"15\" width=\"24\" style=\"vertical-align: 0px;\"\/> value in an array, such as <span class=\"lang:c++ decode:true  crayon-inline \">x[n]<\/span>\u00a0, we say we are <em>dereferencing<\/em> the array.<\/p>\n<p>When we write the array name without any square braces, we get the memory address of the first element in the array. This is also known as a <em>reference<\/em> to the first element of the array. Therefore we call <span class=\"lang:c++ decode:true  crayon-inline \">x1<\/span>\u00a0 a reference type.<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In C and C++, and array is a block of memory reserved to hold an ordered list of data of the same type. This could be a list of type int, float, double etc.. It can also be a list of other arrays, or more complex types such as structures and unions. For example: #include&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/glossary-2\/array-glossary-entry-2\/\">Continue reading <span class=\"screen-reader-text\">Array &#8211; (Glossary Entry)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":153,"menu_order":62,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-538","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/538","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/comments?post=538"}],"version-history":[{"count":1,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/538\/revisions"}],"predecessor-version":[{"id":540,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/538\/revisions\/540"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/153"}],"wp:attachment":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/media?parent=538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}