image

Consider having a program that stores the price of various items in a store. This program could have an integer variable storing the price for each item in the store. Business is bad and the store is forced to raise the price of every item in the store by $1. If each price were its own integer variable, raising the price of every item would be messy. If each price were its own variable, it would take so many lines of code to raise each price by one.

Putting the store scenario aside, there are many instances where large amounts of variables must be changed, or even compared. Having a separate name for each variable would simply be too inconvenient for the user and the coder.

Arrays are the solution to having large amounts of similar variables. An array is a data structure that collects and organizes variables under one name.

Structure of an Array

Table 1. Structure of a 6-Element Integer Array

Data Value

24

35

46

57

68

79

Index

0

1

2

3

4

5

An array is a container that stores variables of the same data type. Each individual variable in an array is called an element. Each element in an array is stored, organized, and numbered with a value called an index. An element in an array can be accessed by using the index that it is assigned. The very first element of an array will always be given the index of 0. The rest of the elements are simply numbered in order. For example, an element of index 2 would be the third element of the array.

Table 2. Array that Stores 50 Prices

Price of item ($)

3

5

6

9

12

42

9

Item number (index)

0

1

2

3

4

5

50

Using the store scenario, an array of 50 prices would not only organize all the values properly, but would also make the prices much easier to work with for a computer. When using arrays, the number of elements that are in an array will always remain constant. This means that an element cannot be added or removed from an array.

Array Initialization and Assignments

On C++, an array initialization should be formatted like the following:

<span id="L1" class="line"><span class="n">DataType</span> <span class="n">ArrayName</span><span class="p">[</span><span class="n">Size</span><span class="p">];</span></span>

This statement will tell the computer to allocate memory for Size variables of DataType type. A variable can be placed where the size of the array should be (Integer variable that is). Notice how square brackets ([ ]) are used when working with arrays.

In order to access the value that an element in an array is storing, the index of that variable must be known. In code, VariableName[n] would return the variable in the array with index n. VariableName[n] refers to a variable and operates like any other variable. For example, VariableName[0] and VariableName[1] refer to two completely different variables and can be used as two separate variables. Each element of the array can be worked with individually.

Note
Initializing an array of size n will have its last element’s index be n-1. For example, initializing an array of size 50 will create 50 elements numbered from 0 to 49. There is no element with index 50.

Since the index of an array increments by one when going through each element, arrays are often used together with for loops where the counter variable acts as the index of the element to be looked at.

Example Code:

<span id="L1" class="line"><span class="cp">#include &lt;iostream&gt;</span></span>
<span id="L2" class="line"><span class="c1">// Makes an array storing the first 5 even number</span></span>
<span id="L3" class="line"><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span></span>
<span id="L4" class="line"><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span></span>
<span id="L5" class="line">    <span class="c1">// creates integer array with 5 elements in it</span></span>
<span id="L6" class="line">    <span class="kt">int</span> <span class="n">numbers</span><span class="p">[</span><span class="mi">5</span><span class="p">];</span> <span class="c1">// size 5</span></span>
<span id="L7" class="line">    <span class="c1">// for loop to store a value in each element</span></span>
<span id="L8" class="line">    <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">5</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span></span>
<span id="L9" class="line">        <span class="c1">// gives each element in the array a value</span></span>
<span id="L10" class="line">        <span class="n">numbers</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">i</span> <span class="o">*</span> <span class="mi">2</span><span class="p">;</span></span>
<span id="L11" class="line">    <span class="p">}</span></span>
<span id="L12" class="line">    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"Array Contains: "</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span></span>
<span id="L13" class="line">    <span class="c1">// for loop to access the value of every element</span></span>
<span id="L14" class="line">    <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">5</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span></span>
<span id="L15" class="line">        <span class="c1">// outputs value of each the current element</span></span>
<span id="L16" class="line">        <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">numbers</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&lt;&lt;</span> <span class="s">" "</span><span class="p">;</span></span>
<span id="L17" class="line">    <span class="p">}</span></span>
<span id="L18" class="line">    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span></span>
<span id="L19" class="line"><span class="p">}</span></span>
Output:
Array Contains:
0 2 4 6 8

Notice how the counter variable i is also being used as an index for the array. A for loop is the ideal method for reading through all elements in an array.