Learning how to code

Learning how to code

Hi,

I'm trying to learn how to program in fortran. I'm reading "Numerical Methods in Economics by Kenneth L. Judd"

Can someone recomend me a forum to ask questions (very basic questions) about how to code?

Thanks a lot

Ignacio

44 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Ignacio,

I don't have an answer to your question but I can comment on your choice of language.

Hope I don't get in trouble for saying this here, but if you're just learning how to code, and especially if you're more interested in economics than engineering and physics, then perhaps Fortran isn't the place to start --- unless you have a good reason for choosing it (?).

In my rather limited understanding of the world of economics, quantitative finance, etc, I'd be suprised at employers looking for Fortran experience -correct me if I'm wrong. As cool as I think modern (90/95/2003) Fortran is, it's more often seen in physics and engineering than finance &c.

Employers in eco/finance/quant - even the number-crunchers - will more likely want experience based in a more functional and/or object-oriented language, rather than an imperitive language like Fortran. Or, if they want something imperitive, it will be C. They also may like Matlab experience it seems.

This means languages like Smalltalk, C++, Scala, OCaml, maybe C# or F#, Python, and maybe even some Lisp derivative. A good strong basis in C is always a good thing, and C++ will be very broadly sought; competence in other languages like Smalltalk will be less broadly useful but also might help net that one job where that's what they're looking for (same can be said for Fortran, too).

Anyway....

IMO by far the best language for learning programming is Python. It also happens to be a fantastic language for learning numerical methods, which it sounds is what you're after - mind you it's not a good language for actually *implementing* numerical methods since it's sloooow, but it's good for *learning* them. It can be programmed in imperitive or functional style, it's object-oriented, and it's insanely ubiquitous as well.

The other cool thing about Python is that it has many syntactical features in common with modern Fortran. :)

Here's what I recommend for the python setup on Ubuntu. Of course you'll already have python, but you'll also want the following:
- ipython
- numpy (numerical python)
- scipy (scientific python)
- matplotlib (for 2D matlab-like plotting)
- mayavi2 (for 3D plotting)

This will give you an interactive graphical environment where you can play around wth numerical methods and get immediate visible results. It's a lot like Matlab - not a copy, mind you, but similar functionality. And FREE.

All of this is super super easy to install using Ubuntu's package manager.

Along with scipy, you'll get f2py, which is Pearu Peterson's fortran-to-python interface generator. With f2py, you can take your Fortran module source code (so long as it doesn't have derived types, and some other limitations - altough the f2py team is working on adding these features) and compile it to a python module, like so, where "foo_mod" is the name of the python module you'd like to generate, and the underlying Fortran module is called bar_mod:

$ f2py -c --fcompiler=intelem -m foo_mod yercode.f90

(Oh by the way - if you are new to linux, get used to the command line; see Applications -> Accessories -> Terminal on standard Ubuntu 9.04 install.... you probably already knew that tho....a book on the bash shell isn't a bad idea either...)

Start up ipython from command line and start using your Fortran like so:

$ ipython -wthread <---- good habit to get into calling ipython w/ -wthread flag, but not necessary

In [1]: import foo_mod

In [2]: foo_mod.bar_mod.func()
(etc etc)

...where your Fortran code is (for example):

module bar_mod
contains
function func()

end function func
end module bar_mod

This works pretty well in my experience. That way you can also learn Fortran, and have a nice graphical front end to plot out your calculations on the fly and get immediate feedback, which is always a good thing when you're learning stuff. You can do the same thing for Python integration with C or C++ instead of Fortran, using SWIG, Pyrex, or any number of other packages.

And, if you decide to continue with Fortran, then welcome to the club. I have heard many stories where mentioning the word "Fortran" on the resume landed the candidate the job, so it can be a definite asset. Just depends on the field and the niche...

Good luck,
Peter

Ignacio,

Hey, I checked out some of the code from that book at:

http://bucky.stanford.edu/numericalmethods/PUBCODE/DEFAULT.HTM

(hope it's ok to post that link here....)

Very cool. OK so Judd's book looks like it makes heavy use of the netlib repositories. This is good.

Most of the netlib code is written in Fortran 77, and the rest is typically either Fortran 90+, C, or C++.

Despite anything I said previously, if you are doing numerical stuff of this nature then knowing Fortran 77 is a *DEFINITE* asset. However, I still would not recommend it as a first and only language - just my opinion. On the other hand, Fortran 77, however, is rather simple and easy to read, so it's pretty easy to pick it up fast enough to start to make sense of the code in this book..... In fact you will likely encounter people in econ/quant finance who are completely unaware that under the hood they are actually using quite a bit of Fortran 77. :)

BTW I doubt you'll encounter any problem at all using f2py to compile netlib Fortran 77 code to a python module. Same for using SWIG for any C or C++ code you encounter. PLUS, with numpy/scipy, you may actually find that somebody else has already done it for you - much of the netlib repository code has been built into scipy.

Peter

Thanks for the reply.
The reason I want (need) to learn fortran is because I'm going to take a class next semester that is going to use it (advanced macro in a phd program).
Thanks again

Quoting - ignacio82
Hi,

Can someone recomend me a forum to ask questions (very basic questions) about how to code?

Here itself you will find good people to help you!
Also, you can try google groups:comp.lang.fortran

Quoting - roddur
Here itself you will find good people to help you!
Also, you can try google groups:comp.lang.fortran

I wasn't sure about asking here...
This is the first thing I'm trying to do (i guess is easy)

1. Write a subroutine (procedure, etc.), called ADDXY, that takes as input two scalars, x and y, and
produces x + y as output.
Write a program that reads two scalars, x and y applies ADDXY to x and y, and prints the answers in a readable fashion. "THE SUM OF x AND y IS "

If someone can help me I would really apreciate it :)

Thanks!

Here's a start.

program sample
  real x,y,res

  print *,"Input a value for x"
  read *,x
  print *,"Input a value for y"
  read *,y

  call ADDXY(x,y,res)

  print *,"THE SUM OF x AND y IS ",res
end

subroutine ADDXY(x,y,res)
  real x,y,res
  res=x+y
end

$ ifort -V sample.f90
Intel Fortran Intel 64 Compiler Professional for applications running on Intel 64, Version 11.1 Build 20090630 Package ID: l_cprof_p_11.1.046
Copyright (C) 1985-2009 Intel Corporation. All rights reserved.

Intel Fortran 11.1-2536
GNU ld version 2.17.50.0.6-5.el5 20061020

$ ./a.out
Input a value for x
1
Input a value for y
2
THE SUM OF x ANDy IS 3.000000

Ignacio,

Apologies for the big core dump earlier - too much coffee.

As Kevin Davis showed, this forum is a nice place to start. I don't know of anything comparable offhand.

Some comments:

(1) Re books: Looks like you'll be doing mostly Fortran 77. Modern Fortran textbooks treat the 90/95/2003 standards. Fortran 90 is very different from Fortran 77. You probably need something geared specifically for 77. One book I've found handy is "The Essentials of Fortran" by D. Smolarski. It's super cheap, small, and just covers the basics.

(2) Re editors: As I said earlier, on Ubuntu, gedit is probably a good place to start.

(3) Get comfortable working on the command line in linux. For example, that's where you call ifort from. Have you gotten that to work yet, for a simple program?

(4) There are tons of language reference books, but precious few that treat practical aspects of programming, like how do you deal with separate compilation (combining multiple bits of code into one program), linking libraries, debuggers, etc. I recommend "A Scientist's and Engineer's Guide to Workstations and Supercomputers", by Landau and Fink. It may be out of publication for all I know; my copy came with a floppy (!) - it's an old book. Chapter 8 is an excellent intro to the practical aspects of writing Fortran on a unix (and linux) platform.

That's for starters.

Before TOO long you'll want to figure out:

(5) How to actually plot your data instead of just looking at numbers on a screen. For this you can use Intel's array visualizer, although it's not supported anymore: http://www.intel.com/cd/software/products/asmo-na/eng/compilers/226277.htm OR, you can use python, f2py, and matplotlib together, but this might be a bit complicated if you're starting out. Also there's a ton of other stuff out there - pgplot and gnuplot are two examples. For starters, you can just have your code dump numbers to a file and then plot them with pgplot or gnuplot.

Eventually (but not now) you will want to learn:

(6) How to use libraries in your code.

(7) How to automate building (compiling, linking) programs using "make" or something else like "scons".

(8) How to keep track of changes you make in your code - this is called version control. You could use bzr, hg, or svn, but the easiest by far for a person working in isolation is cvs.

(9) How to use a debugger.

(10) How to use a more powerful editor, or an IDE if you want (but I don't suggest an IDE for starting out).

Peter

Note added later: Intel's Array Visualizer is for Windows has NOT been ported to linux. (Ooops!)

Thanks a lot :-) This is a great way to learn for me. I really really apriciate the help.
Here are my questions.

1) I'm runin:

/opt/intel/Compiler/11.1/046/bin/intel64/ifort -V sample.f90

Is it ok if I create a symbolic link in /usr/bin so i just run ifort ? To do it is :

sudo ln -s /opt/intel/Compiler/11.1/046/bin/intel64/ifort /usr/bin/ifort

?

2)What is the option -V ?

