0% found this document useful (0 votes)
5 views

Initializing A Dynamic Array

Uploaded by

ys920973
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Initializing A Dynamic Array

Uploaded by

ys920973
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

// initializing a dynamic array

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


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

int len = sizeof(arr) / sizeof(arr[0]);

// Traversing over arr[]

for (int i = 0; i < len; i++) {

cout << arr[i] << " ";

Insertion in Array:
// Function to insert element

// at a specific position

void insertElement(int arr[], int n, int x, int pos)

// shift elements to the right

// which are on the right side of pos

for (int i = n - 1; i >= pos; i--)

arr[i + 1] = arr[i];

arr[pos] = x;

Deletion in Array:
// To search a key to be deleted

int findElement(int arr[], int n, int key);

// Function to delete an element

int deleteElement(int arr[], int n, int key)

// Find position of element to be deleted

int pos = findElement(arr, n, key);

if (pos == -1) {

cout << "Element not found";

return n;
}

// Deleting element

int i;

for (i = pos; i < n - 1; i++)

arr[i] = arr[i + 1];

return n - 1;

// Function to implement search operation

int findElement(int arr[], int n, int key)

int i;

for (i = 0; i < n; i++)

if (arr[i] == key)

return i;

// Return -1 if key is not found

return -1;

You might also like