#include <iostream>
using namespace std;
void RemoveAt(int arr[],int length,int index)
{
if(length < 0)
cout<< "this array is empty";
else
if(index < 0 || index > length)
cout<< "the index is out of range";
else
{
for(int i=index; i<length ;i++)
{
arr[i]=arr[i+1];
}
arr[length-1]=0;
}
//This for loop will show you the values of the array after deleting the element "RemoveAt"
cout << "\n\n" << "Elements after deleting"<<"\n";
for (int i = 0; i < length ; i++)
{
cout << arr[i]<<"\n";
}
}
void RemoveAll(int arr[],int length,int item)
{
if(length < 0)
cout<< "this array is empty";
else
{
for (int i=0;i<length;i++)
{
if(arr[i]==item)
{
RemoveAt(arr,10,i);
i=i-1;
}
}
}
cout << "\n\n" << "Elements after deleting"<<"\n";
for (int i = 0; i < length ; i++)
{
cout << arr[i]<<"\n";
}
cin.get();
}
int main ()
{
int myArray [10] = {0,1,2,3,4,5,6,7,8,9};
int myArray2 [10] = {1,1,2,2,2,3,4,5,5,6};
int index,item,choice;
cout << "If you want to delete index press 1 \nIf you want to delete item press 2 \nChoice: ";
cin>>choice;
if(choice == 1)
{
cout<< "\n\n"<<"enter the index\nIndex: ";
cin>>index;
//This for loop will show you the values of the array before deleting the element "RemoveAt"
cout << "Elements before deleting"<<"\n";
for (int i = 0; i < 10 ; i++)
{
cout << myArray[i]<<"\n";
}
RemoveAt(myArray,10,index);
cin.get();
}
else
{
cout<< "\n\n"<<"enter the item\nItem: ";
cin>>item;
//This for loop will show you the values of the array before deleting the element "RemoveAll"
cout << "Elements before deleting 'this is the second array'"<<"\n";
for (int i = 0; i < 10 ; i++)
{
cout << myArray2[i]<<"\n";
}
RemoveAll(myArray2,10,item);
}
cin.get();
return 0;
}