wohhie
11/14/2018 - 4:25 AM

## Assignment 8

Assignment 8

``````PROGRAM GREATESTCOMMONDIVISOR
! -----------------------------------------------------------------------------
! this a program which read a pair of integer numbers and uses GCD(a,b) to find
! the greatest common divisor for each pair until a pair of zeros is entered
! indicate the end of data
! -----------------------------------------------------------------------------
IMPLICIT NONE

! Declaring variables
INTEGER :: b, result
INTEGER, DIMENSION(:), ALLOCATABLE :: A
INTEGER :: i, j, k, size, check

DO
PRINT *, "ENTER ARRAY SIZE: (O TO STOP THE PROGRAM)"

IF(size .eq. 0) THEN
PRINT *, "PROGRAM EXIT"
EXIT
ELSE IF(size .lt. 0) THEN
PRINT *, "ARRAY INDEX NEVER BE NEGATIVE."
ELSE
ALLOCATE(A(size), STAT=check)
PRINT *, "ENTER ARRAY VALUES: "

result = A(1)
DO i = 2, size
result = GCD2(A(i), result)
END DO

PRINT *, result;
DEALLOCATE(A)
END IF
END DO

CONTAINS

INTEGER RECURSIVE FUNCTION GCD2(m, n) RESULT(res)
IMPLICIT NONE
INTEGER, INTENT(IN) :: m, n
! temp will used for swaping value
if (m == 0) THEN
res = n
ELSE
res = gcd2(MOD(n,m), m)
END IF

END FUNCTION GCD2

END PROGRAM GREATESTCOMMONDIVISOR``````
``````PROGRAM CyclicGraphs1
!---------------------------------------------------------------------------
! Directed graph is entered as adjacency lists (using a char variable) and
! represented as an adjacency matrix to find the shortest cycle for a node
!---------------------------------------------------------------------------
IMPLICIT NONE
CHARACTER(80) :: X
INTEGER, DIMENSION(:,:), ALLOCATABLE :: A
INTEGER :: i,j,k,m,n,check, count
LOGICAL :: connected

connected = .false.

DO
PRINT *, "enter n (or 0 to stop):"
IF (n .eq. 0) STOP
IF (n .lt. 0) THEN
PRINT *, "incorrect value of n"
ELSE
ALLOCATE (A(n,n),STAT=check)
IF (check .ne. 0) THEN
PRINT *, "unsuccessful ALLOCATE"
ELSE
A = 0
PRINT *, "enter the graph, node after node:"
DO i = 1,n
m = INDEX(X,':')
DO WHILE (m .gt. 0)
X(m:m) = ' '
A(i,j) = 1
m = INDEX(X, ',')
END DO
END DO
END IF

PRINT *, "the adjacency matrix is: "
DO i = 1,n
PRINT "(5X,15I5)", A(i,1:n)
END DO

! call the logical function Conn
connected = CONN(A, n)
IF (connected) THEN
PRINT *, "This graph is strongly connected."
ELSE
PRINT *, "This graph is not strongly connected."
END IF

END IF
END DO

CONTAINS

LOGICAL FUNCTION CONN(A,n)
INTEGER, DIMENSION(n, n), INTENT(IN) :: A
INTEGER, INTENT(IN) :: n
INTEGER :: i, j, k, count
LOGICAL :: strong

strong = .false.
DO i = 1, n
count = 0
! check every column for single node and looking for 1
DO j = 1, n
IF (A(i, j) == 1) THEN
count = count + 1
END IF
END DO

! check every row for single node and looking for 1
DO k = 1, n
IF (A(k, i) == 1) THEN
count = count + 1
END IF
END DO

! if the counting value is size - 1 then we will sure that
! it can travarse every nodes
IF (count == n - 1) THEN
strong = .true.
END IF
END DO

CONN = strong

END FUNCTION CONN

END PROGRAM CyclicGraphs1``````