0% found this document useful (0 votes)
10 views5 pages

Array

Uploaded by

aqsa206800
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)
10 views5 pages

Array

Uploaded by

aqsa206800
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
You are on page 1/ 5

#include <iostream>

class Array {

private:

int* arr;

int capacity;

int size;

public:

// Constructor

Array(int capacity) {

this->capacity = capacity;

this->size = 0;

arr = new int[capacity];

// Destructor

~Array() {

delete[] arr;

// Insert element at specific index

void insert(int index, int value) {

if (index < 0 || index > size || size == capacity) {

std::cout << "Invalid index or array is full." << std::endl;


return;

for (int i = size; i > index; i--) {

arr[i] = arr[i - 1];

arr[index] = value;

size++;

// Delete element at specific index

void deleteElement(int index) {

if (index < 0 || index >= size) {

std::cout << "Invalid index." << std::endl;

return;

for (int i = index; i < size - 1; i++) {

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

size--;

// Search for an element

void search(int value) {

bool found = false;

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


if (arr[i] == value) {

std::cout << "Element " << value << " found at index " << i << std::endl;

found = true;

break;

if (!found) {

std::cout << "Element " << value << " not found in the array." << std::endl;

// Display array elements

void display() {

if (size == 0) {

std::cout << "Array is empty." << std::endl;

return;

std::cout << "Array elements: ";

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

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

std::cout << std::endl;

};
int main() {

Array array(10);

// Insert elements

array.insert(0, 10);

array.insert(1, 20);

array.insert(2, 30);

array.insert(3, 40);

array.insert(4, 50);

// Display array elements

std::cout << "After inserting elements: ";

array.display(); // Output: Array elements: 10 20 30 40 50

// Search for an element

array.search(30); // Output: Element 30 found at index 2

// Delete an element

array.deleteElement(2);

// Display array elements after deletion

std::cout << "After deleting element at index 2: ";

array.display(); // Output: Array elements: 10 20 40 50

// Search for a non-existent element


array.search(60); // Output: Element 60 not found in the array.

return 0;

You might also like