0
votes

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
1
I can't see any question. This is a Qustion nad Answer site, do you have a question? - Vladimir F
If there are any problems with your code, we need a thourough description of those problems. Error messages, wrong results... How wrong are they? How should the correct ones look? - Vladimir F
you know you never use your functiondeterminant . - agentp

1 Answers

1
votes

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)