- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
Link Copied
43 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Thanks!
New exercise
[cpp]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.[/cpp]
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:
[cpp] print 100, xThe 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.100 format (F4.2) [/cpp]
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:
[plain] x = 23.50 write(8,100) x write (*,*) x 100 format (f5.1) [/plain].....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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - ignacio82
New exercise
Thanks!
[cpp]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.[/cpp]
Thanks!
Here's one possible solution to exercise 2 assuming I understand the specification.
[plain]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 [/plain]
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 :'(
This is what I get when I run the program
[cpp]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 [/cpp]
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 :-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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:
[cpp]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 [/cpp]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:
[plain]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$[/plain]
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.....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I'm very confuse about how the code works :'(
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!
[cpp]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,".")[/cpp]
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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:
[plain]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 [/plain]
And here's a sample run:
[plain]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$[/plain]
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - ignacio82
I'm very confuse about how the code works :'(
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!
[cpp]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,".")[/cpp]
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:
[cpp]100 format(A,F6.1," and ",F6.1," is ",F6.1,".")[/cpp]
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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.....)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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":
[plain]1.0 2.0 3.0 4.0 5.0 6.0 7.000 8.0 9.00 10.000 [/plain]So here's my program to read it in:
[plain]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 [/plain]When run, you should get this:
[cpp]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$[/cpp]
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:
[plain]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 [/plain]and then read it back like this:
[plain]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[/plain]
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.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - ignacio82
I'm very confuse about how the code works :'(
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!
[cpp]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,".")[/cpp]
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
This is what I get when I try to compile
PS: I feel that thanks to the help of all of you I'm actually learning a lot
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.
[cpp]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 [/cpp]
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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:
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 :-)
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 :-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Quoting - ignacio82
I'm very confuse about how the code works :'(
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!
[cpp]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,".")[/cpp]
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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)
Thanks for the help!
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)
[cpp]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[/cpp]
Thanks for the help!

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page