<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://assela.pathirana.net/index.php?action=history&amp;feed=atom&amp;title=Template%3ACpptutorial_Arrays</id>
	<title>Template:Cpptutorial Arrays - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://assela.pathirana.net/index.php?action=history&amp;feed=atom&amp;title=Template%3ACpptutorial_Arrays"/>
	<link rel="alternate" type="text/html" href="https://assela.pathirana.net/index.php?title=Template:Cpptutorial_Arrays&amp;action=history"/>
	<updated>2026-05-13T15:05:35Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.37.0</generator>
	<entry>
		<id>https://assela.pathirana.net/index.php?title=Template:Cpptutorial_Arrays&amp;diff=3590&amp;oldid=prev</id>
		<title>Root: 1 revision(s)</title>
		<link rel="alternate" type="text/html" href="https://assela.pathirana.net/index.php?title=Template:Cpptutorial_Arrays&amp;diff=3590&amp;oldid=prev"/>
		<updated>2007-06-08T15:04:44Z</updated>

		<summary type="html">&lt;p&gt;1 revision(s)&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 15:04, 8 June 2007&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;en&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Root</name></author>
	</entry>
	<entry>
		<id>https://assela.pathirana.net/index.php?title=Template:Cpptutorial_Arrays&amp;diff=3589&amp;oldid=prev</id>
		<title>Root: /* Vectors running wild! */</title>
		<link rel="alternate" type="text/html" href="https://assela.pathirana.net/index.php?title=Template:Cpptutorial_Arrays&amp;diff=3589&amp;oldid=prev"/>
		<updated>2007-04-04T07:32:15Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Vectors running wild!&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;=Arrays and Vectors=&lt;br /&gt;
{{hbox|When you write new code in C++, use Vectors instead of arrays. Almost always they are easier to work with.}}&lt;br /&gt;
In old C the way to store a number of values of same type (say integers) is to use an Array -- which can be thought of as a line of slots that we can fill in with values. &amp;#039;&amp;#039;In general&amp;#039;&amp;#039; arrays should have fixed size that is determined during the compile time (there are ways to avoid this problem!). C++ has Vectors -- array&amp;#039;s on steroids -- think of these as expandable bags. You can load them with any number of values as you like! If you write code in C++,  you can get away without using Arrays at all. But, in old code written in C, arrays appear quite often. So, we shall cover arrays first. &lt;br /&gt;
&lt;br /&gt;
==Arrays==&lt;br /&gt;
An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.&lt;br /&gt;
&lt;br /&gt;
That means that, for example, we can store 5 values of type &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt; in an array without having to declare 5 different variables, each one with a different identifier. Instead of that, using an array we can store 5 different values of the same type, &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt; for example, with a unique identifier.&lt;br /&gt;
&lt;br /&gt;
For example, an array to contain 5 integer values of type &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt; called &amp;lt;tt&amp;gt;billy&amp;lt;/tt&amp;gt; could be represented like this:&lt;br /&gt;
&lt;br /&gt;
[[Image:9-imgarra1.gif]]&lt;br /&gt;
&lt;br /&gt;
where each blank panel represents an element of the array, that in this case are integer values of type &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;. These elements are numbered from  to &amp;lt;tt&amp;gt;4&amp;lt;/tt&amp;gt; since in arrays the first index is always , independently of its length.&lt;br /&gt;
&lt;br /&gt;
Like a regular variable, an array must be declared before it is used. A typical declaration for an array in C++ is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt; type name [elements];&amp;lt;br /&amp;gt;&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;type&amp;lt;/tt&amp;gt; is a valid type (like &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;float&amp;lt;/tt&amp;gt;...), &amp;lt;tt&amp;gt;name&amp;lt;/tt&amp;gt; is a valid identifier and the &amp;lt;tt&amp;gt;elements&amp;lt;/tt&amp;gt; field (which is always enclosed in square brackets &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;), specifies how many of these elements the array has to contain.&lt;br /&gt;
&lt;br /&gt;
Therefore, in order to declare an array called &amp;lt;tt&amp;gt;billy&amp;lt;/tt&amp;gt; as the one shown in the above diagram it is as simple as:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; billy [5];&lt;br /&gt;
 &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;NOTE&amp;#039;&amp;#039;&amp;#039;&amp;lt;nowiki&amp;gt;: The &amp;lt;/nowiki&amp;gt;&amp;lt;tt&amp;gt;elements&amp;lt;/tt&amp;gt; field within brackets &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt; which represents the number of elements the array is going to hold, must be a &amp;lt;u&amp;gt;constant&amp;lt;/u&amp;gt; value, since arrays are blocks of non-dynamic memory whose size must be determined before execution. In order to create arrays with a variable length dynamic memory is needed, which is explained later in these tutorials.&lt;br /&gt;
