An array (DEF) is a data structure in which all elements are of the same data type as well as contiguous in memory. Contiguous refers to the fact that the spaces in memory are located next to one another.
General syntax:
DataType ArrayName[SIZE];
Consider the result in memory when one integer variable is declared.
int A;
_
A --> |_|
We can declare more than one space in memory with an array.
int Array[5];
_
Array --> |_|
|_|
|_|
|_|
|_|
In this example we have set aside five spaces in memory for five separate integers.
We are allowed to initialize at declaration just as with single variables. Syntax for initialization is as follows:
DataType ArrayName[SIZE] = {DataType values separated by commas};
Consider an example.
int A[5] = {-1, 5, 29, -12, 4};
This declaration looks as thus in memory.
___
A --> |-1 |
|___|
| 5 |
|___|
| 29|
|___|
|-12|
|___|
| 4 |
|___|
We can also leave the SIZE of the array blank as long as we initialize the array. Initialization at declaration will automatically set aside the correct number of spaces in memory.
The following declarations are equivalent:
int A[5] = {-1, 5, 29, -12, 4};
int A[] = {-1, 5, 29, -12, 4};
An array is an important data structure because it provides random access to any of the cells. We can access any of the cells via the following syntax.
ArrayName[EXPRESSION_THAT_EVALS_2_INTEGER]
For instance, if we want to access cell 3 in array A we write
A[3]
Therefore in an assignment statement, we may write
A[3] = 0;
This assigns cell 3 of array A the
value 0.
For clarity consider our previous example and its corresponding indices and values.
int A[5] = {-1, 5, 29, -12, 4};
___
index
A[index]
A --> |-1 |
|___|
0
-1
| 5 |
|___|
1
5
| 29|
|___|
2
29
|-12|
|___|
3
-12
| 4 |
|___|
4
4
Note that the indexing of the array starts at 0.
The difference between index and A[index]
should become clear if you consider a character array. In that situation,
a character is certainly different than the integer index.
int index;
int A[5] = {-1, 5, 29, -12,
4};
for (index = 0; index < 5;
index++)
cout << A[index];
The index starts at 0 and continues
until less than the length of the array. An equivalent comparison
is (index <= 4). However, for continuity, we use < and the
number of cells in the array. This will hopefully alleviate confusion
with the index beginning at 0 instead of 1.
1) A compilation error may result.
2) A runtime error may result.
If a runtime error occurs, you may receive
(a) invalid data
or (b) a possible program crash.
Consider this out of bounds example.
int index;
int A[] = {-1, 5, 29, -12,
4};
for (index = 0; index <=
5; index++)
cout << A[index];
At some point in this loop, A[5] is accessed. Index 5 is out of bounds.
You can think of this phenomenon as
being on a game show. If you win plane tickets on a game show, often
times the tickets are only valid within the continental US, you can not
go to Alaska or Hawaii. This is true with arrays, stay within the
continental US: no luaus for you!
int Matrix[3][4];
^ ^
| |
rows ---|
|--- columns
Theoretically, we create a matrix that looks as thus.
_ _ _ _
Matrix ------> |_|_|_|_|
|_|_|_|_|
|_|_|_|_|
This matrix has 3 rows and 4 columns.
However, in memory, this declaration looks as follows.
_
A --> |_|
<-- A[0][0]
|_|
|_|
|_| <-- A[0][3]
|_| <-- A[1][0]
|_|
|_|
|_| <-- A[1][3]
|_| <-- A[2][0]
|_|
|_|
|_| <-- A[2][3]
Even though we are dealing with more than one dimension, the index starts at 0 for both dimensions of the matrix.
Consider the code for pretty-printing a matrix to the screen.
int index1, index2;
int A[][] = { {-1, 5, 29},
// Initialize and declare a matrix
{-12, 4, 6},
{0, 90, -2} };
for (index1 = 0; index1 <
3; index1++) { // Move down the rows
for (index2 = 0;
index2 < 3; index2++) // Move across the columns
cout
<< A[index1][index2] << " "; //Print cell followed by a space
cout << endl;
}
In this example, we have initialized the matrix at declaration. For each row, we visit each cell in that row; that is why we require the nested loops.
Loop 1 (outer)
| _ _ _ _ _ _ _ _ _ _
| |_|_|_|_|_|_|_|_|_|_|
v |_|_|_|_|_|_|_|_|_|_|
| |_|_|_|_|_|_|_|_|_|_|
| |_|_|_|_|_|_|_|_|_|_|
v |_|_|_|_|_|_|_|_|_|_|
| |_|_|_|_|_|_|_|_|_|_|
| |_|_|_|_|_|_|_|_|_|_|
v |_|_|_|_|_|_|_|_|_|_|
Loop 2 (inner) --->--->--->-->
Consider two strings declarations and initialization (initialize strings by placing a "sentence" in double quotes as below).
char s1[] = "Hi There."; // Size is not specified
char s2[10] = "Example" // Size specified
In memory, we have respectively,
___
___
s1 --> | H |
s2 --> | E |
|___|
|___|
| i |
| x |
|___|
|___|
| |
| a |
|___|
|___|
| T |
| m |
|___|
|___|
| h |
| p |
|___|
|___|
| e |
| l |
|___|
|___|
| r |
| e |
|___|
|___|
| e |
|\0 |
|___|
|___|
| . |
| |
|___|
|___|
|\0 |
| |
|___|
|___|
For s1, space in memory allocated to contain the characters in quotes including the terminating character.
For s2, since we specified the size
of the array we have allocated ten spaces in memory. Therefore, we
have two more spaces after the terminating character.
This library contains some useful functions,
as
1) int strlen(char s[])
2) void strcpy(char s1[], char
s2[])
3) int strcmp(char s1[], char
s2[])
The strlen() function returns the integer length of a string.
The strcpy() functions copies the contents of one string into some destination string.
The strcmp() function compares two
strings and returns an integer value
according to the following table.
Comparison Returned
s1 > s2
1
s1 == s2
0
s1 < s2
-1
To understand strings (and work with arrays a bit more), let's print by several methods.
#include <iostream.h>
#include <string.h>
int main()
{
int index;
char s[] = "Hello";
// Print the string using cout;
pretty simple
cout << s << endl;
// Print the string using the
strlen()
// function as a loop condition
// It is important to note
that here we are comparing integers.
for (index = 0; index <
strlen(s); index++)
cout << s[index];
cout << endl;
// Print the string using the
string terminating
// character as loop condition
// It is important to note
that here we are comparing characters.
for (index = 0; s[index] !=
'\0'; index++)
cout << s[index];
cout << endl;
return 0;
}
Output:
Hello
Hello
Hello