write a program using array 1,2,3,4,5 and insert midle 5,6 and delete 3 ?
//write a program using array 1,2,3,4,5 and insert midle 5,6 and delete 3 ?
#include <iostream>
using namespace std;
int main()
{
int n=5;
int num=5;
int num2=6;
int arr[n]={1,2,3,4,5};
int loc=3;
int loc2=4;
for (int i = n; i >= loc; i--) {
arr[i] = arr[i - 1];
}
n++;
arr[loc - 1] = num;
for (int i = n; i >= loc2; i--) {
arr[i] = arr[i - 1];
}
n++;
arr[loc2 - 1] = num2;
cout << "\nArray elements after insertion:\n";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << "\nArray elements after deletion:\n";
int el=3;
for(int i=0; i<n; i++)
{
if(arr[i]==el)
{
for(int j=i; j<(n-1); j++)
arr[j] = arr[j+1];
i--;
n--;
}
}
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}
Comments
Leave a comment