/* 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);Test driver:
void compare(int actStart,int actLen,int expStart,int expLen) { if (actStart != expStart) printf("Start. Actual: %d Expected: %d\n",actStart,expStart); else if (actLen != expLen) printf("Length. Actual: %d Expected: %d\n",actLen,expLen); } int main() { int a[100],aLen,pStart,pLen; /* aLen == 1 */ printf("Test 1\n"); aLen = 1; a[0] = 1; findLongestPlateau(a,aLen,&pStart,&pLen); compare(pStart,pLen,0,1); /* plateau of length 1 */ printf("Test 2\n"); aLen = 4; a[0] = 0; a[1] = 1; a[2] = 0; a[3] = 1; findLongestPlateau(a,aLen,&pStart,&pLen); compare(pStart,pLen,0,1); /* plateau of length aLen */ printf("Test 3\n"); aLen = 4; a[0] = 1; a[1] = 1; a[2] = 1; a[3] = 1; findLongestPlateau(a,aLen,&pStart,&pLen); compare(pStart,pLen,0,4); /* longest plateau at front */ printf("Test 4\n"); aLen = 4; a[0] = 1; a[1] = 1; a[2] = 3; a[3] = 3; findLongestPlateau(a,aLen,&pStart,&pLen); compare(pStart,pLen,0,2); /* longest plateau at end */ printf("Test 5\n"); aLen = 4; a[0] = 1; a[1] = 2; a[2] = 3; a[3] = 3; findLongestPlateau(a,aLen,&pStart,&pLen); compare(pStart,pLen,2,2); /* two longest plateaus */ printf("Test 6\n"); aLen = 6; a[0] = 1; a[1] = 2; a[2] = 2; a[3] = 3; a[4] = 3; a[5] = 1; findLongestPlateau(a,aLen,&pStart,&pLen); compare(pStart,pLen,1,2); }
/* 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);Test driver:
int stubStart,stubLen; void findLongestPlateau(int a[],int aLen,int* pStart,int *pLen) { *pStart = stubStart; *pLen = stubLen; } void load(int a[],int *aLen,int n) { int i; for (i = 0; i < n; i++) a[i] = pow(10,i); *aLen = n; } void check(int a[],int aLen,int expSum) { int i,actSum = 0; for (i = 0; i < aLen; i++) actSum += a[i]; if (actSum != expSum) printf("Sum. Actual: %d Expected: %d\n",actSum,expSum); } int main() { int a[100],aLen; /* short plateau */ printf("Test 1\n"); load(a,&aLen,4); stubStart = 0; stubLen = 1; removeLongestPlateau(a,&aLen); check(a,aLen,1110); /* medium-sized plateau */ printf("Test 2\n"); load(a,&aLen,4); stubStart = 1; stubLen = 2; removeLongestPlateau(a,&aLen); check(a,aLen,1001); /* long plateau */ printf("Test 3\n"); load(a,&aLen,4); stubStart = 0; stubLen = 4; removeLongestPlateau(a,&aLen); check(a,aLen,0); }