{"id":204,"date":"2020-08-30T21:57:45","date_gmt":"2020-08-30T21:57:45","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/cplusplus\/?p=204"},"modified":"2020-08-30T21:58:27","modified_gmt":"2020-08-30T21:58:27","slug":"c-arrays","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-arrays\/","title":{"rendered":"C++ Arrays"},"content":{"rendered":"<p>IN this lesson, we would consider Arrays. This is a very important topic, not just in C++, but across all programming languages. So, you need to understand it clearly.<\/p>\n<ul>\n<li><a href=\"#t1\">Overview of C++ Arrays<\/a><\/li>\n<li><a href=\"#t2\">Array Declaration<\/a><\/li>\n<li><a href=\"#t3\">Array Initialization<\/a><\/li>\n<li><a href=\"#t4\">Addressing Array Elements<\/a><\/li>\n<li><a href=\"#t5\">Multidimensional Arrays<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Overview of C++ Arrays<\/strong><\/h4>\n<p>Array in C++ is a data structure that stores fixed-length sequential collection of elements. These elements must be of the same type. In order words, we have a collection of variables of same type stored under similar name.<\/p>\n<p>For example, if we have 100 scores, it would we could declare this hundred scores with just on name: score. Then we can access individual scores as score[0], score[1], score[2] and so on. Elements of an array are accessed using their index. That is the number in square brackets.<\/p>\n<p>Arrays are stored in contiguous memory locations.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Array Declaration<\/strong><\/h4>\n<p>Just like variable declaration, you also need to declare arrays before use. To declare an array, you need to specify the data type and the size of the array(that is the number of elements). The general syntax for a 1-dimensional array is:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">type arrayName [ Size ];\r\n<\/pre>\n<p>For example, to declare an array of\u00a0 50 integers, it would be:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">int<\/span> score[<span style=\"color: #0000dd; font-weight: bold;\">50<\/span>]\r\n<\/pre>\n<p>Note that the size of the array must be integer and greater than zero.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Array Initialization<\/strong><\/h4>\n<p>This is a way to assign initial values to array elements. For example, the code below declares array of 4 double and initializes them as well.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">double<\/span> balance[<span style=\"color: #0000dd; font-weight: bold;\">4<\/span>] <span style=\"color: #333333;\">=<\/span> {<span style=\"color: #6600ee; font-weight: bold;\">26.4<\/span>, <span style=\"color: #6600ee; font-weight: bold;\">3.4<\/span>, <span style=\"color: #6600ee; font-weight: bold;\">75.0<\/span>, <span style=\"color: #6600ee; font-weight: bold;\">750.50<\/span>};\r\n<\/pre>\n<p>You can see that the number of elements in curly braces { } is same as the number in the declaration, 4.<\/p>\n<p>You can also omit the size of the array like so<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">double<\/span> score[] <span style=\"color: #333333;\">=<\/span> {<span style=\"color: #6600ee; font-weight: bold;\">96.4<\/span>, <span style=\"color: #6600ee; font-weight: bold;\">67.4<\/span>, <span style=\"color: #6600ee; font-weight: bold;\">75.0<\/span>, <span style=\"color: #6600ee; font-weight: bold;\">75.50<\/span>};\r\n<\/pre>\n<p>I would still work because the compiler would check the number of elements assigned and then reserve memory accordingly.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Addressing Array Elements<\/strong><\/h4>\n<p>As mentioned before, each element of an array has an index. But note that the index is zero-based. So the first element has index of 0, the second element has index of 1 and so on.<\/p>\n<p>To access an element, you use the name of the array with the index in square brackets [ ]. So the code below would retrieve the third score and assign it to a variable student_score<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">double<\/span> student_score <span style=\"color: #333333;\">=<\/span> score[<span style=\"color: #0000dd; font-weight: bold;\">2<\/span>]\r\n<\/pre>\n<p>In the same way, you can assign a value to a particular array element. This would replace the existing value. For example, the code below would change the second score from 67.4 to 99.9.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">score[<span style=\"color: #0000dd; font-weight: bold;\">1<\/span>] <span style=\"color: #333333;\">=<\/span> <span style=\"color: #6600ee; font-weight: bold;\">99.9<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. Multidimensional Arrays<\/strong><\/h4>\n<p>The array we&#8217;ve considered so far are 1-dimensional arrays. This means that they represent a list of numbers. However, we could also have arrays that could be used to represent a matrix.<\/p>\n<p>A two-dimensional array looks like this:<\/p>\n<figure id=\"attachment_205\" aria-describedby=\"caption-attachment-205\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-content\/uploads\/sites\/17\/2020\/08\/Two-dimensional-arrays.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-205 size-medium\" src=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-content\/uploads\/sites\/17\/2020\/08\/Two-dimensional-arrays-300x147.jpg\" alt=\"Two dimensional arrays\" width=\"300\" height=\"147\" srcset=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-content\/uploads\/sites\/17\/2020\/08\/Two-dimensional-arrays-300x147.jpg 300w, https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-content\/uploads\/sites\/17\/2020\/08\/Two-dimensional-arrays.jpg 374w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-205\" class=\"wp-caption-text\">Figure 1 &#8211; A 2-Dimensional Array<\/figcaption><\/figure>\n<p>In this example, we have\u00a0 a two dimensional array. We can define it using it&#8217;s rows and columns. So we can say it&#8217;s a 4 by 3 array (4 x 3 array). This means, 3 rows, 4 columns.<\/p>\n<p>The general syntax to declare a 2D array is:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">type arrayName [ no_of_rows ][ no_of_columns ];\r\n<\/pre>\n<p>So to create the the array in Figure 1 in C++, we would have:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">int<\/span> score[<span style=\"color: #0000dd; font-weight: bold;\">3<\/span>][<span style=\"color: #0000dd; font-weight: bold;\">4<\/span>];\r\n<\/pre>\n<p>However, to initialize it, you can think of creating 3 1-dimensional arrays separated with a comma. The code below does it:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">int<\/span> scores[<span style=\"color: #0000dd; font-weight: bold;\">3<\/span>][<span style=\"color: #0000dd; font-weight: bold;\">4<\/span>] <span style=\"color: #333333;\">=<\/span> {  \r\n   {<span style=\"color: #0000dd; font-weight: bold;\">34<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">67<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">12<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">20<\/span>} ,   <span style=\"color: #888888;\">\/*  row 1 indexed by 0 *\/<\/span>\r\n   {<span style=\"color: #0000dd; font-weight: bold;\">98<\/span>,  <span style=\"color: #0000dd; font-weight: bold;\">5<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">11<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">65<\/span>} ,   <span style=\"color: #888888;\">\/*  for row 2 indexed by 1 *\/<\/span>\r\n   {<span style=\"color: #0000dd; font-weight: bold;\">10<\/span>,  <span style=\"color: #0000dd; font-weight: bold;\">4<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">16<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">45<\/span>}     <span style=\"color: #888888;\">\/*  for row 3 indexed by 2 *\/<\/span>\r\n};\r\n<\/pre>\n<p>You can see the use of nested braces. However, you can choose to remove then and use just one brace like a 1-dimensional array. The compiler will understand!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>IN this lesson, we would consider Arrays. This is a very important topic, not just in C++, but across all programming languages. So, you need &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[31],"class_list":["post-204","post","type-post","status-publish","format-standard","hentry","category-c-tutorials","tag-arrays"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/comments?post=204"}],"version-history":[{"count":3,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/204\/revisions"}],"predecessor-version":[{"id":208,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/204\/revisions\/208"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/media?parent=204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/categories?post=204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/tags?post=204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}