Header Ads

Inserting Value at End Location in Array | Specific Location using Array Data Structure | Delete Value in Array in Data Structure Using C++

In this Post how to Insert or Delete values at any position in an array data structure using simple and easy steps.
From inserting or Delete at the end location to inserting at a specific position this post will guide you through the process of adding value to any array.

Program Output 

Array Value before Insertion 

2  5  9

Enter Value to Insert : 8

Enter the position to Insert : 1

Show Value after Insertion 

2  8  5  9


Program 1-- Source Code Insert Value

#include<iostream>

using namespace std;

class ArrEx{

 private:

  int arr[10], len = 0;

 public:

 void ArrAssign(){

  arr[0] = 10;

  arr[1] = 20;

  arr[2] = 30;

  len = 2;

}

 void insertValue(){

   cout<<"\nEnter Array Value:";

   for(int i =3; i<10;i++){

   cin>>arr[i];

    len = i;

   }

 }

  void ShowValue(){

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

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

  }

};

main(){

 ArrEx obj;

 obj.ArrAssign();

 cout<<"Array Value Before Inserting"<<endl;

 obj.ShowValue();

 obj.insertValue(); 

 cout<<"Array Value After Inserting"<<endl;

 obj.ShowValue();

}


Program 2 -- Source Code Insert Value 

#include<iostream>

using namespace std;

class ArrEx{

 private:

 int arr[6];

 public:

 void ArrAssign(int psArr[]){

       for(int i = 0; i<3; i++)

  arr[i] = psArr[i];

 }

void insertValue(int loc, int nv){                                                

for(int i = 3;i<=loc; i--){

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

}

arr[loc] = nv;

}

void ShowValue(int n){

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

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

}

};

main(){

ArrEx obj;

int pos, n, initArr[3] = {5,8,2};

obj.ArrAssign(initArr);

cout<<"Array Value before insertion "<<endl;

obj.ShowValue(3);

cout<<"\nEnter Value to Insert ?";

cin>>n;  //11

cout<<"\nEnter Position to insert ?";

cin>>pos;  //1

if(pos>= 5){

cout<<"Invalid Location";

return 0;

}

obj.insertValue(pos,n);

cout<<"\nShow Value afer insertion "<<endl;

obj.ShowValue(4);

}

Program Source Code Insert Value 3

#include<iostream>

using namespace std;

class ArrEx{

private:

int arr[10];

public:

void ArrAssign(int ps[]){

for(int i = 0; i<3; i++)

arr[i] = ps[i];  // 5 8 2

}

void insertValue(int loc, int nv){  // 2, 11

for(int i = 5;i>=loc; i--)     // 1 = 2

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

arr[loc] = nv;

}

void ShowValue(int n){

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

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

}

};

main(){

ArrEx obj;

char choice;

int pos, n, initArr[3] = {5,8,2};

int len = 3;

obj.ArrAssign(initArr);

cout<<"Array Value before insertion "<<endl;

obj.ShowValue(3);

do{

len++;  //len = 4

cout<<"\nEnter Value to Insert ?";

cin>>n;  //11

cout<<"\nEnter Position to insert ?";

cin>>pos;   //2

if(pos>= 5){

cout<<"Invalid Location";

return 0;

}

obj.insertValue(pos,n);

cout<<"\nShow Value afer insertion "<<endl;

obj.ShowValue(len);

cout<<"\n-------------------------------\n";

cout<<"\nDo you insert new value:";

cin>>choice;

}while(choice =='Y'||choice == 'y');

}


Related Video 



Program Source Code Delete Value in Array
#include<iostream>
using namespace std;
class ArrEx{
 private:
        int arr[5];
 public:
  void ArrAssign(int ps[]){
  for(int i = 0; i<=5; i++)
  arr[i] = ps[i]; 
  }
  void delValue(int loc){  
  for(int i=loc; i<=4; i++) 
    arr[i] = arr[i+1];
    arr[4] = 0;
  }
  void ShowValue(int n){
   for(int i = 0; i<=n; i++)
   cout<<arr[i]<<" ";
  }
};
main(){
 ArrEx obj;
 char choice;
 int pos, n, initArr[5] = {5,8,2,7,6};
 int len = 5;
 obj.ArrAssign(initArr);
        do{
 system("cls");
 cout<<"Array Value before deletion "<<endl;
 obj.ShowValue(len);
  len--;
 cout<<"\nEnter Position of Deletion number:";
 cin>>pos;
 if(pos<5){
  cout<<"Invalid Position";
  return 0;
 }
 obj.delValue(pos-1);
 cout<<"\nShow Value afer deletion "<<endl;
 obj.ShowValue(len);
 cout<<"\n-------------------------------\n";
 cout<<"\nDo you want to delete another value:";
 cin>>choice;
 }while(choice =='Y'||choice == 'y'); 
 

}


Related Video





No comments

Powered by Blogger.