C++ Program to Delete Array Elements by Location

C++ Program to Delete Array Elements by Location

Write C++ Program to Delete Array Elements by Location

// CPP Program to Delete Array Elements by Location

#include <iostream>

using namespace std;

int main()
{
	int A[] = {1,2,6,58,7}, n, i, j, k,flag = 0;

	n = sizeof(A)/sizeof(A[0]);

        cout << "Array A :--> ";
	for(i=0; i<n; i++)
        cout << A[i] << " ";


	cout << "\nEnter Index of Element to Delete :--> ";
	cin >> k;

	if(k < n)
	{
		for(i = 0; i < n; i++)
		{
			if(i == k)
			{
				flag = 1;
				int f;
				for(f = i; f < n - 1 ; f++)
				   A[f] = A[f+1];
                                n--;
			}

		}
	}
	if(flag == 0)
	{
		cout << "\nInvalid Index";
	}
	else
	{
	    cout << "\nArray After Deleting Element from Index " << k << " :--> ";
	    for(j = 0; j < n; j++)
	    {
                cout << A[j] << " ";
	    }
	}

	return 0;
}

Output:

Run 1:
Array A :--> 1  2  6  58  7
Enter Index of Element to Delete :--> 2

Array After Deleting Element from Index 2 --> 1  2  58  7

Run 2:
Array A :--> 1  2  6  58  7
Enter Index of Element to Delete :--> 11

Invalid Index