&lt;br /&gt;
===Initializing arrays.===&lt;br /&gt;
&lt;br /&gt;
When declaring a regular array of local scope (within a function, for example), if we do not specify otherwise, its elements will not be initialized to any value by default, so their content will be undetermined until we store some value in them. The elements of global and static arrays, on the other hand, are automatically initialized with their default values, which for all fundamental types this means they are filled with zeros.&lt;br /&gt;
&lt;br /&gt;
In both cases, local and global, when we declare an array, we have the possibility to assign initial values to each one of its elements by enclosing the values in braces &amp;lt;tt&amp;gt;{ }&amp;lt;/tt&amp;gt;. For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; billy [5] = { 16, 2, 77, 40, 12071 }; &lt;br /&gt;
 &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This declaration would have created an array like this:&lt;br /&gt;
&lt;br /&gt;
[[Image:9-imgarra3.gif]]&lt;br /&gt;
&lt;br /&gt;
The amount of values between braces &amp;lt;tt&amp;gt;{ }&amp;lt;/tt&amp;gt; must not be larger than the number of elements that we declare for the array between square brackets &amp;lt;tt&amp;gt;[ ]&amp;lt;/tt&amp;gt;. For example, in the example of array &amp;lt;tt&amp;gt;billy&amp;lt;/tt&amp;gt; we have declared that it has 5 elements and in the list of initial values within braces &amp;lt;tt&amp;gt;{ }&amp;lt;/tt&amp;gt; we have specified 5 values, one for each element.&lt;br /&gt;
&lt;br /&gt;
When an initialization of values is provided for an array, C++ allows the possibility of leaving the square brackets empty &amp;lt;tt&amp;gt;[ ]&amp;lt;/tt&amp;gt;. In this case, the compiler will assume a size for the array that matches the number of values included between braces &amp;lt;tt&amp;gt;{ }&amp;lt;/tt&amp;gt;&amp;lt;nowiki&amp;gt;:&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; billy [] = { 16, 2, 77, 40, 12071 };&lt;br /&gt;
 &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
After this declaration, array &amp;lt;tt&amp;gt;billy&amp;lt;/tt&amp;gt; would be 5 ints long, since we have provided 5 initialization values.&lt;br /&gt;
&lt;br /&gt;
===Accessing the values of an array.===&lt;br /&gt;
&lt;br /&gt;
In any point of a program in which an array is visible, we can access the value of any of its elements individually as if it was a normal variable, thus being able to both read and modify its value. The format is as simple as:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt; name[index] &amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Following the previous examples in which &amp;lt;tt&amp;gt;billy&amp;lt;/tt&amp;gt; had 5 elements and each of those elements was of type &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;, the name which we can use to refer to each element is the following:&lt;br /&gt;
&lt;br /&gt;
[[Image:9-imgarra2.gif]]&lt;br /&gt;
&lt;br /&gt;
For example, to store the value &amp;lt;tt&amp;gt;75&amp;lt;/tt&amp;gt; in the third element of &amp;lt;tt&amp;gt;billy&amp;lt;/tt&amp;gt;, we could write the following statement:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 billy[2] = 75;&lt;br /&gt;
 &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
