0

I keep getting this error when compiling this code:


   14 |    integer :: M(2,2), MInv(2,2), Determinant, a, b, c, d, detM, i, v(2,1), ascIIcode(2,1)
      |                                                                                         1
Error: Allocatable array ‘asciicode’ at (1) must have a deferred shape or assumed rank
hw51.f95:14:73:

   14 |    integer :: M(2,2), MInv(2,2), Determinant, a, b, c, d, detM, i, v(2,1), ascIIcode(2,1)
      |                                                                         1
Error: Allocatable array ‘v’ at (1) must have a deferred shape or assumed rank

I tried putting an deferred shape of "(:)" inside those affected arrays but did not help the situation. Also, why am I not getting this error for the 2x2 arrays that I declared before?

Here is the code:

program Decode
   ! Please update the information below: 
   ! Name:Nik Wrye
   ! course: CDS251-001
   ! Assignment #5.1
   ! Due Date: September
   ! Description: A description of what the program does.
   
   implicit none
   
   ! Declare variables - Add variables as necessary (integer only!)
   ! M will store the encoding matrix, MInv will store its inverse
   ! Decoded_message will store the decoded message
   integer :: M(2,2), MInv(2,2), Determinant, a, b, c, d, detM, i, v(2,1), ascIIcode(2,1)
   allocatable :: v, ascIIcode
   character*32 :: Decoded_Message
   
   ! open data file and read in the encoding matrix
   open(42,file='Data3.txt')
   
   read(42,*) M(1,1), M(1,2)
   read(42,*) M(2,1), M(2,2)
   
   ! Invert the encoding matrix and store it in MInv

   detM  = determinant(M)

   MInv(1,1) = +detM *M(2,2)
   MInv(1,2) = -detM *M(2,1)
   MInv(2,1) = -detM *M(1,2)
   MInv(2,2) = +detM *M(1,1)
   
   
   ! Processing steps required:
       
   ! Read from the file in 2 numbers at a time and store in a vector array
       
       do i = 2, 31
       
       allocate (v(2,1), ascIIcode(2,1))
       
       read(42,*) v(1,1)
       read(42,*) v(2,1)
   ! decode the 2 numbers read in (1) by multiplying Minv by the vector array from (1)
       
       ascIIcode(1,1) = ((MInv(1,1)*v(1,1))+(MInv(1,2)*v(2,1)))
       ascIIcode(2,1) = ((MInv(2,1)*v(1,1))+(MInv(2,2)*v(2,1)))

   ! Insert the result from (2) into the character string Decoded_Message. To concatinate 
   ! characters use the '//' operator
       
      Decoded_Message = char(ascIIcode(1,1))//char(ascIIcode(2,1))

   ! Use a loop that advances in steps of 2 and goes to 31 
       deallocate (v)
       deallocate (ascIIcode)
       
       end do 
   ! print results.
   print*, Decoded_Message
   
   ! close files
   close(42)

end program Decode


integer function Determinant(M)

   ! This function computes the determinant of matices of size 2 or 3
   ! M is the matrix for which the determinant is calculated (square matrix only)
   ! n is the number of rows or columns in M  

    implicit none
    
    integer :: M(2,2), a, b, c, d, e, f, g, h, i, Det
    
    do
    
    a = M(1,1)
    b = M(1,2)
    c = M(2,1)
    d = M(2,2)
    
    Det = (a*d)-(b*c)
    
    end do

end function Determinant
francescalus
  • 30,576
  • 16
  • 61
  • 96
Nik Wrye
  • 21
  • 6

0 Answers0