Inspection and Procedural Abstraction

Course theme: problem decomposition

Procedural abstraction

Procedural abstraction in inspection

Example

/* Assign to *pStart and *pLen the starting position and length
 * of the longest plateau in a[0..aLen-1].
 * 
 * Assumed: a has at least aLen elements and aLen > 0
 */
void findLongestPlateau(int a[],int aLen,int* pStart,int *pLen)
{
	int tmpStart,tmpLen,i;

	*pStart = 0;
	*pLen = 1;
	tmpStart = 0;
	tmpLen = 1;
	for (i = 0; i < aLen-1; i++) {
		if (a[i] == a[i+1]) {
			tmpLen++;
		} else {
			if (tmpLen >= *pLen) {
				*pStart = tmpStart;
				*pLen = tmpLen;
			}
			tmpStart = i+1;
			tmpLen = 1;
		}
	}
}

/* Remove P, the longest plateau in a, shifting left all the elements
 * to the right of P and decreasing *aLen appropriately.
 * 
 * Assumed: a has at least *aLen elements and *aLen > 0
 */
void removeLongestPlateau(int a[],int *aLen)
{
	int i,pStart,pLen;

	findLongestPlateau(a,*aLen,&pStart,&pLen);
	for (i = 0; i < *aLen-pStart-pLen; i++) {
		a[i+pStart] = a[i+pStart+pLen];
	}
}

int main()
{
	int i,xLen,x[100];

	/* longest plateau at end */
	xLen = 4; x[0] = 1; x[1] = 2; x[2] = 3; x[3] = 3;
	removeLongestPlateau(x,&xLen);
	for (i = 0; i < xLen; i++)
		printf("%d\n",x[i]);
	printf("\n");

	/* two longest plateaus */
	xLen = 6; x[0] = 1; x[1] = 2; x[2] = 2; x[3] = 3; x[4] = 3; x[5] = 1;
	removeLongestPlateau(x,&xLen);
	for (i = 0; i < xLen; i++)
		printf("%d\n",x[i]);
}