Introduction To Arrays
Introduction To Arrays
Arrays Fundamentals
Introduction
An array is a group of items that can be identified as similar because they are of the same nature.
Arrays come in two flavors: one dimensional and multi-dimensional arrays. Everyone of the pictures
above represents a single dimensional array.
Declaring an Array
Just like any variable you are already familiar with, an array has to be declared before being used. Yet
the difference this time is that you need to tell the compiler what kind of array you are defining, an
array of books? An array of students? An array of billiard balls? An arrays of clothes? This is because,
once more, the compiler wants to know how much space your array is going to occupy in the
computer memory. This is because when you declare an array of items, the compiler puts each one of
the items in an appropriate location.
DataType ArrayName[dimension\order]
The array is first identified by its kind, which could be a char, an int, a float, etc; followed by its name
that follows the C++ naming rules. The name is then followed by square brackets that specify the
dimension of the array or its size.
int age[12];
float grade[100];
double angle[360];
int Age[12]; declares a group or array of 12 values, each one being an integer.
double Angle[360]; declares an array of double-precision numbers. There are 360 of these items in the
group.
Initializing an Array
Just like any variable can be initialized, an array also can be initialized. To accomplish this, for a one-
dimensional array, the syntax used is:
Therefore, you can start with the data type to specify the kind of array you are declaring. This is
followed by the array name, and the square brackets. After specifying the dimension or not, and after
the closing square bracket, type the assignment operator. The elements, also called items, that compose
the array are included between an opening curly bracket '{' and a closing curly bracket '}'. Each item is
separate from the next by a comma operator. As a normal C/C++ initialization, you end it with a semi-
colon.
int number[12] = {18, 42, 25, 12, 34, 15, 63, 72, 92, 26, 26, 12};
double distance[5] = {44.14, 720.52, 96.08, 468.78, 6.28};
If you have decided to initialize the array while you are declaring it, you can omit the dimension.
Therefore, these arrays can be declared as follows:
int number[] = {18, 42, 25, 12, 34, 15, 63, 72, 92, 26, 26, 12};
double distance[] = {44.14, 720.52, 96.08, 468.78, 6.28};
Processing the Elements of an Array
After initializing an array, its elements are counted from left to right. Each element of the array, also
called a member of the array, has a specific and constant position. The position of an item is also called
its index. The first member of the array, the most left, has an index of 0. The second member of the
array has an index of 1. Since each array has a number of items which can be specified as n, the last
member of the array has an index of n-1
Based on this system of indexing, to locate a member of an array, use its index in the group. Imagine
you declare and initialize an array as follows:
To locate the value of the 3rd member of the array, you would type distance[2]. In the same way, the 1st
member of the array can be located with distance[0].
Once you can locate a member of the array, you can display its value using cout. Here is an example:
#include <iostream>
using namespace std;
int main()
{
double distance[] = {44.14, 720.52, 96.08, 468.78, 6.28};
return 0;
}
Using this approach, each member of the array can have its value accessed. Here is an example:
#include <iostream>
using namespace std;
int main()
{
double distance[] = {44.14, 720.52, 96.08, 468.78, 6.28};
return 0;
}
Distance 1: 44.14
Distance 2: 720.52
Distance 3: 96.08
Distance 4: 468.78
Distance 5: 6.28
The Size of an Array
When declaring an array, we saw that you must specify the number of items that the array is made of.
Here is an example:
float averagePrice[45];
Depending on how you want to deal with your array, you may sometimes need to increase or decrease
its dimension. To do this, you would need to locate the declaration of the array and change its
dimension. If the program is long and the array is declared in some unusual place, this could take some
time. The alternative is to define a constant prior to declaring the array and use that constant to hold the
dimension of the array. Here is an example:
#include <iostream>
using namespace std;
int main()
{
const int numberOfItems = 5;
double distance[numberOfItems] = {44.14, 720.52, 96.08, 468.78, 6.28};
return 0;
}
You can use such a constant in a for loop to scan the array and access each of its members. Here is an
example:
#include <iostream>
using namespace std;
int main()
{
const int numberOfItems = 5;
double distance[numberOfItems] = {44.14, 720.52, 96.08, 468.78, 6.28};
return 0;
}
int number[] = {18, 42, 25, 12, 34, 15, 63, 72, 92, 26, 26, 12, 127, 4762, 823,
236, 84, 5};
Instead of counting the number of members of this array (it makes me dizzy when I try), you can use
the sizeof operator as follows:
int main()
{
double distance[] = {44.14, 720.52, 96.08, 468.78, 6.28};
// Using the sizeof operator to get the dimension of the array
int index = sizeof(distance) / sizeof(double);
return 0;
}
When you declare an array without initializing it, we have mentioned that the compiler reserves an
amount of memory space for the members of the array. But that is only what the compiler does. Each
part of such reserved space is filled with garbage. Therefore, you must make sure that you know the
value held by a member of the array before making any attempt to process the value held by that
member of the array. Consider the following example:
#include <iostream>
using namespace std;
int main()
{
const int numberOfItems = 5;
double distance[numberOfItems];
return 0;
}
Distance 1: -9.25596e+061
Distance 2: -9.25596e+061
Distance 3: -9.25596e+061
Distance 4: -9.25596e+061
Distance 5: -9.25596e+061
As you can see, the members of the array in the beginning don't have any recognizable value. There
are two solutions to this problem. You can either initialize the array or request the values of the
members of the array from the user.
So far, when we used an array, we made sure to provide the exact number of members we needed for
the array. We also saw that we could declare and initialize an array without specifying its dimension.
The advantage of not specifying the dimension of the array is that we trust the compiler to find out the
number of elements of the array. If you decide to specify the dimension of the array and initialize it,
make sure you specify the elements less than or equal to the number you specified. Here is an
example:
#include <iostream>
using namespace std;
int main()
{
const int numberOfItems = 5;
double distance[numberOfItems] = {44.14, 720.52, 96.08};
return 0;
}
Distance 1: 44.14
Distance 2: 720.52
Distance 3: 96.08
Distance 4: 0
Distance 5: 0
If you provide more members than the number of elements you specified, the compiler would provide
garbage values to the extra members. Here is an example:
#include <iostream>
using namespace std;
int main()
{
const int NumberOfItems = 5;
double distance[NumberOfItems] = {44.14, 720.52, 96.08, 468.78, 6.28};
return 0;
}
Distance 1: 44.14
Distance 2: 720.52
Distance 3: 96.08
Distance 4: 468.78
Distance 5: 6.28
Distance 6: 2.64214e-308
Distance 7: 2.12414e-314
Distance 8: 1.00532e-307
Depending on the compiler you are using, you would also receive a (strong) warning.
Streaming Array Members
We have already seen how to define an array, how to locate the elements of an array, and how to
display the values of these elements (displaying the value of a member of an array is one aspect of
streaming). The arrays we have seen so far had their dimensions and their elements defined by the
programmer. Many times you will have to get these elements from the user.
When you need to get an array from the user, first decide on what kind of array it is. Next, try to think
of the maximum number of members you will need for the array. When you define an array and
specify its dimension, the compiler will reserve the number of cells in memory that can accommodate
your array. Here is an example:
int Page[5];
Each member of the array can be located using its index, as we have seen so far. In the same way, you
can request the value of any member of the array using its index. In the following example, we declare
an array of 5 integers and then we request the values of the 1st and the 4th members:
#include <iostream>
using namespace std;
int main()
{
const int counter = 5;
int page[counter];
return 0;
}
Summary of books
Book 1: 842 pages
Book 4: 1204 pages
Operations on Arrays
Each member of an array is a pseudo-variable and can be processed as such. This means that you can
add the values of two members of the array(Number[2]+Number[0]), you can subtract the value of
one of the members from another member(member[1]-Number[4]). In the same way, you can perform
multiplication, division, or remainder operations on members of an array.
One of the regular operations performed on an array consists of adding the values of the members to
produce a sum. Here is an example:
#include <iostream>
using namespace std;
int main()
{
// We know that we need a constant number of elements
const int max = 10;
int number[max];
cout << "\n\nThe sum of these numbers is " << Sum << "\n\n";
return 0;
}
int main()
{
// The members of the array
int numbers[] = {8, 25, 36, 44, 52, 60, 75, 89};
int minimum = numbers[0];
int a = 8;
return 0;
}
You can use this same approach to get the maximum value of the members of an array. Here is an
example:
int main()
{
// The members of the array
int numbers[] = {8, 25, 36, 44, 52, 60, 75, 89};
int maximum = numbers[0];
int a = 8;
return 0;
}
Arrays and Functions
An array can be passed to a function as argument. An array can also be returned by a function. To
declare and define that a function takes an array as argument, declare the function as you would do for
any regular function and, in its parentheses, specify that the argument is an array. Here is an example:
#include <iostream>
using namespace std;
int main()
{
const int numberOfItems = 5;
double distance[numberOfItems] = {44.14, 720.52, 96.08, 468.78, 6.28};
return 0;
}
#include <iostream>
using namespace std;
int main()
{
const int NumberOfItems = 5;
double distance[NumberOfItems] = {44.14, 720.52, 96.08, 468.78, 6.28};
return 0;
}
We have already seen that when you declare an array, the compiler reserves an amount of memory
space for the members of the array. To locate these members, the compiler aligns them in a
consecutive manner. For example (hypothetically), if a member is located at 1804 Lockwood drive,
the next member would be located at 1805 Lockwood Drive. This allows the compiler not only to
know where the members of a particular array are stored, but also in what block (like the block houses
of a city) the array starts. This means that, when you ask the compiler to locate a member of an array,
the compiler starts where the array starts and moves on subsequently until it finds the member you
specified. If the compiler reaches the end of the block but doesn't find the member you specified, it
stops, instead of looking for it all over the computer memory.
Based on this, when you call a function that has an array as argument, the compiler only needs the
name of the array to process it. Therefore, the above function can be called as follows:
#include <iostream>
using namespace std;
int main()
{
const int numberOfItems = 5;
double distance[numberOfItems] = {44.14, 720.52, 96.08, 468.78, 6.28};
int main()
{
const int NumberOfItems = 5;
double distance[NumberOfItems] = {44.14, 720.52, 96.08, 468.78, 6.28};
return 0;
}
Unfortunately, this program will not compile. Remember, we saw that the compiler wants only the
name of the array, because the name by itself represents the whole array. distance[3] is a specific value
of a member of the array, it is not a group of values. In other words, distance[3] is the same as 468.78.
It is as if we want to pass 468.78 as the value to be treated and not as a subsequent group of values,
because that is what an array is. Therefore, the compiler complains that you are passing a value after
you have specifically stated that the argument was a group of values and not a single value.
When you declare and define a function that takes an array as argument, if you plan to process the
array, for example, if you want the calling function to control the number of elements to be processed,
you should/must pass another argument that will allow the function to know how many members of
the array would be considered. Such a function can be declared as follows:
#include <iostream>
using namespace std;
return 0;
}
// This function process an array but starts and ends at specific positions
void DisplayTheArray(double mbr[], int Start, int End);
int main()
{
double distance[] = {44.14, 720.52, 96.08, 468.78, 6.28, 68.04,
364.55, 6234.12};
return 0;
}
// This function process an array but starts and ends at specific positions
void DisplayTheArray(double[], int, int);
int main()
{
double distance[] = {44.14, 720.52, 96.08, 468.78, 6.28, 68.04,
364.55, 6234.12};
return 0;
}
Like the above table, a 2-dimensional array is made rows and columns. To declare it, use double pair
of a opening and closing square brackets. Here is an example:
int numberOfStudentsPerClass[12][50];
This declaration creates a first group of 12 elements; it could be an array of 12 classes. Each element
of the array contains 50 elements. In other words, each of the 12 members of the group is an array of
50 items. Simply stated, this declarations creates 12 classes and each class contains 50 students.
Before using the members of an arrays, you should/must make sure you know the values that its
members hold. As done with one-dimensional arrays, there are two ways you can solve this problem:
you can initialize the array or you can get its values by another means.
You can initialize an array the same way you would proceed the a one-dimensional array: simply
provide a list of values in the curly brackets. A multidimensional array is represented as an algebraic
matrix as MxN. This means that the array is made of M rows and N columns. For example, a 5x8
matrix is made of 5 rows and 8 columns. To know the actual number of members of a
multidimensional array, you can multiply the number of rows by the number of columns. Therefore a
2x16 array contains 2*16=32 members.
Based on this, when initializing a 2-dimensional array, make sure you provide a number of values that
is less than or equal to the number of members.
Here is an example:
int main()
{
// A 2-Dimensional array
double distance[2][4] = {44.14, 720.52, 96.08, 468.78, 6.28, 68.04,
364.55, 6234.12};
int main()
{
// A 2-Dimensional array
double distance[][4] = {
{ 44.14, 720.52, 96.08, 468.78 },
{ 6.28, 68.04, 364.55, 6234.12 }
};
Multidimensional Arrays