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