$29
1. Write a Fortran integer function CountSeq(A,k) which finds, in an array A[1 : K] of integer numbers, the number of weakly increasing and decreasing sequences. For example, if A is [1,1,3,5,5,4,2,3], the result is 3. If all elements of A are equal, the result is zero.
Write also a Fortran program which reads the data, invokes CountSeq and outputs the returned result.
2. Let an integer matrix A[1 : M, 1 : N] contain nonnegative elements. Two nonzero elements are contiguous if they are adjacent to each other in the same row or the same column. A blob is defined as a group of all nonzero elements which are contiguous. Write an integer Fortran function CountBlobs (A,m,n) which finds and returns the number of blobs in a nonnegative integer matrix A.
Write also a Fortran program which enters matrix A, invokes CountBlobs and prints the result. Hint: An example outline of COUNTBLOBS is:
COUNT := 0
while there is a positive element in A do
COUNT := COUNT + 1
change the element found to −COUNT
iteratively find the blob by finding positive elements which are contiguous to elements equal to −COUNT and changing them to −COUNT, until no new elements are added to the blob
end while
Example: In the matrix below, the positive elements are represented by an asterisks:
*
*
*
*
*
0
*
*
*
0
0
0
0
*
0
0
0
*
*
*
*
0
*
0
*
0
*
*
0
0
0
*
0
*
0
*
*
0
*
*
*
0
*
0
*
*
0
0
0
0
0
*
0
*
*
*
*
*
*
*
*
0
*
0
0
0
0
0
0
0
*
*
*
*
*
*
*
*
*
*
*
There are 3 blobs in this matrix.