3) The first line in the code is "program sample", you don't need to put ! on front to say that is a comment?

4) The second line says that x,y,res are real numbers?

5) You use a lot "*," what is this for?

6) After i compile it to run it i do ./a.out . Is there a way to make it something different like ADDXY.out ?

THANKS A LOT AGAIN!!!!

Hi Peter,

No need to apology, you are all being very nice with me :-)

1) I'm going to get one of those book in a couple of weeks when I'm back in the US (I'm in argentina right now and books here are expensives...)

2)I'm using emacs22-x.

3)I'm confortable with the command line, I've being using linux since 1999 (i'm a user not a linux guru)

4)Eventually I think I'm going to use matlab to plot (we use it last semester)

5-10) I'm going to learn all that in a near future, be ready for questions ;-)

Thanks everyone

Ignacio

Ignacio,

Creating a symbolic link to ifort won't typically work. It breaks stuff. Instead you should source the appropriate shell scripts that setup the environment variables for you so ifort's in your path.

For example, in my .bashrc I have:

[shell]# Intel compiler stuff:
source /opt/intel/Compiler/11.1/046/bin/iccvars.sh intel64
source /opt/intel/Compiler/11.1/046/bin/ifortvars.sh intel64[/shell]

If you're using csh or tcsh instead of bash then you need to source the csh/tcsh versions of these scripts instead, which are in the same directory. If you're using ksh or zsh, then I dunno.... :)

Regards,
Peter

Quoting - mrentropy
Ignacio,

Creating a symbolic link to ifort won't typically work. It breaks stuff. Instead you should source the appropriate shell scripts that setup the environment variables for you so ifort's in your path.

For example, in my .bashrc I have:

[shell]# Intel compiler stuff:
source /opt/intel/Compiler/11.1/046/bin/iccvars.sh intel64
source /opt/intel/Compiler/11.1/046/bin/ifortvars.sh intel64[/shell]

If you're using csh or tcsh instead of bash then you need to source the csh/tcsh versions of these scripts instead, which are in the same directory. If you're using ksh or zsh, then I dunno.... :)

Regards,
Peter

I'm not a linux guru :'(.
I'm using whatever ubuntu use by default (no idea what)
Should I edit " ~/.bashrc " and add those 2 lines at the end? is this going to let me run the compiler from any directory as if i had a link in /usr/bin ?
Sorry for my ignorance :'(

Thanks

Ubuntu default is bash, so yes, add the two lines as Peter showed to your .bashrc and then execute: source ~/.bashrc

The changes will allow you to just type 'ifort' from any directory (and 'icc' if you need the Intel C++ compiler) without specifying the full path to the compiler.

By adding this to the .bashrc, the same setup will exist when you logout and log back onto your system. The 'source' command is just a short-cut to activate the changes and avoid logging out and logging back in.

I'm trying to change a little the code and is not working :'(
Can somebody explaing me what I'm doing wrong? and how to fix it?

!Exersice one JUDD page 27
program one
  real x,y,add, prod, division 

  print *,"Input a value for x"
  read *,x
  print *,"Input a value for y"
  read *,y

  call ADDXY(x,y,add)

  print *,"THE SUM OF x AND y IS ",add
end

  call MXY(x,y,prod)

  print *,"THE product OF x AND y IS ",prod
end

  call DXY(x,y,division)

  print *,"THE quotient OF x AND y IS ",division
end


subroutine ADDXY(x,y,add)
  real x,y,add
  add=x+y
end

subroutine MXY(x,y,prod)
  real x,y,prod
  prod=x*y
end

subroutine DXY(x,y,division)
  real x,y,division
  division=x/y
end

This is the error I get

[shell]~/Desktop/FORTRAN/CODE/JUDD/INTRODUCTION$ ifort -V one.f90
Intel Fortran Intel 64 Compiler Professional for applications running on Intel 64, Version 11.1    Build 20090630 Package ID: l_cprof_p_11.1.046
Copyright (C) 1985-2009 Intel Corporation.  All rights reserved.
FOR NON-COMMERCIAL USE ONLY

 Intel Fortran 11.1-2536
one.f90(15): warning #5427: Program may contain only one main entry routine
  call MXY(x,y,prod)
--^
one.f90(17): warning #5427: Program may contain only one main entry routine
  print *,"THE product OF x AND y IS ",prod
--^
one.f90(18): warning #5427: Program may contain only one main entry routine
end
^
one.f90(15): error #5507: There are multiple unnamed main program declarations in this file
  call MXY(x,y,prod)
--^
compilation aborted for one.f90 (code 1)
[/shell]

Thanks for the help

I'm also learning how to code, great thread!

http://www.binoculars-connection.com

You have too many END statements in the main program. Simply delete lines 13, 18.

Quoting - Kevin Davis (Intel)

You have too many END statements in the main program. Simply delete lines 13, 18.

Thanks!!! :-)

One more question.

If I use x=6 y=3 the when i run the program i get "the sum of x and y is" but i would like "the sum of 6 and 3 is"

How do you do that?

Thanks!!!

Quoting - ignacio82

One more question.

If I use x=6 y=3 the when i run the program i get "the sum of x and y is" but i would like "the sum of 6 and 3 is"

How do you do that?

A quick method sans any attempt to format the real values is to change the print statement to this:

print *,"THE SUM OF ",x," AND ",y," IS ",res

Which produces:

THE SUM OF 6.000000 AND 3.000000 IS 9.000000

If you want a bit nicer output, you'll need some formatting. One method is to use the following WRITE instead of PRINT.

write (*,'("THE SUM OF ",F6.1," AND ",F6.1," IS ",F6.1)') x,y,res

Which produces:

THE SUM OF 6.0 AND 3.0 IS 9.0

That is great :-)

