0% found this document useful (0 votes)
12 views9 pages

Chap 0105

Uploaded by

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

Chap 0105

Uploaded by

udyadav430
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Lecture – 5

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.

The array representation in a picture is provided below.


Advantages of Array

•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:

•In an array, it is essential to identify the number of elements to be stored.


•It is a static structure. It means that in an array, the memory size is fixed.
•When it comes to insertion and deletion, it is a bit difficult because the
elements are stored sequentially and the shifting operation is expensive.
Initialization of Array in C++

1. Initialize Array with Values in C++


int arr[5] = {1, 2, 3, 4, 5};

2. Initialize Array with Values and without Size in C++

int arr[] = {1, 2, 3, 4, 5};

Accessing the Elements of an Array


string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars[0];
Change an Array Element Output:
Volvo
Change an Array Element
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
Output :
cout << cars[0]; Opel
Traversing / Printing Array elements
int arr[5]={10, 0, 20, 0, 30}; //creating and initializing array
Output :
//traversing array
10
for (int i = 0; i < 5; i++) 0
{ 2
cout<<arr[i]<<"\n"; 0
} 30

Size of an Array in C++


• we can calculate the size of an array using sizeof() operator trick.

1. Size of one element of an array


int arr[] = { 1, 2, 3, 4, 5 }; Output :
Size of arr[0] : 4
cout << "Size of arr[0]: " << sizeof(arr[0]) << endl;
2. Size of array
int arr[] = { 1, 2, 3, 4, 5
};
cout << "Size of arr: " << sizeof(arr) <<
endl;

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

One -Dimensional Arrays


• A one-dimensional array is a kind of linear array.
• It involves single sub-scripting.
• The [] (brackets) is used for the subscript of the array
and to declare and access the elements from the array.
Syntax: DataType ArrayName [size];
For example: int a[10];

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.

Syntax : DataType ArrayName[row_size][column_size];


For Example : int arr[5][5];

1.2. Three-Dimensional Arrays


• When we require to create two or more
tables of the elements to declare the array
elements, then in such a situation we use
three-dimensional arrays.

Syntax: DataType ArrayName[size1][size2][size3];


For Example: int a[5][5][5];
Do Subscribe

You might also like