0

There is 3 columns in the file i'm reading and I want to average each column and take the std. The code compiles now, but nothing is being printed.

Here is my code:

program cardata
implicit none
real, dimension(291) :: x
intEGER I,N
double precision date, odometer, fuel
real :: std=0
real :: xbar=0



  open(unit=10, file="car.dat", FOrm="FORMATTED", STATUS="OLD", ACTION="READ")
  read(10,*) N
  do I=1,N

  read(10,*) x(I)
  xbar= xbar +x(I)

  enddo
  xbar = xbar/N
  DO I =1,N
std =std +((x(I) -xbar))**2
  enddo
  std = SQRT((std / (N - 1)))

  print*,'mean:',xbar
  print*, 'std deviation:',std
  close(unit=10)
  end program cardata

I am fairly new to this, any input will be greatly appreciated.

Example of car.dat:

date odometer fuel
19930114 298 22.4 
19930118 566 18.1 
19930118 800 18.9 
19930121 960 15.8 
19930125 1247 19.8 
19930128 1521 17.1 
19930128 1817 19.8 
19930202 2079 18.0 
19930202 2342 10.0 
19930209 2511 16.4 
19930212 2780 16.7 
19930214 3024 19.0 
19930215 3320 17.7 
19930302 3560 16.4 
19930312 3853 18.8 
19930313 4105 18.5
chw21
  • 7,970
  • 1
  • 16
  • 31
  • The code looks okay, and it works for me. "Nothing is being printed" is a bit of an ambiguous statement. Can you add: How do you compile it? How do you run it? What happens when you run it? Can you give an example of `car.dat`? – chw21 Mar 08 '17 at 00:52
  • so here is an example of the car.dat: date odometer fuel 19930114 298 22.4 19930118 566 18.1 19930118 800 18.9 19930121 960 15.8 19930125 1247 19.8 19930128 1521 17.1 19930128 1817 19.8 19930202 2079 18.0 19930202 2342 10.0 19930209 2511 16.4 19930212 2780 16.7 19930214 3024 19.0 19930215 3320 17.7 19930302 3560 16.4 19930312 3853 18.8 19930313 4105 18.5 – Saif Aldeen Safwan Altekarli Mar 08 '17 at 00:58
  • first column is date with values 19930114 second column is odometer 566 third column is 18.1. I need to calculate the std and average of each column, not each element in each row. When i run the program it does not show anything. – Saif Aldeen Safwan Altekarli Mar 08 '17 at 00:59

1 Answers1

0

From the car.dat that you gave in the comments, it's surprising that the program doesn't show anything. When I run it, it shows a very clear runtime error:

$ gfortran -o cardata cardata.f90
$ ./cardata 
At line 12 of file cardata.f90 (unit = 10, file = 'car.dat')
Fortran runtime error: Bad integer for item 1 in list input

You seem to be copying code from another example without really understanding what it does. The code, as you wrote it, expects the file car.dat to be in a certain format: First an integer, which corresponds to the number of items in the file, then a single real per line. So something like this:

5
1.2
4.1
2.2
0.4
-5.2

But with your example, the first line contains text (that is, the description of the different columns), and when it tries to parse that into an integer (N) it must fail.

I will not give you the complete example, as I have the nagging suspicion that this is some sort of homework from which you are supposed to learn something. But here are a few hints:

You can easily read several values per line:

read(10, *) date(I), odometer(I), fuel(I)

I'm assuming here that, different to your program, date, odometer, and fuel are arrays. date and odometer can be integer, but fuel must be real (or double precision, but that's not necessary for these values).

You need to jump over the first line before you can start. You can just read the line into a dummy character(len=64) variable. (I picked len=64, but you can pick any other length that you feel confident with, but it should be long enough to actually contain the whole line.)

The trickiest bit is how to get your N as it is not given at the beginning of the file. What you can do is this:

N = 0
readloop : do
    read(10, fmt=*, iostat=ios) date(N+1), odometer(N+1), fuel(N+1)
    if (ios /= 0) exit readloop
    N = N + 1
end do readloop

Of course you need to declare INTEGER :: ios at the beginning of your program. This will try to read the values into the next position on the arrays, and if it fails (usually because it has reached the end of the file) it will just end.

Note that this again expects date, odometer, and fuel to be arrays, and moreover, to be arrays large enough to contain all values. If you can't guarantee that, I recommend reading up on allocatable arrays and how to dynamically increase their size.

Community
  • 1
  • 1
chw21
  • 7,970
  • 1
  • 16
  • 31