Questions:
1) What is F6.1 ?
2) Can somebody answer my questions 2 to 6 in the reply #8?

Thanks!!!

Quoting - ignacio82

That is great :-)

Questions:
1) What is F6.1 ?
2) Can somebody answer my questions 2 to 6 in the reply #8?

Thanks!!!

(1) F6.1 is a format specifier. It means "print a real number (that's the 'F' part), make the field (the space on the screen used to print it) 6 characters wide, and display one digit after the decimal place". Why does 'F' mean "print a real number"? Well, you might want to print a real number in "F"ixed format, or in "E"xponential format. Hence, F and E are the two basic format descriptors for printing real numbers. A third is "ES" for, true scientific notation.

Incidentally all computer languages have this problem about how to print out real (or floating-point) numbers, because USUALLY you don't want to know the number to the full equivalent decimal precision to the internal binary representation of the number. So there's always some default behavior and then some formating syntax to learn.

(2.2) The -V flag is just an (undocumented) verbose "version" flag that tells ifort that - in addition to whatever else it's doing - to print out all the version info you might need if you're gonna ask support for help. (Anybody correct me if I'm wrong on this one.)

(2.6) Yes, there is a way to do this: the "-o" flag.
Instead of doing
$ ifort myprog.f

do this:
$ifort -o myprog myprog.f

and your executable will be named "myprog" instead of "a.out". Some people prefer to append a .x suffix on their executables but most people don't.

Why, you might ask, do you have to type "myprog" twice? One reason is that in most circumstances you're not going to be compiling just one source file into an executable, you'll be compiling - or linking, rather - several.

Eventually you will want to learn about the "-c" flag, which makes an *object* file, so you can do precisely that. This will require learning the difference between compiling and linking.

P.S. BTW technically a.out is also an object file, but it's an executable object file as opposed to a relocatable object file, and the latter are just called "object files" as opposed to "executables", by most people anyway....

Thanks :-) I think I almost fully understand this first exercise. My last question is about the "*," that is all over the code. Can somebody explain that? Thanks

New exercise

2. Write a routine FEVAL that takes as input a function of two variables F and two scalars x and y and
produces as output the value of F(x, y). Write a program that reads two scalars x and y, applies FEVAL
to  ADDXY together with the scalars x and y, and prints the answers in a readable fashion.

Thanks!

Quoting - ignacio82
Thanks :-) I think I almost fully understand this first exercise. My last question is about the "*," that is all over the code. Can somebody explain that? Thanks

New exercise

2. Write a routine FEVAL that takes as input a function of two variables F and two scalars x and y and
produces as output the value of F(x, y). Write a program that reads two scalars x and y, applies FEVAL
to  ADDXY together with the scalars x and y, and prints the answers in a readable fashion.

Thanks!

The *'s are a hangover from the old days, where instead of "*" you would have placed a number. Those numbers would mean one of two things: either it would say something about the format, or about where to send the output. Both of these things are still very useful, but often you just want the default behavior and you have to put down SOMEthing, so you put "*".

In the case of print, the "*" takes the place of a *line number* for a format specification. You could put a format specification just about anywhere in your code, give it a line number, and then use that in your print statement. For example:

       print 100, x
       
100    format (F4.2)

The OTHER thing a "*" often stands for is the input/output device, which in Fortran are refered to by small integers, typically. This dates back to the days before everybody had the same default output device (the screen). So, you might be able to output to a card puncher (I think), a line printer, etc.

By far the most imporant of these are 0,5,and 6. 6=stdout (usual terminal output), 5=stdin(usual terminal input), 0=stderr(usual terminal error output). That's unix/linux parlance. Don't know about Windows...

If you open a file from within your code to read or write to, you can give it your own *different* number and use that for read/write statements.

With basic "print" this doesn't come into play since it just takes a format specifier and not a device specifier, but with write() and read() you'll typically use both. With the former you can use "*" for both of them. Here's an example using the specifiers for one write and not for another:

     x = 23.50
        write(8,100) x
         write (*,*) x
100    format (f5.1)

