{"id":273,"date":"2017-06-14T12:08:00","date_gmt":"2017-06-14T10:08:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/2017\/06\/14\/arrays\/"},"modified":"2020-08-22T20:42:56","modified_gmt":"2020-08-22T18:42:56","slug":"arrays","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/arrays\/","title":{"rendered":"Arrays"},"content":{"rendered":"<p>Arrays are the most commonly used data structure in Computer Programming and is implemented in some way in almost all programming language.<\/p>\n<p><b>Definition<\/b>:<br \/>\nAn array is a data structure that stores a collection of items under the same data and accessible through an ordered set of integers called subscripts or index. The array index is used to reference each of the array elements.<\/p>\n<p>Array may also be viewed as a collection of variables of the same data type stored in a contiguous memory location. Arrays are fixed size meaning that the number of elements are fixe at the time of declaration.<br \/>\nAn array can have one, two or more subscripts. When an array have 2 subscripts, it is said to be a 2-dimensional array. For 3 subscripts, it is 2-dimensional array.<br \/>\nIndividual elements in an array is called elements.<\/p>\n<p><b>One-Dimensional Arrays<\/b><br \/>\nOne-dimensional array also known a single-dimensional array or linear array where the elements can be accessed using a single index.<\/p>\n<p>An example of one-dimensional array is shown in Figure 1.<\/p>\n<div style=\"clear: both; text-align: center;\"><a style=\"margin-left: 1em; margin-right: 1em;\" href=\"https:\/\/4.bp.blogspot.com\/-uFpdb_E7euo\/WUrfL3nEpTI\/AAAAAAAAAH4\/eHixX-GpGfsCAofSWaKESdV5HGqwCGttgCLcBGAs\/s1600\/ARRAY.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/4.bp.blogspot.com\/-uFpdb_E7euo\/WUrfL3nEpTI\/AAAAAAAAAH4\/eHixX-GpGfsCAofSWaKESdV5HGqwCGttgCLcBGAs\/s400\/ARRAY.jpg\" width=\"400\" height=\"226\" border=\"0\" data-original-height=\"224\" data-original-width=\"396\" \/><\/a><\/div>\n<div style=\"clear: both; text-align: center;\">Figure 1: One-Dimensional Array<\/div>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<div style=\"clear: both; text-align: left;\">The array is made up of 6 elements: A[0] to A[6]. Also note that the index of an array most times start from 0;<\/div>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<div style=\"clear: both; text-align: left;\"><b>Two-Dimensional Array<\/b><\/div>\n<div style=\"clear: both; text-align: left;\">A two dimensional array has its elements reference by using two subscripts and are referred to as non-linear array. Think of a 2 by 2 matrix where the elements are arranged in rows and columns.<\/div>\n<div style=\"clear: both; text-align: left;\">Example of a two dimensional array is shown in Figure 2.<\/div>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<div style=\"clear: both; text-align: center;\"><a style=\"margin-left: 1em; margin-right: 1em;\" href=\"https:\/\/2.bp.blogspot.com\/-pOH6uJL7-fs\/WUrhWfbEoFI\/AAAAAAAAAIA\/EctQGg9CEOQM_jxMFiHQscSuSG7Tq-1yQCLcBGAs\/s1600\/Array2.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/2.bp.blogspot.com\/-pOH6uJL7-fs\/WUrhWfbEoFI\/AAAAAAAAAIA\/EctQGg9CEOQM_jxMFiHQscSuSG7Tq-1yQCLcBGAs\/s320\/Array2.jpg\" width=\"320\" height=\"294\" border=\"0\" data-original-height=\"365\" data-original-width=\"396\" \/><\/a><\/div>\n<div style=\"clear: both; text-align: center;\">Figure 2: A Two Dimensional Array<\/div>\n<div style=\"clear: both; text-align: left;\">To reference an element of a two dimensional array, you need to specify the row index and the column index.<\/div>\n<div style=\"clear: both; text-align: left;\">So the element<\/div>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<div style=\"clear: both; text-align: left;\">A[i][j] \u00a0(it could also by A(i, j) in another language)<\/div>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<div style=\"clear: both; text-align: left;\">referes \u00a0to the element at row i and column j. In the example above, A[0][3] means the element at row 0 and column 3 and that is 34.<\/div>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<div style=\"clear: both; text-align: left;\"><b>Array Declaration<\/b><\/div>\n<div style=\"clear: both; text-align: left;\">Before an array is used in a program, it is first declared. The declaration sets aside memory space for storage of the array. Array declaration \u00a0reserves contagious memory locations for the elements of the array.<\/div>\n<div style=\"clear: both; text-align: left;\">In array declaration, the following is done:<\/div>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<ul>\n<li>Specify the name of the array<\/li>\n<li>Specify the data type for the array elements<\/li>\n<li>Specify the dimension of the array<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<div style=\"clear: both; text-align: left;\">In the C programming language, an array can be declared with the following syntax:<\/div>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<div style=\"clear: both; text-align: left;\">int A[5][4];<\/div>\n<div style=\"clear: both; text-align: left;\">This means that:<\/div>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<ul>\n<li>The array has a name A<\/li>\n<li>The array elements would be integer values<\/li>\n<li>The array would have 5 rows and 4 columns<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<div style=\"clear: both; text-align: left;\">Could you work out the maximum number of elements the above array can hold?<\/div>\n<div style=\"clear: both; text-align: left;\">Simply multiply the subscripts, that is 5 x 4 = 20 elements<\/div>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<div style=\"clear: both; text-align: left;\"><b>Adding Elements to Arrays<\/b><\/div>\n<div style=\"clear: both; text-align: left;\">The computer programmer would have to supply the value of the elements of the array. To assign a value to an array, you assign value using the subscript of the array elements. For example;<\/div>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<div style=\"clear: both; text-align: left;\">A[0] = 87;<\/div>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<div style=\"clear: both; text-align: left;\">This assigns the value of 87 to the first element of the one-dimensional array A<\/div>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<div style=\"clear: both; text-align: left;\">A[1][2] = 34;<\/div>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<div style=\"clear: both; text-align: left;\">This line assigns the value of 34 to the element at the second row and third column(remember the first column is 0 an the first row is 0)<\/div>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<div style=\"clear: both; text-align: left;\">You can also assign values to the array elements using loop. The algorithm below would read input fro the user an continue to assign to the array until all the elements are assigned.<\/div>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<div style=\"clear: both; text-align: left;\">Initialize array A[10]<\/div>\n<div style=\"clear: both; text-align: left;\">Initialize i = 0<\/div>\n<div style=\"clear: both; text-align: left;\">DO<\/div>\n<div style=\"clear: both; text-align: left;\">READ VALUE FROM INPUT<\/div>\n<div style=\"clear: both; text-align: left;\">ASSIGNE TO VALUE TO A[i]<\/div>\n<div style=\"clear: both; text-align: left;\">INCREMENT i<\/div>\n<div style=\"clear: both; text-align: left;\">WHILE( i&lt;= 10) CONTINUE<\/div>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<div style=\"clear: both; text-align: left;\">The loop continues to read integer value from the input and assign to the array until all the elements of the array is filled up, that is the subscript reaches maximum.<\/div>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<div style=\"clear: both; text-align: left;\"><\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Arrays are the most commonly used data structure in Computer Programming and is implemented in some way in almost all programming language. Definition: An array &hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[35],"tags":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/273"}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=273"}],"version-history":[{"count":2,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/273\/revisions"}],"predecessor-version":[{"id":828,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/273\/revisions\/828"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}