chapter 06 Arrays and strings
chapter 06 Arrays and strings
[CoEng1112]
Chapter Six:
Arrays and Strings
6.1 Arrays initialization and
value accessing
• Arrays are a series of elements (variables) of the
same type placed consecutively in memory that
can be individually referenced by adding an
index to a unique name. The index value starts
at 0 i.e., first value is referred.
• For example, an array to contain 5 integer
values of type int called billy could be
represented this way:
2
Cont’d
• A typical declaration for an array in C++ is:
type name [elements];
• where
– type is a valid object type (int, float...),
– name is a valid variable identifier and
– the elements field, that is enclosed within
brackets [], specifies how many of these
elements the array contains.
• Example: int billy [5];
3
Initializing arrays
• When declaring an array of local scope
(within a function), if we do not specify
otherwise, it will not be initialized, so its
content is undetermined until we store
some values in it.
• If we declare a global array (outside any
function) its content will be initialized with
all its elements filled with zeros.
4
Cont’d
• If in the global scope we declare:
int billy [5];
every element of billy will be set initialy to
0:
square brackets [ ].
Cont’d
• C++ includes the possibility of leaving the
brackets empty [ ] and the size of the
Array will be defined by the number of
values included between curly brackets { }:
• int billy [] = { 16, 2, 77, 40, 12071 };
7
Access to the values of an Array
• Format:
name[index]
• Following the previous examples in which
billy had 5 elements and each of those
elements was of type int, the name which
we can use to refer to each element is the
following:
8
Cont’d
• At this point it is important to be able to
clearly distinguish between the two uses
that brackets [ ] have related to arrays.
14
Cont’d
• Multidimensional arrays are nothing more
than an abstraction, since we can obtain
the same results with a simple array just
by putting a factor between its indices:
int jimmy [3][5]; is equivalent to
int jimmy [15]; (3 * 5 = 15)
15
// multidimensional array // pseudo-multidimensional array
#include <iostream.h> #include <iostream.h>
#define WIDTH 5 #define WIDTH 5
#define HEIGHT 3 #define HEIGHT 3
int jimmy [HEIGHT][WIDTH]; int jimmy [HEIGHT * WIDTH];
int n,m; int n,m;
int main () int main ()
{ {
for (n=0;n<HEIGHT;n++) for (n=0;n<HEIGHT;n++)
for (m=0;m<WIDTH;m++) for (m=0;m<WIDTH;m++)
{ jimmy[n][m]=(n+1)*(m+1); {
} jimmy[n * WIDTH + m] = (n+1) *
return 0; (m+1);
} }
return 0;
}
16
Cont’d
• We have used defined constants (#define)
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 by
changing the line:
#define HEIGHT 3 to
#define HEIGHT 4
• with no need to make any other
17
modifications to the program.
6.3 Arrays as parameters
• At some moment we may need to pass an
array to a function as a parameter. In C++
is not possible to pass by value a
complete block of memory as a parameter
to a function, even if it is ordered as an
array, but it is allowed to pass its address.
This has almost the same practical effect
and it is a much faster and more efficient
operation.
18
Cont’d
• In order to admit arrays as parameters the
only thing that we must do when declaring
the function is to specify in the argument
the base type for the array, an identifier
and a pair of void brackets []. For example,
the following function:
void procedure (int arg[])
• admits a parameter of type "Array of int"
called arg.
19
Cont’d
• In order to pass to this function an array
declared as:
int myarray [40];
• it would be enough to write a call like this:
procedure (myarray);
20
// arrays as parameters 5 10 15
#include <iostream.h> 2 4 6 8 10
void printarray (int arg[], int length) {
for (int n=0; n<length; n++)
cout << arg[n] << " ";
cout << "\n";
}
int main ()
{
int firstarray[] = {5, 10, 15};
int secondarray[] = {2, 4, 6, 8, 10};
printarray (firstarray,3);
printarray (secondarray,5);
return 0; 21
}
Cont’d
• As you can see, the first argument (int
arg[]) admits any array of type int,
wathever its length is. For that reason we
have included a second parameter that
tells the function the length of each array
that we pass to it as the first parameter.
This allows the for loop that prints out the
array to know the range to check in the
passed array.
22
Cont’d
• In a function declaration is also possible to
include multidimensional arrays. The
format for a tridimensional array is:
base_type[][depth][depth]
• For example,
void procedure (int myarray[][3][4])
• Notice that the first brackets [] are void
and the following ones are not. Because
the compiler must be able to determine
within the function which is the depth of
23
each additional dimension.
6.4 String initialization and
value assignment
Strings
• In C++ there is no specific elemental
variable type to store strings of characters.
In order to fulfill this feature we can use
arrays of type char, which are successions
of char elements.
For example, the following array (or string of
characters):
char jenny [20];
• can store a string up to 20 characters long.
Cont’d