.....where earlier in the code you had *opened* a file and given it the unit number of "8". The first write statement writes x to the file with unit number 8 with the formatting specified on line 100, and the second just writes it to the screen using the default formatting. (The "format" line above is supposed to have "100" in front of it, but for the life of me I can't seem to get it to show.....)

Peter

Quoting - ignacio82
New exercise

2. Write a routine FEVAL that takes as input a function of two variables F and two
scalars x and y and produces as output the value of F(x, y). Write a program that
reads two scalars x and y, applies FEVAL to ADDXY together with the scalars
x and y, and prints the answers in a readable fashion.

Thanks!

Here's one possible solution to exercise 2 assuming I understand the specification.

program exercise2
  real x,y
  external ADDXY

  write(6,'("Input a value for x")')
  read (5,*)x
  write(6,'("Input a value for y")')
  read (5,*)y

  call FEVAL(ADDXY,x,y)
end

subroutine FEVAL(F,x,y)
  real F,x,y
  write (6,'("THE SUM OF ",F6.1," AND ",F6.1," IS ",F6.1)')x,y,F(x,y)
end

real function ADDXY(x,y)
  real x,y
  ADDXY=x+y
end function ADDXY

I also incorporated changes demonstrating the versatility of the language features mrentropy so nicely explained,and show some of the mentionedcompiler options.

I use -V to be clear in my posts about the compiler used. mrentropy is correct, this logo information is vital when inquiring about possible bugs. It is a documented option, execute: ifort -help

$ ifort -V -o exercise2 exercise2.f90
Intel Fortran Intel 64 Compiler Professional for applications running on Intel 64, Version 11.1 Build 20090630 Package ID: l_cprof_p_11.1.046
Copyright (C) 1985-2009 Intel Corporation. All rights reserved.

Intel Fortran 11.1-2536
GNU ld version 2.17.50.0.6-5.el5 20061020

$ ./exercise2
Input a value for x
6
Input a value for y
3
THE SUM OF 6.0 AND 3.0 IS 9.0

I'm trying to modify that code a little in order to have sum; product and division. But I don't know how to make the program print the answers in the right way :'(

program exercise2
  real x,y
  external ADDXY, MXY, DXY

  write(6,'("Input a value for x")')
  read (5,*)x
  write(6,'("Input a value for y")')
  read (5,*)y

  call FEVAL(ADDXY,x,y)
  call FEVAL(MXY,x,y)
  call FEVAL(DXY,x,y)
end

subroutine FEVAL(F,x,y)
  real F,x,y
  write (6,'("THE SUM OF ",F6.1," AND ",F6.1," IS ",F6.1)')x,y,F(x,y)
end

real function ADDXY(x,y)
  real x,y
  ADDXY=x+y
end function ADDXY

real function MXY(x,y)
  real x,y
  MXY=x*y
end function MXY

real function DXY(x,y)
  real x,y
  DXY=x/y
end function DXY

This is what I get when I run the program

[shell]./exercise2 
Input a value for x
6
Input a value for y
3
THE SUM OF    6.0 AND    3.0 IS    9.0
THE SUM OF    6.0 AND    3.0 IS   18.0
THE SUM OF    6.0 AND    3.0 IS    2.0
[/shell]

Thanks for the help :-)

Ignacio,

The computer's doing exactly what you're telling it to do. You're just not telling it what you think you are. Welcome to the world of debugging, which can be summed up as trying to figure out the difference between what you THINK you're telling the computer to do and what you're REALLY telling it to do.

If you'll look at your code, you'll see that the only line in which a result is printed out is the one in subroutine FEVAL(), which prints "THE SUM OF ".........

There's no statement anywhere that prints "THE PRODUCT OF" or "THE QUOTIENT OF", so where would that text come from? In fact, you ARE printing out the product and the quotient, but you're telling the computer to call the result a sum.... it's just doing what it's told!

You have to choose where to put these statements, and you have two basic options: Either put the print (or write) statements in the procedure calls, or put them in the main program.

My preference is to put them in the main program, so that the procedures are "cleaner". So you can do that like this:

program exercise2
  real x,y, z
  external ADDXY, MXY, DXY
  !
  write(6,'("Input a value for x")')
  read (5,*)x
  write(6,'("Input a value for y")')
  read (5,*)y
  !
  z = FEVAL(ADDXY,x,y)
  write (6,100) "     The sum of ", x,y,z
  z = FEVAL(MXY,x,y)
  write (6,100) " The product of ", x,y,z
  z = FEVAL(DXY,x,y)
  write (6,100) "The quotient of ", x,y,z
  !
100 format(A,F6.1," and ",F6.1," is ",F6.1,".")
  !
end program exercise2

real function FEVAL(F,x,y)
  real F,x,y
  FEVAL = F(x,y)
end function FEVAL

real function ADDXY(x,y)
  real x,y
  ADDXY=x+y
end function ADDXY

real function MXY(x,y)
  real x,y
  MXY=x*y
end function MXY

real function DXY(x,y)
  real x,y
  DXY=x/y
end function DXY

OR, if you'd prefer, you can put the print statements in the procedures, which will make for a bit more lengthy code.

Here's a run:

pwilliam@sarastro:~/samba$ ./a.out
Input a value for x
12
Input a value for y
8
     The sum of   12.0 and    8.0 is   20.0.
 The product of   12.0 and    8.0 is   96.0.
The quotient of   12.0 and    8.0 is    1.5.
pwilliam@sarastro:~/samba$

Notice that output looks a bit funny in that the space taken up by the number is 6 characters (some of which may be blanks), even if the number is just something like 1.0, which only needs 3 spaces.

Fortran's strength is number crunching. It does not produce pretty output and it's not its goal to do so, so don't expect toooo much from it in that regard.

Peter (mrentropy)

P.S. A corrolary to my definition of debugging is that much of debugging consists of swearing at the computer, convinced that the compiler makers messed up somewhere, or some library you're linking to has a bug in it, or even that cosmic rays have flipped a bit in your RAM - until finally you realize that YOU are the one who made the mistake and you've been looking right at it all along.... then you laugh at yourself a bit, and it's a good day b/c you can go home with one less thing driving you crazy.....

I'm very confuse about how the code works :'(

z = FEVAL(ADDXY,x,y)
  write (6,100) "     The sum of ", x,y,z
  z = FEVAL(MXY,x,y)
  write (6,100) " The product of ", x,y,z
  z = FEVAL(DXY,x,y)
  write (6,100) "The quotient of ", x,y,z
  !
100 format(A,F6.1," and ",F6.1," is ",F6.1,".")

how that prints the right answer? :-s
I don't get the (6,100) and the 100 format(A,F6.1," and ",F6.1," is ",F6.1,".")

Can someone enlight me ?

Thanks!

The "6" just says "print this out to the screen". Suppose maybe you have a line printer too (nobody has these anymore... sigh.... VERY fast and handy...) ANYway you might refer to that by the number 7, for example. Then if you wanted the computer to print the results out to the line printer, you'd put 7 in place of 6 in the code.

Don't worry about the 6's. You could replace them all with *'s and you'll get the same result. Same with the 5's in the READ statments. It's just a good habit that will make more sense as you get further along in programming in Fortran.

Here's the same thing without the format statements. Should be easier to understand:

program exercise2
  real x,y, z
  external ADDXY, MXY, DXY
  !
  print *, "Input a value for x"
  read  *, x
  print *, "Input a value for y"
  read  *, y
  !
  z = FEVAL(ADDXY,x,y)
  print *, "     The sum of ", x, " and ", y, " is ", z, "."
  z = FEVAL(MXY,x,y)
  print *, " The product of ", x, " and ", y, " is ", z, "."
  z = FEVAL(DXY,x,y)
  print *, "The quotient of ", x, " and ", y, " is ", z, "."
  !
end program exercise2

real function FEVAL(F,x,y)
  real F,x,y
  FEVAL = F(x,y)
end function FEVAL

real function ADDXY(x,y)
  real x,y
  ADDXY=x+y
end function ADDXY

real function MXY(x,y)
  real x,y
  MXY=x*y
end function MXY

real function DXY(x,y)
  real x,y
  DXY=x/y
end function DXY

And here's a sample run:

pwill@zarathustra:~/samba$ ./a.out
Input a value for x
4.5
Input a value for y
2.3
      The sum of    4.500000      and    2.300000      is    6.800000     .
  The product of    4.500000      and    2.300000      is    10.35000     .
 The quotient of    4.500000      and    2.300000      is    1.956522     .
pwill@zarathustra:~/samba$

Make sense? The *'s just say "do it the usual way - nothing fancy". You want to get fancy, you start using unit numbers (0,5,6, or whatever), and format statements (100, where 100 is the line number for the format statement), etc etc.

Nice :-) Not fancy is good for now, Ill try fancy in a few weeks :P

