C29 Answer
#include <stdio.h>
#include <stdlib.h>
/* Puzzle C29 -- delete the element at index N,
| move elements down to fill the gap.
|
| Do nothing to the array if index N does not exist,
| and return 0. Otherwise return 1.
|
*/
int deleteElement( int arr[], int size, int n )
{
int j;
if ( !(0<=n && n<size) ) return 0;
for ( j=n; j < size-1; j++ )
arr[j] = arr[j+1];
arr[size-1] = 0;
return 1;
}
void printArray( int arr[], int size )
{
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 arr[], int size, int first )
{
int j;
for ( j=0; j < size; j++ )
{
arr[j] = first+j;
}
}
int main(int argc, char *argv[])
{
const int SIZE = 25;
int x[SIZE];
char input[32];
int del ;
fillArrayInOrder( x, SIZE, 0 );
printArray( x, SIZE );
printf("\ndelete at index:");
gets( input );
while ( strcmp( input, "q") != 0 )
{
del = atoi( input );
if ( deleteElement( x, SIZE, del ) )
printf("Success\n\n");
else
printf("Failure\n\n");
printArray( x, SIZE );
printf("\ndelete at index:");
gets( input );
}
system("pause");
return 0;
}