0

I'm trying to write a short F77 programme, which asks the user to type in the name of datapoint with its 10 values. The absolute number of data points (name+10values) should be variable.

Because Fortran doesn't accept mixed arrays, I'm trying to write a 1D array with the names and a 2D array with the values. However the 1D array does not seem to work and I don't know what I'm doing wrong.

   implicit none
   real x, 
   integer ndatapoints, i,j
   character names(1,100)*10
   dimension x(10,10)

   do i= 1, ndatapoints
     read(5,*) (names(i), i=1, ndatapoints),(x(i,j),j=1, 10)
   end do

   do i=1,ndatapoints
     write(6,*)(x(i,j),j=1,10)
     write(6,*)(names(i),i,ndatapoints)
  end do
4
  • Welcome. Don't put any greetings in your posts. Don't put tags in the title if not necessary. Use general tag fortran and add a version tag if necessary. Commented Nov 8, 2016 at 23:31
  • You should write what is the problem with your code. Any error messages? Which ones? Wrong results? How do they look like? Never used it doesn't work, that doesn't say anything. This phrase does not belong to any good question. Commented Nov 8, 2016 at 23:33
  • names isn't declared as a rank-1 array, but a rank-2. But you then reference it as rank-1. So, what error do you get? Commented Nov 8, 2016 at 23:40
  • 1
    Look into derived data types of f90. It is in essence what you want, a mixed type array. Not sure why you'd want to limit yourself to f77 and fixed form source code unless you enjoy making your life harder. Just avoid the space bar before you start typing and you are writing f90 code. Commented Nov 9, 2016 at 7:05

2 Answers 2

3

I am a bit old school, and this seems to work for me:

CHARACTER(LEN=100), DIMENSION(10)     :: names
REAL              , dimension(10,10)  :: X

do i= 1, ndatapoints
  read(5,*) names(i), x(i,:)
end do

I am not sure what ndatapoints should be, but generally along the lines shown. I would probably use CHARACTER(LEN=128), just because.

And I am 90% sure you really want it like this with i (row/col) swapped:

do i= 1, ndatapoints
  read(5,*) names(i), x(:,i)
end do  
Sign up to request clarification or add additional context in comments.

2 Comments

Worth noting this is Fortran 90, not 77. But looks correct, apart from the issue that 10 should probably be ndatapoints instead.
Yeah @Vladimir you're right it is more of an F90 style.
2

character names(1,100)*10

is a 2D array of shape 1x100 which has elements strings of length 10.

1D array would be

character*10 names(100)

Your question is not clear enough to say if that is what you want.

Also some of you values are undefined, like ndatapoints.

This is also clearly wrong:

do i= 1, ndatapoints
     read(5,*) (names(i), i=1, ndatapoints)

and probably should be just

do i= 1, ndatapoints
     read(5,*) names(i)

It is not at all clear where do your bounds 1,10 and 100 come from. Some of them should probably be ndatapoints instead, but hard to say which one.

Wild guess:

integer ndatapoints
parameter (ndatapoints=100)

character names(ndatapoints)*10
real x(ndatapoints,10)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.