• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1321
  • Last Modified:

FORTRAN 77 Question

I'm writing a program that will take arguments from the command line for the dimensions and values of an n*n matrix. The problem is that the intrinsic getarg(integer variable, character variable) as I am using it in my program gives me the following error at compile time when using the Einsys Fortran 1.0 IDE trial version:

Statement at (2) invalid in context established by statement at (1)
C:\PROGRA~1\EINSYS~1\p2m2.f:34:
                       CALL GETARG(N*(I-1)+J+1,R)
                            ^
Reference to intrinsic `GETARG' at (^) invalid -- one or more arguments have incorrect type

----- End of Compile

This is my source:

        PROGRAM P2M2
        PARAMETER (MAX = 10) ! maximum row/column dimension is 10
        REAL A(MAX,MAX)      ! initialize three matrices
        REAL B(MAX,MAX)
        REAL C(MAX,MAX)
        CHARACTER P, D
        INTEGER I,J,N
c        Begin main..................................................      
        CALL GETARG(1,P)
        N = INTEGER(P)      
      IF (N.GT.MAX) THEN
         PRINT*,'n greater than',MAX,', program will exit'
         STOP
      ENDIF
   
        DO I=1,N
           DO J=1,N
                 CHARACTER R
                CALL GETARG(N*(I-1)+J+1,R)
                A(I,J) = REAL(R)
           ENDDO
        ENDDO
            
        CALL FILLB(A,B,N,MAX) ! call to fillb
      
        CALL MULTIPLY(A,B,C,N,MAX) ! call to multiply
      
        DO I=1,N
           DO J=1,N      
              WRITE (6,10) C(I,J) ! print product matrix2
10          FORMAT(F9.3,$)
           ENDDO
           PRINT*
           PRINT*
        ENDDO      
            
        END
            
        SUBROUTINE FILLB(X,Y,N,MAX) ! fill matrix b
        REAL X(MAX,MAX), Y(MAX,MAX), TEMP
        PRINT*
        DO I=1,N
           DO J=1,N
              Y(I,J)=SQRT(X(I,J)) ! fill with square roots of matrix a
           ENDDO
        ENDDO
            
        END
      
        SUBROUTINE MULTIPLY(X,Y,Z,N,MAX) ! multiply two matrices
        REAL X(MAX,MAX), Y(MAX,MAX), Z(MAX,MAX)
        DO I=1,N
           DO J=1,N
              Z(I,J)=X(I,J)*Y(I,J) ! fill third matrix with products
           ENDDO                           ! of the first and second matrices
        ENDDO
            
        END      

Any suggestions you might have to correct the type of one or both arguments would be greatly appreciated. Supr
0
suprslackr
Asked:
suprslackr
  • 4
  • 2
1 Solution
 
suprslackrAuthor Commented:
Since nobody has answered, I'm going to pose another question. I've figured out how to convert the character to an integer, but the values are being read from the command line 1 character at a time, causing problems when the element values are greater than 10(two digit values or greater). Instead of getting the whole value passed, I only get the first digit, so when the value is 16, i only get 1, 25 gets me a 2, etc. What am I doing wrong? Here's the code, corrected a little from last time:

        PROGRAM P2M
        PARAMETER (MAX = 10) ! maximum row/column dimension is 10
        REAL A(MAX,MAX)      ! initialize three matrices
        REAL B(MAX,MAX)
        REAL C(MAX,MAX)
c        Begin main..................................................      
      INTEGER I,J,N
      CHARACTER P,Y
      CALL GETARG(1,Y)
        READ(Y,*),N
      IF (N.GT.MAX) THEN
         PRINT*,'n greater than',MAX,', program will exit'
         STOP
      ENDIF
   
        DO I=1,N
           DO J=1,N
              CALL GETARG(N*(I-1)+J+1,P)      
              READ(P,*),A(I,J)
           ENDDO
        ENDDO
            
        CALL FILLB(A,B,N,MAX) ! call to fillb
      
        CALL MULTIPLY(A,B,C,N,MAX) ! call to multiply
      
        DO I=1,N
           DO J=1,N      
              WRITE (6,10) C(I,J) ! print product matrix2
10          FORMAT(F9.3,$)
           ENDDO
           PRINT*
           PRINT*
        ENDDO      
            
        END
            
        SUBROUTINE FILLB(X,Y,N,MAX) ! fill matrix b
        REAL X(MAX,MAX), Y(MAX,MAX), TEMP
        PRINT*
        DO I=1,N
           DO J=1,N
              Y(I,J)=SQRT(X(I,J)) ! fill with square roots of matrix a
           ENDDO
        ENDDO
            
        END
      
        SUBROUTINE MULTIPLY(X,Y,Z,N,MAX) ! multiply two matrices
        REAL X(MAX,MAX), Y(MAX,MAX), Z(MAX,MAX)
        DO I=1,N
           DO J=1,N
              Z(I,J)=X(I,J)*Y(I,J) ! fill third matrix with products
           ENDDO                           ! of the first and second matrices
        ENDDO
            
        END      

The code compiles with no errors. The only problem is with the moving of args form the command line into the first matrix. Thanks again, Supr
0
 
DabasCommented:
Hi suprslackr,
Must be 25 years since I did any Fortran work!!! (We had to punch cards in those days, hence GETARG probably did not exist)
You have:

      CHARACTER P,Y
      CALL GETARG(1,Y)

I have the feeling that the program is doing exactly what you are telling it to do.
Get 1 character from the command line.

Maybe I can help you further if you can post the help on GETARG here.

Dabas
0
 
suprslackrAuthor Commented:
GETARG(INTEGER N, CHARACTER C)
where N is the index of the command line argument I want, and C is where I want to store it. Thus GETARG(2,C) should give me the first element of the matrix(because 1st arg is the dimension of the nXn matrix). But when I do the same type of command in, say, C language, I get everything following the first space on the command line up to the last character before the third space. So if in C I put on the command line,
%p2m2 2 4 9 16 25
followed by enter, I would get as output:
 8.000    27.000
64.000  125.000

but with my Fortran program as it is in my previous message, I get
 8.000   27.000
 1.000     2.828

Notice too that the bottom row of the matrix output with Fortran give the results of 1*sqrt(1) and 2*sqrt(2), and not 1*sqrt(1) and 6*sqrt(6). This indicates to me that the digits after the first in the element values are being ignored, and not picked up as the following element values.

I think you're right about this, Dabas: somehow I'm telling it to regard only the first digit of each element. But I think it might have something to do with the formatting on the READ statement used to convert character input into real values. I can't see what I could manipulate within the parameters of GETARG() that would fix it. Hope this gives you what you need to solve the problem. In any case thank for your help. Supr
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
suprslackrAuthor Commented:
In the third sentence of my last message, I should have written SECOND, not third space. Sorry 'bout that. Supr
0
 
DabasCommented:
suprslackr,
C and Fortran handle characters in a very different way, and that explains why you are having the problem.
In C a string variable is a pointer to a place in memory. The string goes from the place pointed to by the pointer, until a Chr(0) is reached.
In Fortran (from memory), a string variable is stored by specifying its starting point and its length.

Hence different outcomes.

Does the second argument of GETARG HAVE to be a character? Can it not be a numeric type?

Dabas
0
 
suprslackrAuthor Commented:
With your suggestions, I fixed it. I was using a single character variable P, when I should've been using a character array P*N, where N is the largest number of digits I expect to have. I think I'll try n=255, and change it if that doesn't work. Thanks a lot, Supr
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now