0% found this document useful (0 votes)
48 views4 pages

Understanding Array Types in Programming

The document defines and provides examples of one, two, and three dimensional arrays in C++. It explains that an array is a group of memory locations with the same name and data type. It demonstrates how to define, initialize, access, and extract elements from one dimensional arrays. It also shows how to define two dimensional arrays with rows and columns, and access elements within a two dimensional array. Finally, it provides the syntax for defining a three dimensional array with planes, rows, and columns.

Uploaded by

Saga Putih
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views4 pages

Understanding Array Types in Programming

The document defines and provides examples of one, two, and three dimensional arrays in C++. It explains that an array is a group of memory locations with the same name and data type. It demonstrates how to define, initialize, access, and extract elements from one dimensional arrays. It also shows how to define two dimensional arrays with rows and columns, and access elements within a two dimensional array. Finally, it provides the syntax for defining a three dimensional array with planes, rows, and columns.

Uploaded by

Saga Putih
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Array

1.

An array is a group of memory locations that they all have the same name and the same data type.
Example 1:
Char Nama[4];
Char : Nama [0]

Nama[1]

Nama[2]

Nama[3]

Where Nama [0]: Name of the array is Nama, the position number ( index /sub-script) =0.
Where Nama [1]: Name of the array is Nama, index=1.
Where Nama [2]: Name of the array still Nama, index=2 and so on.
The array Nama is an one dimension array with 4 elemen,
The data store in the arrary is called Element.
In another words, the array is an indexed data sturucture that is used to store data element of the
SAME data type.
Example 2 :
Int Num [4] ;

Num[0]

Num[1]

Num[2]

Num[3]

Where Num [0] : Array name is Num, index is 0.


Num [1] : Array name is Num, index is 1.
2. One Dimensional Array Definition
Syntax : <Elemen data type> <Array Name> [ <number of array element>] ;
Example : char Nama [ 5 ] ;
3. Initializing Arrays
Example 1:
char Nama [4] = ALI ; atau char Nama [4] = {A, L, I}
A

Char : Nama [0]


Nama[1]
\0 is the null terminator.

I
Nama[2]

\0
Nama[4]

Example 2 :
int Num [4] = { 10, 20 ,30, 40} ;
10
Num[0]

20
Num[1]

30
Num[2]

40
Num[3]

Example 3 :
char Nama [ ] ;

Nama [0]

Nama[1]

Nama[2]

Nama[x]

4. Accessing Array
Accessing array means to insert element into the array for storage or to read element from the
array
Example :
char Nama [4] ;
int Nom[4];
Nama[0] = A ;
Nama[1] = L :
Nom [0] = 10 ;
Nom [1] = -22 ;
*The data type of the element you stored should be the same as the data type defined for the
array elements, otherwise, you could get unexpected results.
5.

Extracting Element from One-Dimensional Array


When we extract an element from an array, we dont remove it ! We simply COPY its value.
Example :
int Nom [ 3 ] = {10, 20,30} ;
int X ;
X = Nom [1 ] ;
cout <<X ;
Y = Nom [1] * 3 ;
cout << Y ;
cout <<Nom[1] ;
cout<<Nom[ 3 1];
Output :
X = 20
Y = 60
Nom [1] = 20
Nom [2] = 30

Two Dimensional Array


1. Two Dimensional Array Definition
Syntax :
<Elemen data type> <Array Name> [ <number of Rows>] [ <number of Columns] ;

Example : char Nom [ 3 ] [ 4 ] ;

Nom[0] [0]

Nom[0] [1]

Num[0] [2]

Num [0] [3]

Nom[1] [0]

Nom[1] [1]

Num[1] [2]

Num [1] [3]

Nom[2] [0]

Nom[2] [1]

Num[2] [2]

Num [2] [3]

Rows and Columns :

2.

Row 0
Row 2
Row 3

Row 0
Row 2
Row 3

Row 0
Row 2
Row 3

Row 0
Row 2
Row 3

Column 0
Column 0
Column 0

Column 2
Column 2
Column 2

Column 3
Column 3
Column 3

Column 4
Column 4
Column 4

Row 2
Row 3

Row 1
Row 2
Row 3

Accessing Two Dimensional Array


Example :
int Nom [3] [4] ;
Nom [0] [0] = 10 ;
Nom [0] [1] = 20 ;
cout<<Please key in data for Num[0] [2] : :
cin>>Num [0] [2] ;
10
Row 2
Row 3

20
Row 2
Row 3

Three Dimension Array


1. Three Dimensional Array Definition
Syntax :
<data type> <Array Name> [ number of Plane] [ <number of Rows>] [ <number of Columns] ;

Example : char Nom [ 5 ] [ 4 ] [3] ;

Rows

Planes
Columns
Accessing 3D Array
Plane 0 :
Nom[0][0][0] = 20;
Nom[0][1][2] = 30;
Plane 4 :
Nom[4][0][0] = 50 ;

You might also like