[E-7]
Write a function that deletes the element at index N in an array, and fills
the gap by moving elements above N downward by one. Put a 0 in the last element.
If index N does not exist, do nothing to the array. Return 1 if the deletion
was performed, 0 if not.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 delete at index:15 Failure 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 delete at index:14 Success 0 1 2 3 4 5 6 7 8 9 10 11 12 13 0 delete at index:6 Success 0 1 2 3 4 5 7 8 9 10 11 12 13 0 0 delete at index:
If asked to delete one of the zeros at the end of the array, the function does so, even though this has no effect. Here is a testing framework:
#include <stdio.h>
#include <stdlib.h>
int deleteElement( int size, int arr[], int n )
{
. . .
}
void printArray( int size, int arr[] )
{
const int N = 10;
int j;
for ( j=0; j < size; j++ )
{
if ( j%N == N-1 )
printf("%4d\n", arr[j] );
else
printf("%4d ", arr[j] );
}
}
void fillArrayInOrder( int size, int arr[], int first )
{
int j;
for ( j=0; j < size; j++ )
{
arr[j] = first+j;
}
}
int main()
{
const int SIZE = 25;
int x[SIZE];
char input[32];
int del ;
fillArrayInOrder( x, SIZE, 0 );
printArray( SIZE, x );
printf("\ndelete at index:");
gets( input );
while ( strcmp( input, "q") != 0 )
{
del = atoi( input );
if ( deleteElement( SIZE, x, del ) )
printf("Success\n\n");
else
printf("Failure\n\n");
printArray( SIZE, x );
printf("\ndelete at index:");
gets( input );
}
return 0;
}