19EAC203 Data Structures
and Algorithms
Dr. Nimmy K
Assistant Professor
Department of Computer Science and Engineering
Amrita Vishwa Vidyapeetham, Amritapuri
Arrays
• An array is a collection of similar type of items stored at contiguous
memory locations.
• Elements: Each item stored in an array.
• Index: Each location of an element in an array has a numerical index.
Array: Insertion Operation
Array: Deletion Operation
Declaring Array in C++
• To declare an array in CPP, need to specifies the type of the elements and the number
of elements required by an array as follows
type arrayName [ arraySize ];
• Single-dimensional array.
• The arraySize must be an integer constant greater than zero and type can be any valid C++ data
type
Eg: float balance[10];
• balance is an array which is sufficient to hold up to 10 float numbers.
Initializing an Array
• It is possible to initialize an array during declaration
int mark[5] = {19, 10, 8, 17, 9};
int mark[] = {19, 10, 8, 17, 9};
Example 1: Traversal
C++ Dynamic Allocation of Arrays
• In C++, a dynamic array can be created using new keyword and can be
deleted it by using delete keyword.
• int *a = new int(n);
• The allocated memory must be deleted using the delete[] operator.
Output
• Write the above program in OOP.
Output: