Compact an array while preserving retained order

Write keep_nonnegative(int a[], int n), which removes negative values from the logical sequence in place, preserves the relative order of the retained values, and returns the new logical length. No second data array is allowed. Storage after the new length may contain any old values and must not be printed as retained data. Input is 0<=n<=10 followed by n values in [-100,100]. Print length=L values: followed by each retained value with one preceding space and a newline. For the mixed sample 6 / -2 0 5 -1 0 4, trace the read index and next write index. Explain why writing cannot overwrite an unread value.


