|
|
 |
 |
 |
 |
Fortran Programming Language
|
 |
 |
 |
 |
 |
 |
 |
 |
Simple DO loops
Hi guys, A simple question from a novice here. I have 120 values defined previously in the program as 'lat', where lat(1) = +90 and lat(121) = -90. I want to print these (to the screen for now) as a column, but cycling through each value in turn with an increment of 1 each time. I've tried a couple of approaches, including the following, but I'm only able to either print the first value lat(1) repetitively, or gain errors: do i=1,120,1 print 100, lat(1) enddo 102 format (1x,f9.2, /) This prints the first value repetitively. If I input something like "i=i+1" or "i=lat(1)+1)" I receive errors. But as you can probably see, my aim is to increase lat(1) to lat(2) then lat(3), lat(4)... upto lat(121) in turn. Finally, I need to assign this 'sequence' to a variable called 'collat'. Is there an easy way of doing this? Thanks for your help, smurray444
"smurray444" <smurray @gmail.com> wrote in message news:1179487141.680511.278020@h2g2000hsg.googlegroups.com... > Hi guys, > A simple question from a novice here. I have 120 values defined > previously in the program as 'lat', where lat(1) = +90 and lat(121) = > -90. I want to print these (to the screen for now) as a column, but > cycling through each value in turn with an increment of 1 each time. > I've tried a couple of approaches, including the following, but I'm > only able to either print the first value lat(1) repetitively, or gain > errors: > do i=1,120,1 > print 100, lat(1)
print 100, lat(i) ! Note : the letter i not numeric 1 > enddo > 102 format (1x,f9.2, /) > This prints the first value repetitively. If I input something like > "i=i+1" or "i=lat(1)+1)" I receive errors. But as you can probably > see, my aim is to increase lat(1) to lat(2) then lat(3), lat(4)... > upto lat(121) in turn. > Finally, I need to assign this 'sequence' to a variable called > 'collat'. Is there an easy way of doing this?
What do you mean ? If you show us what you want to achieve, then maybe someone can help you. If "collat" is an array, same as "lat" then just collat = lat will do the assignment (F90 and above) Or you could do it in a do loop similar to the print above.
> Thanks for your help, > smurray444
I missed this bit : either : > print 100, lat(i) ! Note : the letter i not numeric 1
change 100 to 102 or >> 102 format (1x,f9.2, /)
change 102 to 100 Les
On May 18, 7:19 am, smurray444 <smurray@gmail.com> wrote: > Hi guys, > A simple question from a novice here. I have 120 values defined > previously in the program as 'lat', where lat(1) = +90 and lat(121) = > -90. I want to print these (to the screen for now) as a column, but > cycling through each value in turn with an increment of 1 each time.
In Fortran 90 and later standards, I think write (*,"(1x,f9.2)") lat(l:120:l) does what you want -- note that the format string is "recycled".
In article <1179487141.680511.278@h2g2000hsg.googlegroups.com>, smurray444 <smurray @gmail.com> wrote: > I have 120 values defined > previously in the program as 'lat', where lat(1) = +90 and lat(121) = > -90. I assume there is a typo in this sentence somewhere, but your array appears to have 121 elements, not 120 elements. In addition to the other corrections that have been posted, you should make sure that your array indices are consistent with the declared dimension of your array. Also, fortran arrays do not have to begin with 1. This problem seems suited for an index mapping that matches directly your problem. For example, if NMAX is the number of steps in each hemisphere, then it might make sense to do something like: integer, parameter :: NMAX = 60 real :: lat(-NMAX:NMAX) do i = -NMAX, NMAX lat(i) = (i) * 1.5 enddo or whatever is appropriate for your steps. In a complicated program, the parameter NMAX and the associated arrays might be stored in a module. $.02 -Ron Shepard
In article <1179490126.638861.288@w5g2000hsg.googlegroups.com>, Beliavsky <beliav @aol.com> wrote: >On May 18, 7:19 am, smurray444 <smurray @gmail.com> wrote: >> Hi guys, >> A simple question from a novice here. I have 120 values defined >> previously in the program as 'lat', where lat(1) = +90 and lat(121) = >> -90. I want to print these (to the screen for now) as a column, but >> cycling through each value in turn with an increment of 1 each time. >In Fortran 90 and later standards, I think >write (*,"(1x,f9.2)") lat(l:120:l)
Eh? The l in (l.120.l) are both lower case L not digit 1 (one), and even if you correct that you omit lat(121). Any of these will do the job starting at the north: write (*,"(1x,f9.2)") lat(1:121) in F90 or later write (*,'(1x,f9.2)') lat in f77 or later if lat was declared with size 121. [Formats in " " not ' ' aren't standard f77.] write (*,'(1x,f9.2)') (lat(i),i=1,121) in f77 or later To start at the south and work north you need stride -1: write (*,"(1x,f9.2)") lat(121:1:-1) or write (*,'(1x,f9.2)') (lat(i),i=121,1,-1) Another matter: if lat was declared only with DIMENSION lat(121) then it will be integer (as +90 and -90 are), and must be written with an I format not the F above, but I suspect lat(2) should be +88.5 if the lat values are equally spaced. Then lat must be declared as real (or double precision), e.g. REAL lat(121). -- John Harper, School of Mathematics, Statistics and Computer Science, Victoria University, PO Box 600, Wellington 6140, New Zealand e-mail john.har@vuw.ac.nz phone (+64)(4)463 5341 fax (+64)(4)463 5045
|
 |
 |
 |
 |
|