Solved

FORTRAN 77 Question

Posted on 2004-03-24
6
1,211 Views
Last Modified: 2013-11-08
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
Comment
Question by:suprslackr
  • 4
  • 2
6 Comments
 

Author Comment

by:suprslackr
ID: 10673317
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
 
LVL 27

Expert Comment

by:Dabas
ID: 10673353
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
 

Author Comment

by:suprslackr
ID: 10673527
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:suprslackr
ID: 10673538
In the third sentence of my last message, I should have written SECOND, not third space. Sorry 'bout that. Supr
0
 
LVL 27

Accepted Solution

by:
Dabas earned 225 total points
ID: 10673589
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
 

Author Comment

by:suprslackr
ID: 10673827
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now