If Any Required Program Please Ask In Comment I Will Help You(any Program in JAVA or C++) . . THANKS FOR VISITING MY BLOG!

If U LIKE MY PROFILE RAISE YOUR HAND IF U NOT RAISE UR STANDARD. Powered by Blogger.

Sunday, October 21, 2018

INSERT AND DELETE ELEMENTS FROM ARRAY's GIVEN INDEX.



#include <iostream>
#include <conio.h>

using namespace std;

class Array
{
private:
int *Arr, Size, Num;

public:
Array(int Siz)
{
Arr = new int[Siz];
for (int I = 0; I <Siz; I++)
{
Arr[I] = 0;
}

Num = 0;
Size = Siz;
}

void Input()
{
int No;

cout << "Enter Size Of Array: ";
cin >> No;
Num = No;

cout << "Enter Elements Of Array: " << endl;
for (int I = 0; I <Num; I++)
{
cin >> Arr[I];
}
}

void Insert(int Index, int Value)
{
for (int I = Num + 1; I >= Index; I--)
{
Arr[I] = Arr[I - 1];
}

Arr[Index] = Value;
Num++;
}

void Delet(int Index)
{
for (int I = Index; I <Num; I++)
{
Arr[I] = Arr[I + 1];
}

Num--;
}

void Print()
{
cout << "\nElements Of Array is: " << endl;
for (int I = 0; I <Num; I++)
{
cout << Arr[I] << endl;
}
}

~Array()
{
cout << "\n\n\n\n************PROGRAM ENDED***************";
}
};

int main()
{
Array Obj(10);

Obj.Input();
Obj.Insert(3, 334);       ///334 inserted at index 4th and size increases by 1;
Obj.Delet(5);                 ///value at 6th index deleted and size decreases by 1;
Obj.Print();

getch();
return 0;
}

0 comments:

Post a Comment