wohhie
11/5/2018 - 12:12 PM

## Assignment 7

Assignment 6

``````  !------------------------------------------------------------------------
! The fortran subroutine CSRorCSC(R,C,V, n, nz) which converts
! the CSR representation of a sparse matrix to its CSC representation
!-------------------------------------------------------------------------
! Also a program which enters a sparse matrix (in the COO format) and
! creates its CSR Representation, invokes the CSRorCSC Subroutine and
! output the result.
!-------------------------------------------------------------------------

PROGRAM A7Q1

IMPLICIT NONE
! DECLARE VARIABLES
INTEGER, DIMENSION(:,:), ALLOCATABLE :: A
INTEGER, DIMENSION(3) :: B
INTEGER :: i, j, size, m, n, check, k, rowIndex
! R, C, V
INTEGER, DIMENSION(:), ALLOCATABLE :: DUMMY
INTEGER, DIMENSION(:), ALLOCATABLE :: R
INTEGER, DIMENSION(:), ALLOCATABLE :: C
INTEGER, DIMENSION(:), ALLOCATABLE :: V

! GET THE SPARSE MATRIX IN COO FORMAT
DO
PRINT *, "ENTER THE (SIZE OF MATRIX) m and n (OR 0 0 to END): "

IF (m .eq. 0 .or. n .eq. 0) STOP
IF (m .lt. 0 .or. n .lt. 0) THEN
PRINT *, "NEGATIVE NUMBER OF ELEMENTS: "

ELSE
! ALLOCATE THE ARRAY A
ALLOCATE(A(m,n))
PRINT "(1X, 'ENTER THE MATRIX VALUES OF A', I3, '*', I3, ' ELEMENTS:')", m, n
READ *, ((A(i, j), i = 1, m), j = 1, n)

! DISPLAY USER INPUT (LATER WE WILL USE THE FORMATTING.)
PRINT *, "COO FORMAT : "
DO i = 1,m
PRINT "(5X,15I5)", A(i,1:n)
END DO

! CSRorCSC FORMAT
size = 1
DO i = 1, m
DO j = i, n
IF (A(i, j) /= 0) THEN
size = size + 1
END IF
END DO
END DO

PRINT *, size   ! 8 for demo sparse matrix

! ALLOCATING R, C, V
ALLOCATE(DUMMY(size), STAT = check)
ALLOCATE(R(m), STAT = check)
ALLOCATE(C(size), STAT = check)
ALLOCATE(V(size), STAT = check)

! GETTING THE R,C,V FOR CSR
k = 1
rowIndex = 0
DO i = 1, m
DO j = 1, n
IF (A(i, j) /= 0) THEN
DUMMY(k) = i
rowIndex = rowIndex + 1
C(k) = j
V(k) = A(i, j)
k = k + 1
END IF
END DO
R(i) = rowIndex
END DO

PRINT *, "CSR FORMAT: "
PRINT *, "R: ", DUMMY
PRINT *, "R: ", R
PRINT *, "C: ", C
PRINT *, "V: ", V

END IF
END DO

END PROGRAM A7Q1``````