New exercise

3. Write a routine that declares a one-dimensional array A of length 10, reads in the values of the
components of A from an imput file, computes the sum of the components of A, and outputs the sum

Quoting - ignacio82
I'm very confuse about how the code works :'(

z = FEVAL(ADDXY,x,y)
  write (6,100) "     The sum of ", x,y,z
  z = FEVAL(MXY,x,y)
  write (6,100) " The product of ", x,y,z
  z = FEVAL(DXY,x,y)
  write (6,100) "The quotient of ", x,y,z
  !
100 format(A,F6.1," and ",F6.1," is ",F6.1,".")

how that prints the right answer? :-s
I don't get the (6,100) and the 100 format(A,F6.1," and ",F6.1," is ",F6.1,".")

Can someone enlight me ?

Thanks!

OK here's what's going on:

First line above gets result for z. Should be z = x + y.

Next line says "write (6,100) (blah blah blah...)" which means, write to unit #6, aka the terminal screen you're looking at; write the stuff that comes later (the blah blah blah part), and when you write it, format it using the format code that you find on line number 100.

Line number 100 does not mean the 100'th line of your code. It just means the one and only one line of your code that you've written the number "100" in front of to identity it. You could have called it "256" or "12345" or whatever - it's just an identifier, like your driver's license ID (or whatever).

The next four lines follow the same pattern. The "(blah blah blah)" part consists of a comma-separated list of things that correspond to entries in the format statement on line number 100. That format line is expecting four things, in order: (1) a character string, (2) a real number, (3) a real number, (4) a real number. So that's what we give it.

OK let's look at the format statement:

100 format(A,F6.1," and ",F6.1," is ",F6.1,".")

The format statement is, again, just a comma-separated list. It consists either of what are called format descritpors, such as F6.1 or A, or string literals (not sure if this is the technical term in Fortran-speak), such as " and " and " is ".

So, this format statement says it's expecting to receive (1) a character string (the "A"), (2) a real number (the "F"), (3) another real number (the next "F"), and (4) a third real number (the final "F").

Given these FOUR items, the format statement will then format an output string that consists of:
item #1 (the string that's passed to it), plus
item #2 (the first real number that's passed to it, formatted according to F6.1), plus
the sequence of characters " and ", plus
item #3 (the second real number that's passed to it, formatted according to F6.1), plus
the sequence of characters " is ", plus
item #4(the third real number that's passed to it, formatted according to F6.1), plus
the sequence of characters " . ".

And then it sends this output string back to the write statement, which spits everything out as told to output unit #6, yer handy terminal output.

K?

Quoting - ignacio82
Nice :-) Not fancy is good for now, Ill try fancy in a few weeks :P

New exercise

3. Write a routine that declares a one-dimensional array A of length 10, reads in the values of the
components of A from an imput file, computes the sum of the components of A, and outputs the sum

Ignacio,

Well, that's a good exercise to do, and I'm sure that any number of people here would be happy to show you.

HOWEVER...... Some gratuitous advice.....

You will NEVER learn how to code if you don't get your hands dirty. The entire process of learning how to code involves writing lots and lots and lots of code that doesn't work, banging your head against the wall, wandering around muttering to yourself, etc etc, until finally you figure out how to do what you want to do.

If you don't go through this process, you'll never learn how to code. Everything you write will end up being carbon copies of something that you have already learned by rote how to do before. That's pretty useless.

Have you tried yourself a bit first, to see what you can come up with? Ask questions when you hit particular stumbling blocks, but allow yourself to get a little frustrated first - it's good for the soul.

Sorry if that sounds paternalistic.

Best wishes,
mrentropy

