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

// C Program to Delete Array Elements by Location

#include <stdio.h>
int main()
{
	int A[] = {1,2,6,58,7}, n, i, j, k,flag = 0;

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

    printf("Array A :--> ");
	for(i=0; i<n; i++)
        printf("%d  ", A[i]);


	printf("\nEnter Index of Element to Delete :--> ");
	scanf("%d", &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)
	{
		printf("\nInvalid Index");
	}
	else
	{
	    printf("\nArray After Deleting Element from Index %d --> ", k);
		for(j = 0; j < n; j++)
		{
            printf("%d  ",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