?
Solved

FORTRAN 77 Question

Posted on 2004-03-24
6
Medium Priority
?
1,283 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 900 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Six Sigma Control Plans
Suggested Courses

765 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