and, for example, to pass the value of the third element of &amp;lt;tt&amp;gt;billy&amp;lt;/tt&amp;gt; to a variable called &amp;lt;tt&amp;gt;a&amp;lt;/tt&amp;gt;, we could write:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 a = billy[2];&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Therefore, the expression &amp;lt;tt&amp;gt;billy[2]&amp;lt;/tt&amp;gt; is for all purposes like a variable of type &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Notice that the third element of &amp;lt;tt&amp;gt;billy&amp;lt;/tt&amp;gt; is specified &amp;lt;tt&amp;gt;billy[2]&amp;lt;/tt&amp;gt;, since the first one is &amp;lt;tt&amp;gt;billy[0]&amp;lt;/tt&amp;gt;, the second one is &amp;lt;tt&amp;gt;billy[1]&amp;lt;/tt&amp;gt;, and therefore, the third one is &amp;lt;tt&amp;gt;billy[2]&amp;lt;/tt&amp;gt;. By this same reason, its last element is &amp;lt;tt&amp;gt;billy[4]&amp;lt;/tt&amp;gt;. Therefore, if we write billy[5], we would be accessing the sixth element of &amp;lt;tt&amp;gt;billy&amp;lt;/tt&amp;gt; and therefore exceeding the size of the array.&lt;br /&gt;
&lt;br /&gt;
In C++ it is syntactically correct to exceed the valid range of indices for an array. This can create problems, since accessing out-of-range elements do not cause compilation errors but can cause runtime errors. The reason why this is allowed will be seen further ahead when we begin to use pointers.&lt;br /&gt;
&lt;br /&gt;
At this point it is important to be able to clearly distinguish between the two uses that brackets &amp;lt;tt&amp;gt;[ ]&amp;lt;/tt&amp;gt; have related to arrays. They perform two different tasks: one is to specify the size of arrays when they are declared; and the second one is to specify indices for concrete array elements. Do not confuse these two possible uses of brackets &amp;lt;tt&amp;gt;[ ]&amp;lt;/tt&amp;gt; with arrays.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; billy[5];         &amp;lt;span class=&amp;quot;comm&amp;quot;&amp;gt;// declaration of a new array&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 billy[2] = 75;        &amp;lt;span class=&amp;quot;comm&amp;quot;&amp;gt;// access to an element of the array.&amp;lt;/span&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
If you read carefully, you will see that a type specifier always precedes a variable or array declaration, while it never precedes an access.&lt;br /&gt;
&lt;br /&gt;
Some other valid operations with arrays:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 billy[0] = a;&lt;br /&gt;
 billy[a] = 75;&lt;br /&gt;
 b = billy [a+2];&lt;br /&gt;
 billy[billy[a]] = billy[2] + 5;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;codebox&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;comm&amp;quot;&amp;gt;// arrays example&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span class=&amp;quot;prep&amp;quot;&amp;gt;&amp;lt;nowiki&amp;gt;#include &amp;lt;iostream&amp;gt;&amp;lt;/nowiki&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;using&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;namespace&amp;lt;/span&amp;gt; std;&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; billy [] = {16, 2, 77, 40, 12071};&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; n, result=0;&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; main ()&lt;br /&gt;
 {&lt;br /&gt;
   &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;for&amp;lt;/span&amp;gt; ( n=0 ; n&amp;lt;5 ; n++ )&lt;br /&gt;
   {&lt;br /&gt;
     result += billy[n];&lt;br /&gt;
   }&lt;br /&gt;
   cout &amp;lt;&amp;lt; result;&lt;br /&gt;
   &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;return&amp;lt;/span&amp;gt; 0;&lt;br /&gt;
 }&lt;br /&gt;
| class=&amp;quot;result&amp;quot; |&lt;br /&gt;
 12206&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Multidimensional arrays===&lt;br /&gt;
