Chapter Five Print4
Chapter Five Print4
Declaration of Arrays
An array declaration is very similar to a variable declaration. First a type is given for the elements of
the array, then an identifier for the array and, within square brackets, the number of elements in the
array. Declaring the name and type of an array and setting the number of elements in an array is
called dimensioning the array. Like a regular variable, an array must be declared before it is used. In
the array declaration one must define:
1. The data type of the array (i.e. integer, floating point, char etc.)
2. Name of the array, arrayname must be a valid identifier and the elements field which is
always enclosed in square brackets [ ]
3. The total number of memory locations to be allocated or the maximum value of each subscript.
i.e. the number of elements in the array. So the general syntax for the declaration is:
Syntax: Datatype arrayname [array size]; or datatype arrayname [element];
The expression array size, which is the number of elements, must be a constant such as 10 or a
symbolic constant declared before the array declaration, or a constant expression such as 10*sizeof
(int), for which the values are known at the time compilation takes place.
Note: array size cannot be a variable whose value is set while the program is running.
1
Thus to declare an integer with size of 10 having a name of num is: int num [10];
This means: ten consecutive two byte memory location will be reserved with the name num. That
means, we can store 10 values of type int without having to declare 10 different variables each one
with a different identifier. Instead of that, using an array we can store 10 different values of the same
type, int for example, with a unique identifier.
Consider the following typical declaration for an array in C++ below, for example, an array to
contain 5 integer values of type int called num could be represented like this:
int num[5];
Where each blank panel represents an element of the array, that in this case are integer values of
type int. These elements are numbered from 0 to 4 since in arrays the first index is always 0,
independently of its length.
Types of ARRAYS
Single dimensional Arrays
Multidimensional Arrays
Single dimensional Arrays
Single dimensional Arrays: As its name indicates single dimensional array, is an array which has
single index size. The above Arrayname[arraySize] is example of single dimensional array. The
arraysize must be an integer constant greater than zero and type can be any valid C++ data type.
Initializing arrays
When we declare an Array, we have the possibility to assign initial values to each one of its
elements using curly brackets { }. For example: int num[5];
int num[5] = { 16, 2, 77, 40, 12071 };
The above declaration would have created an array like the following one:
2
The number of elements in the array that we initialized within curly brackets { } must be equal
or less than the length in elements that we declared for the array enclosed within square brackets [ ].
If we have less number of items for the initialization, the rest will be filled with zero. For example,
in the example of the num array we have declared that it had 5 elements and in the list of initial values
within curly brackets { } we have set 5 different values, one for each element. If we ignore the last
initial value (12071) in the above initialization, 0 will be taken automatically for the last array
element at the 4.
Because this can be considered as useless repetition, C++ allows the possibility of leaving
empty the brackets [ ], where the number of items in the initialization bracket the compiler will be
counted to set the size of the array, thus the compiler will make it large enough to hold the number
of elements in the list..
int primes[ ] = {1, 2, 3, 5, 7, 11, 13};
In this case primes would be allocated space for seven elements. If the array is given a size that size
must be greater than or equal to the number of elements initialization list within curly brackets { }.
The compiler will count the number of initialization items which is 6 and set the size of the array day
to 6 (i.e.: int prime [6]).
You can use the initialization form only when defining the array. You cannot use it later, and
cannot assign one array to another once. I.e.
int arr [] = {16, 2, 77, 40, 12071};
int ar [4];
ar[]={1,2,3,4};//not allowed
arr=ar;//not allowed
Note: when initializing an array, we can provide fewer values than the array elements. For example:
int primes [10] = {1, 2, 3, 5, 7}; in this case would reserve space for a ten element array but would
only initialize the first five elements, the compiler sets the remaining elements to zero.
Accessing and Processing Array Elements
In any point of the program in which the array is visible we can access individually anyone of its
elements for reading or modifying it as if it was a normal variable. To access individual elements,
index or subscript is used in format, name [ index ].
3
In C++ the first element has an index of 0 and the last element has an index, which is one less the
size of the array (i.e. arraysize-1). Thus, from the above declaration, int num[0] is the first element
and day[4] is the last element.
//Sample of one or single Dimensional of array
#include <iostream.h>
using namespace std;
int main() {
int num[5];
cout<<"Enter 5 numbers: "; //Storing 5 number entered by user in an array using for loop.
for (int i = 0; i < 5; ++i) {
cin>>num[i];
}
cout<<"First number: "<<num[0]<<endl; // first element of an array is num[0]
cout<<"[1]: "<<num[1]<<endl; // first element of an array is num[1]
cout<<"Last number: "<<num[4]; // last element of an array is n[SIZE_OF_ARRAY - 1]
return 0;
}
// This program display week of a day by single dimension array
#include<iostream.h>
#include<string>
using namespace std;
int main()
{
char weekdays[7]={'M','T', 'W', 'T', 'F', 'S', 'S'} ;
// Monday Tuesday Thursday Wednesday Friday Sunday Saturday
for (int i=0;i<7;i++)
if (i<=6)
cout<<'['<<i<<']'<<" " <<weekdays[i]<<endl;
cout<<endl;
return 0;
}
4
Multidimensional
Multidimensional arrays can be described as arrays of arrays. For example, a bi-dimensional array
can be imagined as a bi-dimensional table of a uniform concrete data type. For example, the following
declaration creates a three dimensional 5,10 , 4 integer array: int threedim[5][10][4]; The general
form of a multidimensional array declaration syntax:
Declaration syntax: Datatype ArrayName[size1][size2]...[sizeN];
The simplest form of the multidimensional array is the two-dimensional array.
Thus, every element in array a is identified by an element name of the form a[ i ][ j ], where a is the
name of the array, and i and j are the index(subscripts) that uniquely identify each element in a.
Initializing Two-Dimensional Arrays
Multidimensional arrays may be initialized by specifying bracketed values for each row. Following
is an array with 3 rows and each row have 4 columns.
int a[3][4] = {{0, 1, 2, 3} , {4, 5, 6, 7} , {8, 9, 10, 11} };
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */ };
5
The nested braces, which indicate the intended row, are optional. The following initialization is
equivalent to previous example:
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
To initialize a multidimensional arrays , you must assign the list of values to array elements in order,
with last array subscript changing while the first subscript while the first subscript holds steady. The
program initializes this array by writing
int num[5][3] ={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
for the sake of clarity, the program could group the initializations with braces, as shown below.
int num[5][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}, {13, 14,15} };
The compiler ignores the inner braces, which clarify how the numbers are distributed. Each value
should be separated by comma, regardless of whither inner braces are include. The entire initialization
must set must appear within braces, and it must end with a semicolon.
Omitting the Array Size(element)
If a one-dimensional array is initialized, the size(element) can be omitted as it can be found from
the number of initializing elements:
int x[] = { 1, 2, 3, 4} ;
This initialization creates an array of four elements. However impossible to omitting the array
size(element) multidimensional arrays.
int x[ ][ ] = { {1,2}, {3,4} } ; // error is not allowed.
Thus we must be write the size of an array
int x[2][2] = { {1,2}, {3,4} } ;
6
Example1:
//Sample of two-Dimensional Array
#include<iostream.h>
using namespace std;
int main() {
int a[3][4] = { {0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};
cout<<"index(Element) Values\n";
cout<<"--------------------------\n";
for(int i=0;i<3;i++){
for(int j=0;j<4;j++){
cout<<i<<j<<" "<<a[i][j]<<endl;
}
cout<<"\n";
cout<<"--------------------------\n";
}
return 0;
}