ARRAY IN BORLAND C++
An
array is a series of elements of the same type placed in contiguous
memory locations that can be individually referenced by adding an index
to a unique identifier.
That
means that, for example, we can store 5 values of type int in an array
without having to declare 5 different variables, each one with a
different identifier. Instead of that, using an array we can store 5
different values of the same type, int for example, with a unique
identifier.
For example, an array to contain 5 integer values of type int called billy could be represented like this:
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.
Like a regular variable, an array must be declared before it is used. A typical declaration for an array in C++ is:
type name [elements];
where
type is a valid type (like int, float...), name is a valid identifier
and the elements field (which is always enclosed in square brackets []),
specifies how many of these elements the array has to contain.
Therefore, in order to declare an array called billy as the one shown in the above diagram it is as simple as:
int billy [5];
|
Example
#include<conio.h>
#include<iostream.h>
#include<stdio.h>
main()
{
int nilai[5] = {56, 67, 57, 76, 72};
int i;
clrscr();
for(i=0; i<5; i++)
{
cout<<"Nilai Array Index ke - "<<i<<" = ";
cout<<i<<endl;
}
getch();
}
Output
Sumber (Modul C++ BSI: 102:2013)