Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- int main(){
- //n is for total elements in array at this moment
- int arr[10]={2,3,5,6,7},pos,value,i,n=5;
- //n++ means n=6 and we put value 8 at sixth position
- arr[n++]=8;
- cout<<"8 added to last position..."<<endl;
- //here we shift all the value to next position
- //then at arr[0] mean at 1st position we put value 1
- //after that increase the element value by 1
- for(int i = n ; i > 0 ; i--){
- arr[i] = arr [i-1];
- }
- arr[0]=1;
- n++;
- cout<<"1 added to first position..."<<endl;
- //here at position 3 we put value 4
- pos = 3;
- value = 4;
- // shift all value form last position to the desired position
- for(int i = n ; i >= pos-1 ; i--){
- arr[i] = arr[i-1];
- }
- //puting value to the position
- //after that increase the element value by 1
- arr[pos-1] = value;
- n++ ;
- cout<<"4 added to third position..."<<endl;
- cout<<"\nThe new array"<<endl;
- //Printing the New Array after adding all new elements
- for (int i = 0; i < n; i++)
- {
- cout<<arr[i]<<" ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement