Professional Documents
Culture Documents
We will
ARRAYS still print all the values entered but we want to print them
Introduction in reverse order from the value entered last to the value
entered first. Can we handle this restriction with only one
Throughout the first chapter, with the exception where simple data type variable? Definitely not. We need to store
we used string data types few times, we mostly dealt with all the five values first to print them in reverse order. A
simple data types such as integers, floating-point values simple data type value can be accessed by using different
and characters. Those data types can store only a single variables (such as reference variables) but it can only store
value. For the problems which we tried to solve in that one value at a time. Therefore, only one simple data type
chapter, those data types were adequate. Because we variable is not adequate to print what we wanted. Is there
mostly dealt with variables which can be fully expressed another option? Simply, we can define five variables, we
by a single value. store the five integers in them and print their values in
However, in real world problems, we may be interested in reverse order. In that case, we will have the following
some structured arrangement of those simple data types. program.
To give you a simple example, if you can structurally Program 2.2[Correct].cpp
arranged char data types, you will get a string. Those data
types which are formed by various structural arrangement
of the simple data types are termed as structured data
types. The simple data types are the building blocks of
the structured data types.
Structured data types which are formed from the same
(homogeneous) simple data type are known as arrays.
The data type an array is the same as the data type of the
building components or elements. For instance, a char
array has all its elements made from char data type. An In the above program, we worked only with five integer
int array is made from all integer data types. The same is values. Defining five variables was not that difficult. Now
true for other simple data types as well. consider yourself having such kind of problem involving
hundreds or thousands of values? Will you keep on
Before looking into the details of arrays, we will see the defining the same number of variables as the number of
scenarios where arrays become useful. For this, we will values which you want to deal with? If that is the only
start from simple programs. option we have, you will say C++ is not that decent, right?
First, let us write a program which takes five integers and For such problems, however, C++ has provided us with
which prints their values as they are entered. Very easy. If the option to define only one array variable to store and
you remember for-loop, you can only define one int manipulate a number of simple data types of the same
variable. kind. In the above program, all the data types of the
variables are of int data type. As a result, a single array can
Program 2.1[Correct].cpp
be defined and can be used to store all the five integers.
No more defining on variable for every value.
Not only does C++ allow us to store and manipulate
those values via one array variable, it also provides us with
variety of options to arrange them. Those individual
simple data types in an array, can be arranged in various
number of dimensions. For instance, you can put them in
only one dimension. You can also put them in two
dimensions with specified numbers of rows and columns.
The option to arrange them in more than two dimensions
is also there. Those arrays which are formed by arranging
In the above program, the variable x takes five integer the simple data types in only one dimension are called
values and prints them as they are entered. one-dimensional arrays. It is easy to guess what the other
arrays with more than one dimension would be called.
In the above program, the statement on line 6 defines an Passing Arrays as Parameters
int variable which is initialized to a value of 3. The In C++, arrays are passed to functions by reference
keyword const states that the value of the only. However, there is no need to put the & symbol in
arraySize variable cannot be changed in the program. the formal parameter list of the called function to express
This variable holds the sizes of the arrays. If we want to that the array is received by reference. An interesting