Rrays
Rrays
All arrays consist of contiguous memory locations. The compiler will reserve a
section of the memory to hold all of the data that is defined for the array. The
lowest address corresponds to the first element and the highest address to the
last element.
Declaring Arrays
To declare an array in C++, the programmer specifies the type of the elements
and the number of elements required by an array as follows:
type arrayName [ arraySize ];
---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 1 ……………..………. Computer Programming
Initializing Arrays
You can initialize C++ array elements either one by one or using a single
statement as follows:
The number of values between braces { } can't be larger than the number of
elements that we declare for the array between square brackets [ ]. If any values
are not defined, they will be left empty. For example, the 4th and 5th elements
of the array will be empty if only 3 values were provided.
If you omit the size of the array, an array just big enough to hold the values
defined is created. Therefore, if you write:
You will create exactly the same array as you did in the previous example.
balance[4] = 50.0;
The above statement assigns the 5th element in the array a value of 50.0. There
is a 4 inside of the brackets because the base index used to define the individual
elements starts at 0. This means the highest index is always going to be one less
than the maximum size of the array. Shown below is the pictorial representation
of the same array we discussed above.
---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 2 ……………..………. Computer Programming
Accessing Array Elements
An element is accessed by indexing the array name. This is done by placing the
index of the element within square brackets after the name of the array. For
example:
The above statement will take 10th element from the array and assign the value
to the salary variable. Below is an example that declares, assigns values, and
accesses elements to an array.
#include<iostream>
using namespace std;
int main()
{ int n [10]; //n is an array of 10 integers
for(int i =0 ; i < 10 ; i++)
{
n [ i ] = i+100; //set element at location i to i+100
}
cout<<"Element Value"<<endl;
---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 3 ……………..………. Computer Programming
result:
type name[size1][size2]...[sizeN];
---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 4 ……………..………. Computer Programming
Two-Dimensional Arrays:
The simplest form of the multi-dimensional array is the two-dimensional array.
A two-dimensional array is a list of one-dimensional arrays. To declare a two-
dimensional integer array of size x, y, you would write something as follows:
Where type can be any valid C++ data type and arrayName will be avalid C++
identifier.
A two-dimensional array can be think as a table, which will have x number of
rows and y number of columns. A 2-dimensional array a, which contains three
rows and four columns can be shown as below:
---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 5 ……………..………. Computer Programming
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*/
};
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};
int val=a[2][3];
The above statement will take 4th element from the 3rd row of the array. You can
verify it in the previous diagram.
---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 6 ……………..………. Computer Programming
When the above code is compiled and executed, it produces the following
result:
---------------------------------------------------------------------------------------------------------------
Dr. Omaima Bahaidara …………..……. 7 ……………..………. Computer Programming