0

How can I take a text file that first lists a 2x2 matrix that was used to encrypt a message, followed by one integer per line (total 32 lines) that encode a message? Basically, I want to create a program that will:

1) Read and invert the 2x2 encryption matrix

2) Write a subroutine for determinant function to help invert matrix

3) Decrypt by reading in two integers at a time and inserting them into a character string (chracter*32 = full string)

And finally, print the hidden message.

I am pretty new to Fortran (and programming in general) but here is what I have so far and would appreciate any help.

program Program2

implicit none

INTEGER, DIMENSION(2,2) :: M, M2, M3
INTEGER :: B(32)
INTEGER :: row,col,max_rows,max_cols, Det, i, n, a
max_rows = 2
max_cols = 2

open(11, file = 'Data3.txt')

DO row = 1,max_rows
  READ(11,*) (M(row,col),col=1,max_cols)
END DO

!Finding the inverse of a 2x2 matrix and reassigning.
M2(1,1) = M(2,2)
M2(2,2) = M(1,1)
M2(1,2) = -M(1,2)
M2(2,1) = -M(2,1)

! Could not get determinant function to yield correct answer (calc by hand)
M3 = M2/-1
print*, M3
print*, Det

open(11, file = 'Data3.txt')
i = a

do i = 1, 16
    read(11,*) n
    print*, n
enddo

close(11)
end program Program2

! Determinant function for a 2x2 matrix
function Determinant(M2) result(Det)

   implicit none
   INTEGER, DIMENSION(2,2) :: M, M2, M3
   INTEGER :: B(32)
   INTEGER :: row,col,max_rows,max_cols, Det, i, n, a

   Det = M2(1,1)*M2(2,2) - M2(1,2)*M2(2,1)

end function Determinant

Here is the text file (or just a copy of what it looks like):

    3          11
       2           7
    1488
     955
     703
     458
    1379
     887
    1465
     943
    1196
     764
    1392
     895
    1433
     922
    1403
     902
    1372
     876
    1467
     943
     697
     454
    1518
     975
    1596
    1026
    1392
     895
    1536
     988
     726
     473
ReePOP
  • 41
  • 2

1 Answers1

1

First of all, to get your Determinant function working, it needs a type (see Function has no implicit type):

INTEGER :: Determinant

Edit: No need to be referenced as external, as pointed out in the comments. Thanks.

Then it can be used and it works well:

Det = Determinant(M2)
print*, Det

prints -1.

Second, could you please provide more explanation about what you want to do in step 3) so that we can help you out.

3) Decrypt by reading in two integers at a time and inserting them into a character string (chracter*32 = full string)

ekqnp
  • 284
  • 3
  • 13
  • In step 3, essentially what I am trying to do is take two 2 integers and read them as if they were a 2x1 matrix. I want to first take M2 and divide by Det, get a new matrix (ie M3). Then take M3 (a 2x2) and multiply with two integers at a time (2x1) to get a matrix that holds the values for the ASCII characters. (ie 83 would be S). Is this clearer? – ReePOP Mar 19 '18 at 19:11