Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Let a1, a2, . . . , an be a sequence of positive integers. An increasing subsequ

ID: 3639681 • Letter: L

Question

Let a1, a2, . . . , an be a sequence of positive integers. An increasing subsequence of length l is a contiguousblock ai, ai+1, . . . , ai+l1 satisfying ai 6 ai+1 6 · · · 6 ai+l1.Write a C program to read a sequence of positive integers and to print the length of the longest increasingsubsequence in it. In order to terminate the sequence, the user should enter zero or a negative value. Yourprogram must contain only one loop. Both scanning the next integer and processing the scanned integershould be done in that loop. Write no functions other than main(). Do not use any array.

Explanation / Answer

#include <stdio.h>

int main ()

{

int a, prev = -1, runningmaxlen = 0,

maxlen = 0;while (1)

{printf("Enter an integer: ");

scanf("%d", &a);

if (a <= 0) {if (runningmaxlen > maxlen) maxlen = runningmaxlen;break;}

if (a >= prev) {++runningmaxlen;}

else {if (runningmaxlen > maxlen) maxlen = runningmaxlen;runningmaxlen = 1;}

prev = a;}

printf("Length of the longest increasing sequence = %d ", maxlen);

}