&lt;br /&gt;
Multidimensional arrays can be described as &amp;quot;arrays of arrays&amp;quot;. For example, a bidimensional array can be imagined as a bidimensional table made of elements, all of them of a same uniform data type.&lt;br /&gt;
&lt;br /&gt;
[[Image:9-imgarra5.gif]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;jimmy&amp;lt;/tt&amp;gt; represents a bidimensional array of 3 per 5 elements of type &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;. The way to declare this array in C++ would be:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; jimmy [3][5];&lt;br /&gt;
 &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
and, for example, the way to reference the second element vertically and fourth horizontally in an expression would be:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 jimmy[1][3]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
[[Image:9-imgarra6.gif]]&lt;br /&gt;
&lt;br /&gt;
(remember that array indices always begin by zero).&lt;br /&gt;
&lt;br /&gt;
Multidimensional arrays are not limited to two indices (i.e., two dimensions). They can contain as many indices as needed. But be careful! The amount of memory needed for an array rapidly increases with each dimension. For example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;char&amp;lt;/span&amp;gt; century [100][365][24][60][60];&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
declares an array with a &amp;lt;tt&amp;gt;char&amp;lt;/tt&amp;gt; element for each second in a century, that is more than 3 billion chars. So this declaration would consume more than 3 gigabytes of memory!&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Multidimensional arrays are just an abstraction for programmers, since we can obtain the same results with a simple array just by putting a factor between its indices:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; jimmy [3][5];   &amp;lt;span class=&amp;quot;comm&amp;quot;&amp;gt;// is equivalent to&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; jimmy [15];     &amp;lt;span class=&amp;quot;comm&amp;quot;&amp;gt;// (3 * 5 = 15)&amp;lt;/span&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
With the only difference that with multidimensional arrays the compiler remembers the depth of each imaginary dimension for us. Take as example these two pieces of code, with both exactly the same result. One uses a bidimensional array and the other one uses a simple array:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;boxed&amp;quot;&lt;br /&gt;
! multidimensional array&lt;br /&gt;
! pseudo-multidimensional array&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;#define WIDTH 5&lt;br /&gt;
 #define HEIGHT 3&lt;br /&gt;
 &lt;br /&gt;
 int jimmy [HEIGHT][WIDTH];&lt;br /&gt;
 int n,m;&lt;br /&gt;
 &lt;br /&gt;
 int main ()&lt;br /&gt;
 {&lt;br /&gt;
   for (n=0;n&amp;lt;HEIGHT;n++)&lt;br /&gt;
     for (m=0;m&amp;lt;WIDTH;m++)&lt;br /&gt;
     {&lt;br /&gt;
       jimmy[n][m]=(n+1)*(m+1);&lt;br /&gt;
     }&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;#define WIDTH 5&lt;br /&gt;
 #define HEIGHT 3&lt;br /&gt;
 &lt;br /&gt;
 int jimmy [HEIGHT * WIDTH];&lt;br /&gt;
 int n,m;&lt;br /&gt;
 &lt;br /&gt;
 int main ()&lt;br /&gt;
 {&lt;br /&gt;
   for (n=0;n&amp;lt;HEIGHT;n++)&lt;br /&gt;
     for (m=0;m&amp;lt;WIDTH;m++)&lt;br /&gt;
     {&lt;br /&gt;
       jimmy[n*WIDTH+m]=(n+1)*(m+1);&lt;br /&gt;
     }&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/nowiki&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
None of the two source codes above produce any output on the screen, but both assign values to the memory block called jimmy in the following way:&lt;br /&gt;
&lt;br /&gt;
[[Image:9-imgarra7.gif]]&lt;br /&gt;
&lt;br /&gt;
We have used &amp;quot;defined constants&amp;quot; (&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;#define&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;) to simplify possible future modifications of the program. For example, in case that we decided to enlarge the array to a height of 4 instead of 3 it could be done simply by changing the line:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;prep&amp;quot;&amp;gt;&amp;lt;nowiki&amp;gt;#define HEIGHT 3&amp;lt;/nowiki&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
to:&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;prep&amp;quot;&amp;gt;&amp;lt;nowiki&amp;gt;#define HEIGHT 4&amp;lt;/nowiki&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
with no need to make any other modifications to the program.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Arrays as parameters===&lt;br /&gt;
&lt;br /&gt;
At some moment we may need to pass an array to a function as a parameter. In C++ it is not possible to pass a complete block of memory by value as a parameter to a function, but we are allowed to pass its address. In practice this has almost the same effect and it is a much faster and more efficient operation.&lt;br /&gt;
&lt;br /&gt;
In order to accept arrays as parameters the only thing that we have to do when declaring the function is to specify in its parameters the element type of the array, an identifier and a pair of void brackets &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt;. For example, the following function:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;void&amp;lt;/span&amp;gt; procedure (&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; arg[])&lt;br /&gt;
 &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
accepts a parameter of type &amp;quot;array of &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;&amp;quot; called &amp;lt;tt&amp;gt;arg&amp;lt;/tt&amp;gt;. In order to pass to this function an array declared as:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; myarray [40];&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
it would be enough to write a call like this:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 procedure (myarray);&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Here you have a complete example:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;codebox&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;comm&amp;quot;&amp;gt;// arrays as parameters&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span class=&amp;quot;prep&amp;quot;&amp;gt;&amp;lt;nowiki&amp;gt;#include &amp;lt;iostream&amp;gt;&amp;lt;/nowiki&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;using&amp;lt;/span&amp;gt; &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;namespace&amp;lt;/span&amp;gt; std;&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;void&amp;lt;/span&amp;gt; printarray (&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; arg[], &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; length) {&lt;br /&gt;
   &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;for&amp;lt;/span&amp;gt; (&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; n=0; n&amp;lt;length; n++)&lt;br /&gt;
     cout &amp;lt;&amp;lt; arg[n] &amp;lt;&amp;lt; &amp;lt;span class=&amp;quot;str&amp;quot;&amp;gt;&amp;quot; &amp;quot;&amp;lt;/span&amp;gt;&amp;lt;nowiki&amp;gt;;&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;lt;/nowiki&amp;gt;&amp;lt;span class=&amp;quot;str&amp;quot;&amp;gt;&amp;quot;\n&amp;quot;&amp;lt;/span&amp;gt;&amp;lt;nowiki&amp;gt;;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;/nowiki&amp;gt;&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; main ()&lt;br /&gt;
 {&lt;br /&gt;
   &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; firstarray[] = {5, 10, 15};&lt;br /&gt;
   &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; secondarray[] = {2, 4, 6, 8, 10};&lt;br /&gt;
   printarray (firstarray,3);&lt;br /&gt;
   printarray (secondarray,5);&lt;br /&gt;
   &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;return&amp;lt;/span&amp;gt; 0;&lt;br /&gt;
 }&lt;br /&gt;
| class=&amp;quot;result&amp;quot; |&lt;br /&gt;
 5 10 15&lt;br /&gt;
 2 4 6 8 10&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
As you can see, the first parameter (&amp;lt;tt&amp;gt;int arg[]&amp;lt;/tt&amp;gt;) accepts any array whose elements are of type &amp;lt;tt&amp;gt;int&amp;lt;/tt&amp;gt;, whatever its length. For that reason we have included a second parameter that tells the function the length of each array that we pass to it as its first parameter. This allows the &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop that prints out the array to know the range to iterate in the passed array without going out of range.&lt;br /&gt;
&lt;br /&gt;
In a function declaration it is also possible to include multidimensional arrays. The format for a tridimensional array parameter is:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 base_type[][depth][depth]&lt;br /&gt;
 &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
for example, a function with a multidimensional array as argument could be:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;snippet&amp;quot;&lt;br /&gt;
| class=&amp;quot;code&amp;quot; |&lt;br /&gt;
 &amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;void&amp;lt;/span&amp;gt; procedure (&amp;lt;span class=&amp;quot;kw&amp;quot;&amp;gt;int&amp;lt;/span&amp;gt; myarray[][3][4])&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Notice that the first brackets &amp;lt;tt&amp;gt;[]&amp;lt;/tt&amp;gt; are left blank while the following ones are not. This is so because the compiler must be able to determine within the function which is the depth of each additional dimension.&lt;br /&gt;
&lt;br /&gt;
Arrays, both simple or multidimensional, passed as function parameters are a quite common source of errors for novice programmers. I recommend the reading of the chapter about Pointers for a better understanding on how arrays operate.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Vectors -- Arrays made easy==&lt;br /&gt;
Arrays are simple as long as their dimensions are fixed. What if the size of the array is determined by the data? While [[C++ Dynamic Memory -- Advanced|there are ways]] to overcome this issue, they tend to be somewhat complicated. An alternative is to use the vector structure that is available in C++. Vectors are way easier to use than traditional arrays. Let&amp;#039;s start with an example. &lt;br /&gt;
&amp;lt;source lang=cpp&amp;gt;&lt;br /&gt;
/** Vector demonstration I&lt;br /&gt;
**/&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
&lt;br /&gt;
int main ()&lt;br /&gt;
{&lt;br /&gt;
	vector&amp;lt;string&amp;gt; animals;&lt;br /&gt;
	do{&lt;br /&gt;
		string animal;&lt;br /&gt;
		cout &amp;lt;&amp;lt; &amp;quot;An animal (Just Enter to end):&amp;quot;;&lt;br /&gt;
		getline(cin, animal);&lt;br /&gt;
		if(animal==&amp;quot;&amp;quot;){&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
		animals.push_back(animal);&lt;br /&gt;
	}while(true);&lt;br /&gt;
	cout &amp;lt;&amp;lt; &amp;quot;I got the following:\n&amp;quot;;&lt;br /&gt;
	for(unsigned int i=0;i&amp;lt;animals.size();i++){&lt;br /&gt;
		cout &amp;lt;&amp;lt; animals[i]&amp;lt;&amp;lt;&amp;#039;\n&amp;#039;;&lt;br /&gt;
	}&lt;br /&gt;
		&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
An animal (Just Enter to end):rabbit&lt;br /&gt;
An animal (Just Enter to end):fox&lt;br /&gt;
An animal (Just Enter to end):chicken&lt;br /&gt;
An animal (Just Enter to end):&lt;br /&gt;
I got the following:&lt;br /&gt;
rabbit&lt;br /&gt;
fox&lt;br /&gt;
chicken&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
{{hbox|Almost always, vectors are better substitutes for arrays. Learn to use them effectively. However, one situation where you may have to use arrays is when dealing with C language (prior to C++) code libraries.}}&lt;br /&gt;
Let&amp;#039;s go through this code:&lt;br /&gt;
;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;#include &amp;lt;vector&amp;gt;&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;: directive needed (to include vector headers) if you want to use vectors. &lt;br /&gt;
;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;vector&amp;lt;string&amp;gt; animals;&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;: animals is a vector, with string elements. (You can define vectors with any type of elements. e.g. &amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;vector&amp;lt;int&amp;gt; age;&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;. &lt;br /&gt;
;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;animals.push_back(animal);&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;: add the string stored in animal variable to animals vector. (Vector will grow by one element.)&lt;br /&gt;
;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;animals.size()&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;: The size of the vector. (i.e. number of elements.)&lt;br /&gt;
;&amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;animals[i]&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;: Elements of vectors can be accessed using the same notation that we use for arrays.  (Alternatively you can use &amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;animals.at(i)&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt;.)&lt;br /&gt;
&lt;br /&gt;
===Vectors of Vectors===&lt;br /&gt;
It is possible to define vectors of vectors (of vectors ...) as follows. &lt;br /&gt;
&amp;lt;source lang=cpp&amp;gt;&lt;br /&gt;
vector &amp;lt; vector &amp;lt;int&amp;gt; &amp;gt; matrix; //defines a vector of vector of integers. &lt;br /&gt;
&amp;lt;/source&amp;gt; &lt;br /&gt;
Following is an example of vector of vectors in use. &lt;br /&gt;
&amp;lt;source lang=cpp&amp;gt;&lt;br /&gt;
/* vectors of vectors */&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;sstream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
&lt;br /&gt;
int main ()&lt;br /&gt;
{&lt;br /&gt;
	vector &amp;lt; vector &amp;lt;double&amp;gt; &amp;gt; matrix; //matrix is a vector of, vector of doubles.&lt;br /&gt;
	vector &amp;lt;double&amp;gt; line;              //line is a vector of doubles&lt;br /&gt;
	matrix.push_back(line);			   //add a &amp;#039;line&amp;#039; to the &amp;#039;matrix&amp;#039;&lt;br /&gt;
	cout &amp;lt;&amp;lt; &amp;quot;Enter your matrix. One number at a time.\n&amp;quot;;&lt;br /&gt;
	cout &amp;lt;&amp;lt; &amp;quot;&amp;#039;Enter&amp;#039; to break current line.\n&amp;quot;;&lt;br /&gt;
	cout &amp;lt;&amp;lt; &amp;quot;x&amp;#039;Enter&amp;#039; to end entering.\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
	int i=0,j=0;&lt;br /&gt;
	while(true){&lt;br /&gt;
		string mystr;&lt;br /&gt;
		getline (cin,mystr); //read what was entered. &lt;br /&gt;
		if(mystr==&amp;quot;&amp;quot;){       // if it is blank (just Enter)&lt;br /&gt;
			j++;                   // increase the row count. &lt;br /&gt;
			matrix.push_back(line);// add a row to the matrix.&lt;br /&gt;
			cout &amp;lt;&amp;lt; &amp;quot;Enter next row.\n&amp;quot;;&lt;br /&gt;
			continue;	     // no need to waste time, start next iteration. &lt;br /&gt;
		}&lt;br /&gt;
		if(mystr==&amp;quot;x&amp;quot;){	     // if it is &amp;quot;x&amp;quot;&lt;br /&gt;
			break;           // we are out!&lt;br /&gt;
		}&lt;br /&gt;
		// now we assume its a number. &lt;br /&gt;
		//In reality we need a bit of error handling.&lt;br /&gt;
		//But let&amp;#039;s keep things simple here. &lt;br /&gt;
		double tmp;&lt;br /&gt;
		stringstream(mystr) &amp;gt;&amp;gt; tmp; //convert mystr to a double and store in tmp&lt;br /&gt;
		matrix[j].push_back(tmp);   //add that double (tmp) to row j of matrix.&lt;br /&gt;
	}&lt;br /&gt;
	cout &amp;lt;&amp;lt; &amp;quot;Done entering!\n You entered the following matrix.\n&amp;quot;;&lt;br /&gt;
	for (unsigned int j=0;j&amp;lt;matrix.size();j++){// for each row in matrix&lt;br /&gt;
		for(unsigned int i=0;i&amp;lt;matrix[j].size();i++){// for each place in jth row.&lt;br /&gt;
			cout &amp;lt;&amp;lt; matrix[j][i] &amp;lt;&amp;lt;&amp;#039;\t&amp;#039;;&lt;br /&gt;
		}&lt;br /&gt;
		cout &amp;lt;&amp;lt; &amp;#039;\n&amp;#039;;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A typical run of this program would look like  the following: &lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
Enter your matrix. One number at a time.&lt;br /&gt;
&amp;#039;Enter&amp;#039; to break current line.&lt;br /&gt;
x&amp;#039;Enter&amp;#039; to end entering.&lt;br /&gt;
2&lt;br /&gt;
3&lt;br /&gt;
&lt;br /&gt;
Enter next row.&lt;br /&gt;
4&lt;br /&gt;
5&lt;br /&gt;
6&lt;br /&gt;
7&lt;br /&gt;
8&lt;br /&gt;
9&lt;br /&gt;
&lt;br /&gt;
Enter next row.&lt;br /&gt;
1&lt;br /&gt;
&lt;br /&gt;
Enter next row.&lt;br /&gt;
25&lt;br /&gt;
26&lt;br /&gt;
x&lt;br /&gt;
Done entering!&lt;br /&gt;
 You entered the following matrix.&lt;br /&gt;
2       3&lt;br /&gt;
4       5       6       7       8       9&lt;br /&gt;
1&lt;br /&gt;
25      26&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Vectors running wild!===&lt;br /&gt;
{{wbox|If you reuse a vector, first you need to explicitly remove all stuff. Remember to empty your bags before refilling them!}}&lt;br /&gt;
Think of vectors as bags of unlimited space. They are very convenient because you don&amp;#039;t have to know what is the number of items you are going to fill them with in advance, they just keep on growing!! However, this same property can lead to problems if you don&amp;#039;t pay attention. One of the common mistakes made by new programmers is forgetting to empty the vector (bag) before putting new set of items (refilling the bag!). &lt;br /&gt;
&lt;br /&gt;
Lets suppose you write a program where you use an array/a vector to store a number of items. Let&amp;#039;s say, within the program you do it several times. In arrays when you do the following: &lt;br /&gt;
&amp;lt;source lang=cpp&amp;gt; &lt;br /&gt;
int vals[5];&lt;br /&gt;
...&lt;br /&gt;
vals[i]=5;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
we explicitly say &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;replace the slot number &amp;lt;tt&amp;gt;i&amp;lt;/tt&amp;gt; of &amp;lt;tt&amp;gt;vals&amp;lt;/tt&amp;gt; with value &amp;lt;tt&amp;gt;5&amp;lt;/tt&amp;gt;&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;. &lt;br /&gt;
But in vectors&lt;br /&gt;
&amp;lt;source lang=cpp&amp;gt;&lt;br /&gt;
vector &amp;lt;int&amp;gt; vals;&lt;br /&gt;
...&lt;br /&gt;
vals.push_back(5);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
what we say is &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;add the value &amp;lt;tt&amp;gt;5&amp;lt;/tt&amp;gt; to the bag &amp;lt;tt&amp;gt;vals&amp;lt;/tt&amp;gt;&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;. Notice that if we don&amp;#039;t need the old values, we have to explicitly erase them! &lt;br /&gt;
&lt;br /&gt;
You can erase a whole vector by &lt;br /&gt;
&amp;lt;source lang=cpp&amp;gt;&lt;br /&gt;
vals.erase();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Root</name></author>
	</entry>
</feed>