Chap 0105
Chap 0105
Arrays
Arrays
• Array is a group of similar types of elements that have
contiguous memory location.
• In C++, array index starts from 0. Meaning that the first item saved
at index 0 is x[0].
• We can store only fixed set of elements in C++ array.
• They can be used to store a collection of any type of primitive data
type, including int, float, double, char, etc.
• An array in C/C++ can also store derived data types like structures,
pointers, and other data types, which is an addition.
•It is a better version of storing the data of the same size and same type.
•It enables us to collect the number of elements in it.
•Arrays can represent multiple data items of the same type using a single
name.
•Random data access
Disadvantages Of Array:
Output :
Size of arr: 20
3. Length of an array
int arr[] = { 1, 2, 3, 4,
5 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Length of an array: " << n << endl;
Output :
Length of an array: 5
Types of Arrays:
There are two types of arrays:
•One-Dimensional Arrays
•Multi-Dimensional Arrays
Multi-Dimensional Arrays
In multi-dimensional arrays, we have two categories:
•Two-Dimensional Arrays
•Three-Dimensional Arrays
1.1 Two-Dimensional Arrays
• This array involving two subscripts [] []
• They are also known as the array of the array.
• Two-dimensional arrays are divided into rows
and columns and are able to handle the data of the table.