0

*i made an earlier post to do with my assignment and i managed to get much appreciated help, however i am stuck once again. i am in no way trying to get my homework done or anything like that i just dont understand it. the assignment tells me to 'Within your code you should define an array of your vector TYPE with 5 elements. Your code should then open the supplied .dat file to read in the five vector values' and also nstead of reading in each vector into the array separately, you should instead use a loop from 1 to 5 to read all of the required vectors. This will leave you with a vector array, which will be notated as Vi , where i represents the index of the particular element of the array. You should then perform, and output (with suitable notes output indicating which output is which) the following calculations:

L = V1 + (V2 − V3)
M = V2 ● V4
N = V1 × V5'

so far i have done this and i am more than certain that its wrong id just like a giant push in the right direction thanks.*

program assign_9_2_main

    USE assign_9_2_module

      type(myvector), dimension(5) :: varray
      integer :: i, L, M, N
    OPEN(5,FILE='vectors.dat')
    READ(5,*) varray(1)
    DO i = 1, 5, 1
    end do
     CLOSE(10)

    L=varray(1)+(varray(2)-varray(3))
    M=varray(2)*varray(4)
    N=varray(1)*varray(5)

    write(*,*) L, M, N

    end program assign_9_2_main

when i compile it i get an error message saying: L=varray(1)+(varray(2)- varray(3)) 1 Error: Operands of binary numeric operator '-' at (1) are TYPE(myvector)/TYPE(myvector) assign_9_2_main.f90:14.2:

        M=varray(2)*varray(4)
          1
        Error: Operands of binary numeric operator '*' at (1) are      TYPE(myvector)/TYPE(myvector)
        assign_9_2_main.f90:15.2:

        N=varray(1)*varray(5)
          1
        Error: Operands of binary numeric operator '*' at (1) are TYPE(myvector)/TYPE(myvector).


clearly i'm doing something wrong please can someone help
  • Are you sure you're meant to be using a derived type `type(myvector)` here? That's the problem, because you haven't defined how to multiply together two `myvector` types. – Kyle_S-C Mar 18 '15 at 23:48
  • hi thanks a lot for the reply. i wasnt sure, but if i remove it then i get an error message of: dimension(5) :: varray 1 Error: Unclassifiable statement at (1) assign_9_2_main.f90:8.17: READ(5,*) varray(1) 1 Error: Syntax error in READ statement at (1) – john henry Mar 18 '15 at 23:50
  • So it needs to have a type, but perhaps something like `real`. So your declaration would be `real, dimension(5) :: varray` or equivalently `real :: varray(5)` – Kyle_S-C Mar 18 '15 at 23:57

2 Answers2

0

So, based on what you've said in comments, I would suggest something like the following:

program assign_9_2_main

use assign_9_2_module
! Try to always put implicit none here, it prevents what are called implicit types
implicit none

! You can provide the dimensions of an array variable in brackets, rather than a 
! dimension(x, y, ...) statement.
real :: varray(5)
integer :: i, l, m, n

! Avoid 5 as a unit number, it's often got a special meaning (standard input)
! Also avoid 0, 6, 100, 101 and 102. They're (sometimes) special too.
open(15, file='vectors.dat')
do i = 1, 5
    read(15, *) varray(i)
end do
close(15)

! It's a matter of personal preference, but I like whitespace around operators.
l = varray(1) + (varray(2) - varray(3))
m = varray(2) * varray(4)
n = varray(1) * varray(5)

write(*, *) l, m, n

end program assign_9_2_main

I've added in some stuff in comments in the source, just as pointers for general Fortran things. And in general, I find that putting comments in your own code really helps for when you come back to look at something. Fortran can make very light work of some things (e.g. array operations), but makes other operations very tricky indeed. Having some explanation scattered about is very helpful.

Kyle_S-C
  • 1,107
  • 1
  • 14
  • 31
  • thank you so much its made everything clear too, it's nice to know i wasnt too far off too. i cant thank you enough – john henry Mar 19 '15 at 00:27
0

Try:

OPEN(10,FILE='vectors.dat')
DO i = 1,5
  READ(5,*) varray(i)
END DO
CLOSE(10)

Two notes: The DO loop was empty and the file open/close where not operating on the same file number.

John Alexiou
  • 28,472
  • 11
  • 77
  • 133