wohhie
10/12/2018 - 5:57 AM

## Assignment 4

Assignment 4 Question: 1

PROGRAM assignment4Question2

IMPLICIT NONE
INTEGER, DIMENSION (:, :), ALLOCATABLE :: Ad
INTEGER, DIMENSION (15, 15) :: As
INTEGER :: i, j, m, n, check
INTEGER :: result

DO
PRINT *, "ENTER m and n (OR 0 0 to END):"
READ *, m, n

IF (m .eq. 0 .or. n .eq. 0) STOP
IF (m .lt. 0 .or. n .lt. 0) THEN
PRINT *, "NEGATIVE NUMBER OF ELEMENTS"
ELSE IF(m .gt. 15 .or. n .gt. 15) THEN
PRINT *, "TO LARGE ARRAY. "
ELSE
PRINT "(1X, 'ENTER THE VALUES A', I3, '*', I3, ' ELEMENTS:')", m, n
READ *, ((As(i, j), i = 1, m), j = 1, n)

result = COUNTBLOB(As, m, n)
PRINT *, "NUMBER OF BLOBS :", result
END IF
END DO

CONTAINS

INTEGER FUNCTION COUNTBLOB(A, m, n)
INTEGER, DIMENSION (m, n) :: A
INTEGER, INTENT(IN) :: m, n
INTEGER :: count, item

! check the first section
IF (A(1,2) == 0 .or. A(2,1) == 0) THEN
IF (A(1,1) /= 0) THEN
count = count + 1;
END IF
END IF

IF (A(1,n-1)==0 .or. A(2,n)==0) THEN
IF (A(1,n)/=0) THEN
count=count+1;
END IF
END IF

IF (A(m-1,1)==0 .or. A(m,2)==0) THEN
IF (A(m,1)/=0) THEN
count=count+1;
END IF
END IF

IF (A(m,n-1)==0 .or. A(m-1,n)==0) THEN
IF (A(m,n)/=0) THEN
count=count+1;
END IF
END IF

! CHECK SECTION 2
DO  j = 2, n-1
IF (A(1,j) /= 0) THEN
IF (A(1,j-1) == 0) THEN
item = item + 1;
END IF
IF (A(1,j+1)==0) THEN
item = item + 1;
END IF
IF (A(2,j)==0) THEN
item = item + 1;
END IF
IF (item == 2) THEN
count = count + 1;
END IF
END IF
item = 0;
END DO

DO  j = 2, n - 1
IF (A(m,j) /= 0) THEN
IF (A(m,j-1) == 0) THEN
item = item + 1;
END IF
IF (A(m,j+1) == 0) THEN
item = item + 1;
END IF

IF (A(m-1,j) == 0) THEN
item = item + 1;
END IF
IF (item == 2) THEN
count = count + 1;
END IF
END IF
item = 0;
END DO

DO  i = 2, m - 1
IF (A(i,1) /= 0) THEN
IF (A(i-1,1) == 0) THEN
item = item + 1;
END IF
IF (A(i + 1, 1) == 0) THEN
item = item + 1;
END IF

IF (A(i,2) == 0) THEN
item = item + 1;
END IF

IF (item == 2) THEN
count=count+1;
END IF
END IF
item = 0;
END DO

DO  i = 2, m - 1
IF (A(i,n) /= 0) THEN
IF (A(i-1,n) == 0) THEN
item = item + 1;
END IF

IF (A(i+1,n)==0) THEN
item = item + 1;
END IF

IF (A(i,n-1)==0) THEN
item = item + 1;
END IF

IF (item == 2) THEN
count = count + 1;
END IF
END IF
item = 0;
END DO

! CHECK SECTION 3
DO  i = 2, m - 1
DO  j = 2, n - 1
IF (A(i,j) /= 0) THEN
IF (A(i+1,j) == 0) THEN
item = item + 1;
END IF
IF (A(i-1,j) == 0) THEN
item = item + 1;
END IF

IF (A(i,j+1) == 0) THEN
item = item +1;
END IF

IF (A(i,j-1) == 0) THEN
item = item + 1;
END IF

IF (item == 3) THEN
count = count + 1;
END IF

item = 0;
END IF
END DO
END DO

! GETTING THE NUMBER OF BLOBs
result = count/2;
COUNTBLOB = result

END FUNCTION COUNTBLOB
END PROGRAM assignment4Question2
PROGRAM ORDERING
!-------------------------------------------------------
! An example of Allocatable array and selection sort as
! a subroutine (with an INOUT parameters)
!---------------------------------------------------------
IMPLICIT NONE
! declaring variable
INTEGER, DIMENSION(:), ALLOCATABLE :: Aone
INTEGER :: k, check, result

DO
PRINT *, "Enter the number of elements (OR 0 TO STOP):"
READ *, k

IF (k .eq. 0) STOP
IF (k .lt. 0) THEN
PRINT *, "negative number of elements"
ELSE
! allocating array
ALLOCATE(Aone(k), STAT = check)
IF (check .ne. 0) THEN
PRINT "(1X, 'enter the values of', I3,' elements: ')", k
READ *, Aone
PRINT "(1X, 'data:', 15I5)", Aone
result = CountSeq(Aone, k)
PRINT *, "RESULT:", result
! deallocating array
DEALLOCATE(Aone)
END IF
END IF
END DO

CONTAINS
! AN IMPLEMENTATION OF CountSeq
INTEGER FUNCTION CountSeq(A, k)
INTEGER, DIMENSION (:), INTENT (INOUT) :: A
INTEGER, INTENT(IN) :: k
INTEGER :: count = 0, currItem, nextItem, i, zero = 0
CHARACTER*3 :: state

! CHECK EVERY ITEM IS 0 THEN CLOSE
DO i = 1, k - 1
IF (A(i) .eq. 0) THEN
zero = zero + 1
END IF
END DO

! IF ALL ELEMENTS ARE 0 THEN CLOSE
IF (zero /= k) THEN
DO i = 1, k - 1
currItem = A(i)
nextItem = A(i + 1)
IF (A(i + 1) .eq. currItem) THEN
state = 'oke'

! if the previous item not equal to next time, greater than and the state is increasing
! it will change the state to increment and increment the weakly increasing
ELSE IF (A(i + 1) .gt. A(i) .and. currItem .ne. nextItem .and. (state /= 'inc')) THEN
count = count + 1
state = 'inc'

! if the previous item not equal to next time, less than and the state is increasing
! it will change the state to increment and increment the weakly decreasing
ELSE IF(A(i + 1) .lt. A(i) .and. currItem .ne. nextItem .and. (state /= 'dec')) THEN
count = count + 1
state = 'dec'
END IF

! set the final result
CountSeq = count

END DO

ELSE
PRINT *, "PROGRAM CLOSE"
END IF

END FUNCTION CountSeq
END PROGRAM ORDERING