P.S. Some fun trivia: unit=2 refers to the card reader (except when it doesn't, like when the card reader is 5), and unit=3 is the line printer (usually). OK show of hands, who has a card reader (crickets chirping.....)

OK Ignacio here's a start: You're going have to figure out how to create (declare) an array with ten "slots" in it. How do you do that? How would you figure out how to do that - do you have a textbook, or language reference, or something? If nothing else, there's the Intel language reference that ifort comes with, although that's probably too daunting if you're just starting off......

Ok so the deal with arrays (standard terminology across all common languages, by the way) is that they're just like boxes in a row with numbers on them, starting with 1 (in the case of Fortran) and going on up to the last one, 10 in this case.

Because of Fortran's loooong history, there are a gazillion different ways of declaring arrays, and types of arrays, and properties (er, attributes) of the array, etc etc. So be prepared to see it done in many different ways.

The old school way to declare an array of 10 reals is like this:

real A(10)

Pretty simple.

The new way of doing it is this:

real, dimension (10) :: A

or:

real :: A(10)

I suggest this last form for now. It's simple and in the modern idiom.

So now if you want to refer to the 4th element of A(), then that's just A(4).

This can be a bit confusing because if you didn't know any better you might think A(4) was a function call instead of an element of an array. Keeping these two things straight can cause confusion that could be alleviated if the standards committee just decided to use different syntax for arrays like in C, where you'd say A[4], but now [] are used for array constructors, so it's a mess... But I digress....

OK here's another hint: You're going to need what in Fortran is called a "do-loop". Every computer language has a construct to do this: it means do such-and-such over and over again until a certain condition is met. For example:

do i = 1,10
print *, i
end do

which
(1) sets i=1,
(2) tests to see if i>10 (which of course it isn't). If it WERE, then the program would jump on past the "end do" line and do whatever came after that. But it's NOT, so it's a moot point.
(3) goes thru the loop (the part between the "do" line and the "end do" line, which in this case is just one statement: "print *, i"). So, it prints "1" to the screen.
(4) pops back up to the top,
(5) increments i by one (that is, sets i to a new value that is equal to one plus the old value, so in this case 2),
(6) tests to see if i > 10 again. Since 2 is not > 10, the computer then
(7) goes thru the loop, printing "2" to the screen.
(8) pops to the top again.
(9) increments i by one again, so that after this step i=3
(10) tests to see if i > 10, which it isn't, so it...
(11) goes through the loop, printing "3" to the screen since i=3.
(etc etc yawn..... you get the idea).... until, finally, 5 nanoseconds later or so, the test at the top fails because i is 11 and 11 > 10, and the program then just jumps right on past the "end do" line and keeps going.

Peter

Peter

Quoting - mrentropy

(10) How to use a more powerful editor, or an IDE if you want (but I don't suggest an IDE for starting out).

Peter

As this post sum up the step by step of coding, in my personal opinion, IDE is waste of time. As far as linux is concerned, vim is nearly the best, if you code clearly.

Quoting - roddur
As this post sum up the step by step of coding, in my personal opinion, IDE is waste of time. As far as linux is concerned, vim is nearly the best, if you code clearly.

I use emacs, but vim is what I'd recommend to somebody who wants to learn the right way. Super-fast. People who use vi or vim are like lightning.

Ignacio,

OK the last clue in how to do what you're trying to do is that you need to figure out how to read in data from a file.

In principle it doesn't seem this should be that hard, but in actual fact it's usually a big pain - except in Perl, in which it's a peice of cake, but this isn't Perl, and that's only if you're reading in text anyway.....

The hard part is getting the file open/read to work right. In practice you have to sit down with your reference manual and make sure you have all the settings just right; try it out and crash a few times before it finally works, etc.

So here's a file I'm going to read into my array a_(10) (the little underscore is my own notation to remind me it's an array and not a scalar). File name is "ascii.data":

1.0
2.0
3.0
4.0
5.0
6.0
7.000
8.0
9.00
10.000

So here's my program to read it in:

program main
  integer :: istat
  real :: a_(10)
  open (unit=9,file='ascii.data',status='OLD', iostat=istat) 
  read (9,*) a_
  close (9)
  print *, a_
end program

When run, you should get this:

pwill@zarathustra:~/samba$ ./a.out
   1.000000       2.000000       3.000000       4.000000       5.000000    
   6.000000       7.000000       8.000000       9.000000       10.00000    
pwill@zarathustra:~/samba$

Truth is, it's better to write the data into the file in binary form and read it back in that way. Reading/writing data in ASCII format is only useful if humans need to read it. Otherwise it's like a Frenchman gives you a book to hold, you translate it into English, and then when he asks for it back you hire somebody else to translate your translation back into French --- not only inefficient, but also inaccurate. ANYway...

You can write your data in binary to file like this:

program main
  integer :: istat, i
  real :: a_(10)
  open (unit=8,file="binary.data",form='BINARY',action='WRITE', iostat=istat, status='NEW')
  !
  do i=1,10
     a_(i) = 1.0 * i
  end do
  !
  write (8) a_
  !
  close(8)
  !
end program

and then read it back like this:

program main
  integer :: istat
  real :: a_(10)
 open(unit=9,file='binary.data',form='BINARY',action='READ',iostat=istat, status='OLD')
  if (istat==0) then
    read (9) a_
    print *, a_
    close (9)
  else
    write(0,*) "Problem opening file; game over dude."
  end if
end program

You can check out what your data looks like in the file "binary.data". If you dump to screen you just get garbage, but if you look in GHex you can see what the actual binary data looks like, which is pretty darn cool.... (unlike Intel Array Visualizer, I have actually personally used GHex and it's very nifty.)

Quoting - ignacio82
I'm very confuse about how the code works :'(

z = FEVAL(ADDXY,x,y)
  write (6,100) "     The sum of ", x,y,z
  z = FEVAL(MXY,x,y)
  write (6,100) " The product of ", x,y,z
  z = FEVAL(DXY,x,y)
  write (6,100) "The quotient of ", x,y,z
  !
100 format(A,F6.1," and ",F6.1," is ",F6.1,".")

how that prints the right answer? :-s
I don't get the (6,100) and the 100 format(A,F6.1," and ",F6.1," is ",F6.1,".")

Can someone enlight me ?

Thanks!

Review Peter's reply #21.

The "6" is the unit number to which the I/O is directed (output in the case of the WRITE), the "100" is the statement label where the WRITE statement will now find further formatting instructions for displaying the text string (e.g. " The sum of") and x, y, zvariables that are listed on WRITE statement.

Given one WRITE statement such as:

write (6,100) " The sum of ", x,y,z

There are four items to be written, the text string " The sum of " and the three variables x,y,z. The FORMAT statement found at statement label 100 contains four corresponding edit descriptors describing how each of these items should be written out.

Given this FORMAT statement:

100 format(A,F6.1," and ",F6.1," is ",F6.1,".")

The text string " The sum of " is written using the "A" edit descriptor appearing first in the format statement. Then, each of x, y, and z variables are printed using "F" edit descriptors, specifically F6.1. The variables are printed in the order specified on the WRITE and positioned in the outputcorresponding to the edit descriptor positionwithin the format statement when merged with the other text strings, like " and ", " is " in the final output.

By using the FORMAT statement, one can apply a similar format to the data being output for variables x,y,z and gain the ability to change the text portion of the WRITE statement to reflect the actual operation the called function performed. You can see that only the text portion of the WRITE statement is different between the three statements.

Hope that helps.

Thanks for all the help. I agree with the idea of try by your self first in order to learn. But the thing is that I don't know the fortran abc yet... When i learned english first i read some stupid book; after that a better book; and after that i tried to write by my self. I'm learning for the first time how to program, I don't know C or something else...
At this moment I don't have any fortran book, and getting one in Tucuman Argentina is hard. So I'll buy one in a couple of weeks when I'm back in the US.

The exercises I'm trying to learn from the introuction of Judd. The book says that I need to know how to do those 6 exersises before I can keep going with the reading

About the code in exercise 6. I had 2 ideas about how to do the sum of the elements, but neither is working. One was to do s=sum a(i) the other was to do a loop that sum the elements. Obviusly I'm making some mistake.

Here is what I have.

program exercise3
  integer :: istat, i, s
  real :: a_(10)
  open (unit=8,file="binary.data",form='BINARY',action='WRITE', iostat=istat, status='NEW')
  !
  do i=1,10
     a_(i) =  i
  end do
  !
  write (8) a_
  !
  close(8)
  !
 open(unit=9,file='binary.data',form='BINARY',action='READ',iostat=istat, status='OLD')
  if (istat==0) then
    read (9) a_
    print *, a_
    close (9)
  else
    write(0,*) "Problem opening file; game over dude."
  end if
!
!
s=0 
do i=1,10
s=s+a_(i)
end do
print s
end program

This is what I get when I try to compile

[shell]$ ifort -V -o exercise3.x exercise3.f90
Intel Fortran Intel 64 Compiler Professional for applications running on Intel 64, Version 11.1    Build 20090630 Package ID: l_cprof_p_11.1.046
Copyright (C) 1985-2009 Intel Corporation.  All rights reserved.
FOR NON-COMMERCIAL USE ONLY

 Intel Fortran 11.1-2536
exercise3.f90(28): error #5503: Variable 'S' has not been assigned a label in this program unit.
print s
------^
compilation aborted for exercise3.f90 (code 1)
[/shell]

Thanks!

PS: I feel that thanks to the help of all of you I'm actually learning a lot

Quoting - ignacio82
This is what I get when I try to compile

[shell]$ ifort -V -o exercise3.x exercise3.f90
Intel Fortran Intel 64 Compiler Professional for applications running on Intel 64, Version 11.1 Build 20090630 Package ID: l_cprof_p_11.1.046
Copyright (C) 1985-2009 Intel Corporation. All rights reserved.
FOR NON-COMMERCIAL USE ONLY
Intel Fortran 11.1-2536 exercise3.f90(28): error #5503: Variable 'S' has not been assigned a label in this program unit.
print s
------^ compilation aborted for exercise3.f90 (code 1) [/shell]

There's a minor typo on line 28 as the compiler highlighted.Here's a hint, the line is missing something but what is it missing?

Re-read Peter's reply #21 again, specifically his discussion of the line number for a format specification for PRINT.

While not related to the error, you declared S as an integer and you probably wanted it to be real. It would be a fun exercise for you to leave it declared as an integer, fix the syntax error in line 28, compile and run and see what line 28 prints for S. Then, declare S as a real and re-compile and re-run the program, and now what does line 28 print?

Quoting - ignacio82
Thanks for all the help. I agree with the idea of try by your self first in order to learn. But the thing is that I don't know the fortran abc yet... When i learned english first i read some stupid book; after that a better book; and after that i tried to write by my self. I'm learning for the first time how to program, I don't know C or something else...
At this moment I don't have any fortran book, and getting one in Tucuman Argentina is hard. So I'll buy one in a couple of weeks when I'm back in the US.




Oh, that explains a lot! Well, in that case, nevermind what I said. If you don't even have a basic language reference, then it's pretty hard to just try things out on your own. Apologies.

Check these out:
http://www.stanford.edu/class/me200c/tutorial_77/

http://vergil.chemistry.gatech.edu/courses/cwcs/pdf/f77Basics.pdf

http://www.obliquity.com/computer/fortran/

http://www.tat.physik.uni-tuebingen.de/~kley/lehre/ftn77/tutorial/basics.html

I think the first of these is probably the best place to start, but see for yourself.

I would emphasize that it appears to me that the book you mentioned uses Fortran 77. (Correct me if the newer editions do not - how do you know? Well, post a sample code from the book and we can tell you if it's Fortran 77 or something else).

This is important!!!!: Ken's examples were consistently in the 77 dialect, but my examples were in a mixed tongue (sorry).

The "77" refers to the year "1977". Later standards that have been implemented are 90 (1990), 95 (1995), and 2003.

ifort will compile code written in any of these dialects, so it doesn't care.

However, 90,95, and 2003 are collectively called modern Fortran, and differ substantially in idiom from Fortran 77.In contrast, Fortran 77 is an old standby workhorse, and what most people past a certain age think of when you say "Fortran".Since it seems you need to learn Fortran 77, the links I put for you above are specifically for that idiom.

Hope that helps,
Peter

I was close, I'll never forget the *, again :-)

I just have some questions about the "open" in this code. I don't understand the "unit=8" and "iostat=istat" also what is the 8 in "write (8) a_" and the what is the "close(8)" for?

Finally I'm guessing that istat==0 iff the data file is ok and otherthing if the data file has some problem?

Thanks!!!!

PS1: I'm going to post the new exercise tomorrow (after trying and crying for a while)
PS2: Peter, thanks for the links , i'm looking at that rightnow :-)

Ignacio,

Hey, I'm crying over my own problems right now too. ;)

You are right about the iostat=istat part. The variable "iostat" is an optional argument to open(). You pass it a placeholder for it to spit back its result. In this case I called the placeholder istat. Optional arguments are things that you can pass to procedures, but you don't have to. Since you don't have to, there may not be any way for the computer to figure out what argument you are trying to set without you telling it explicitly, which is what you're doing here. Normally with a subroutine you'd just call mysub(arg1, arg2, ...), but open() potentially takes a ton of different arguments so instead you say open (argA=blah, argC= doodle, arg23 = junebug....), for example (of course, argA, argC and arg23 aren't real arguments to open()).

In principle for big codes that are supposed to be stable, have a large number of users, etc, you should always test iostat. In practice for what you are doing there's really no need, and I should have just left that out. If it fails, you will know, believe me. :)

Now as for the "8": This is just a label that you make up. You could have replaced "8" everywhere with "23487" and it would have worked exactly the same.

Why do you need a label? Well, who's to say you won't open *several* files at once, instead of just one? When you say read(), which file is it supposed to read from? For that matter, how does it know to read from the file you opened, instead of just asking for input from the terminal like in the earlier programs you wrote?

In many other languages, you can give more human-readable names to files that you open, like "myinputfile" or whatever. In Fortran, you given them a number you pull out of a hat. Same thing.

Just make sure not to use 0,5,or 6. Those are taken. And just to be safe, a larger number is better, in case you try running your code on some machine where they've set up 4 (or whatever) to be a tape drive or what have you, e.g.

As for the close(8):

This just closes the file after opening it. Same as opening a document in a word processor. When you're done, it's good manners to close it. That way you don't forget that you had it open and accidentally type something in when you didn't mean to. Same principle here.

You don't HAVE to close the file; it'll get closed anyway when the program finishes. It's just a good habit, like flossing your teeth, and IMO more important than testing iostat. My feeling is you can skip iostat, but don't skip close(). But that's just me.

Peter

Feel free to ignore this posting. I didn't notice that I was only at the end of the first page in this thread and not at the end of the thread, so this does not take into account the intervening replies. To the best of my knowledge, nothing in it is incorrect, but it redundantly answers questions already addressed in this thread. -KWH

Quoting - ignacio82
I'm very confuse about how the code works :'(

z = FEVAL(ADDXY,x,y)
  write (6,100) "     The sum of ", x,y,z
  z = FEVAL(MXY,x,y)
  write (6,100) " The product of ", x,y,z
  z = FEVAL(DXY,x,y)
  write (6,100) "The quotient of ", x,y,z
  !
100 format(A,F6.1," and ",F6.1," is ",F6.1,".")

how that prints the right answer? :-s
I don't get the (6,100) and the 100 format(A,F6.1," and ",F6.1," is ",F6.1,".")

Can someone enlight me ?

Thanks!

The convention in Fortran is that one reads from and writes to "units" that, for the most part are identified by numbers. Thus, when the program says "write (6, ...", it is saying write to unit number 6. In the early days of Fortran (when it was still spelled FORTRAN), unit 6 was usually preconnected to a printer. These days, in Linux compilers, it is likely (but not guaranteed) to be connected to standard output. For about the last 30 years, Fortran has offered the option of putting a "*" rather than a number at this point in the statement, with a guarantee that this will write to the default output unit. Thus, I would encourage you to use "write (*, ..." in place of "write (6, ...".

The next item in the write statements is a format specification. In this case "100" is a reference to the statement "100 format (...)" at the end of the fragment. This statement describes how the items in the list part of the write statement are to be layed out on the output line. The first edit descriptor ("A") takes a character string value from the write list (in your example, a literal character constant) and puts that on the line. The next ("F6.1") takes a floating point value and puts it out in six characters with one after the decimal point. The next puts out the literal characters " and ". The next "F6.1" works like the previous one, but using the next number in the write list. We then have more literal output (" is "), another number ("F6.1"), and one last character of literal output (".").

There are other possible ways of specifying the format:
- A character constant (or other character expression) whose value looks like the parenthesized part of a format statement. For example, the character constant '(A,F6.1," and ",F6.1," is ",F6.1,".")' would have the same effect as referencing statement 100 in your fragment.
- A "*" denote "list-directed" formatting, giving the compiler license to lay out the items from the write list as it wishes. (This is convenient for debugging output or other output where you don't care much about its exact appearance.)

I hope this answers your questions.

Finally, I hope you won't be offended by what I'm about to say, but asking questions at this level in a forum like this is highly inefficient (and thus inappropriate). If every beginner sent their questions to hundreds of experienced programmers (which is what you have been doing), that might well drive those experienced programmers away, so noone's questions were answered. I would encourage you to do the following:

1. Get yourself an introductory book on Fortran programming and read it.

2. Seek local support. Try to find a friend or colleague that already knows Fortran, or perhaps a teaching assistant or help desk that can address your questions. Questions asked and answered locally are less wasteful of resources and likely to be answered more thoroughly and more quickly.

3. If you have a problem whose answer you can't find in you book or from your local resources, then asking questions in a wider forum can be appropriate. For questions specific to the Intel compiler, this is certainly the right place. For more general questions, a more general forum ilke comp.lang.fortran might be better, especially if you are trying to understand some legacy code originally written for some other compiler, but if you don't overdo it, you will likely still find people in this forum who can help you.

I do wish you success in your endeavors with Fortran.

-Kurt

Thanks for your help Kurt, your explanation is great.

About asking question here... I wasn't sure first but roddu told me that this forum was a good place to ask questions (reply #4); I got a lot of great answers; rsruss888 (#14) mention that he is also learning from the post; and finally no admin from the forum told me that I was braking some rule or that this forum is only for gurus....

If the people want to stop answering my questions or an admin want to close the post I will understand.

Thanks everyone for the help!

Ignacio

PS: I'm not posting questions right now because I'm trying to read and do some stuff by my self.

Another question:

4. Write a routine that declares a two-dimensional array A with 5 rows and 3 columns, reads in the
values of the components of A from an input file, computes the sum of the components of each row of
A, and outputs these sums.

I know that my code is wrong, but I have no clue about how to fix it (if someone feels that this question is too easy for this forum, there is no need to reply)

program exersice4
  integer :: istat, i, j
  real :: a_(5,3), s
open (unit=8,file="binary2.data",form='BINARY',action='WRITE', iostat=istat, status='NEW')
  !
  do i=1,5
     do j=1,3
     a_(i,j) =  i/j
    end do
  end do
  !
  write (8) a_
  !
  close(8)
  !
 open(unit=9,file='binary2.data',form='BINARY',action='READ',iostat=istat, status='OLD')
  if (istat==0) then
    read (9) a_
    print *, a_ 
    close (9)
  else
    write(0,*) "Problem opening file; game over dude."
  end if
!
!
s=0 
do j=1,3
do i=1,5
s=s+a_(i,j)
end do
print *,s
end do 
end program

Thanks for the help!

The 6 part is a holdover from 30 years ago, before * was introduced. In the old days, unit 6 was the usual (but not universal) designation for the standard output device (printer, screen). The A format field matches the literal character string of the write, and F6.1 specifies a format of 6 numerical characters, including a decimal point with 1 place after decimal.
Assuming you have a reasonable textbook, that text might skip over some of the more archaic features which wouldn't normally be required in your beginning efforts. Even better would be if it didn't mix styles from different eras, as this example does.

Quoting - tim18
The 6 part is a holdover from 30 years ago, before * was introduced. In the old days, unit 6 was the usual (but not universal) designation for the standard output device (printer, screen). The A format field matches the literal character string of the write, and F6.1 specifies a format of 6 numerical characters, including a decimal point with 1 place after decimal.
Assuming you have a reasonable textbook, that text might skip over some of the more archaic features which wouldn't normally be required in your beginning efforts. Even better would be if it didn't mix styles from different eras, as this example does.

Hi,
I would strongly suggest buying a good Fortran book: I use and would highly recommend:
Fortran 95/2003 For Scientists and Engineers by Stephen J. Chapman.
It is text book with many examples to work on.

Secondly, nothing beats having the Intel Fortran Manual at your fingertips. Although I use Fortran only on Linux,
I have it also as an icon on my Windows Laptop. Thatwas one of the most productive decisions I have made.

Thirdly, even though I work on mainly large projects, I have a test directory containing a number of subdirectories
each of which is a small experiment I needed to make. Typically each directory contains a small source file and a Makefile. These test directories are then reminders or snapshots of how I solved particular problems.

So, as an example, if I needed to understand something about unit 6 or *, I create a new directory, write a small example and try to understand what is going on. The code, some comments and the makefile are then invaluable
later on when you need to incorporate or understand what is going on in larger projects.

regards
Mike

Leave a Comment

Please sign in to add a comment. Not a member? Join today