lab exercise

 

Compact

 

 

 

Background:

 

A common task with array processing might be to traverse a list and eliminate an undesired value.  A text file of integers is provided on disk (compact.txt) which contains both non-zero and zeroes in random fashion.  The number of integers (number <= 100) on disk is not given.

 

 

 

Assignment:

 

1.   Write a program that reads a text file (compact.txt) and stores the integers in an array.  This text file will be provided by your instructor.

 

2.   Write a function compact that eliminates all 0's (zeroes) from its array parameter, leaving the order of the other elements unchanged.  All local variables within this function must be scalar.  In other words, you may not use a second array to solve the problem.  The function also receives an integer parameter which represents the number of integer data stored in the array.  You must use the following function header.

 

      void  compact  (apvector<int> &list,  int  &N)

      //  precondition:  list contains integers from 0..N-1

      //  postcondition:  all 0's have been removed from list;  N is reduced as appropriate.

 

3.   Do not solve the problem by printing out only the non-zero values in the array.  Function compact must remove all zeroes from the array.

 

 

 

Instructions:

 

1.   Print out the list both before and after removing the zeros.  For example:

 

Before:  0, 9, 7, 0, 0, 23, 4, 0

 

After:  9, 7, 23, 4

 

2.   Your program must use proper modular design and parameter passing.