|
Prev: Call Array valued Fortran function from C
Next: Advice on how to keep track of allocated memory
From: Luka Djigas on 19 Apr 2008 20:58 What would be the shortest way to sum some elements of an array? For example from 5 to 50. I know the "obvious" way to do it: s=0. do 1 i=5,50 s=s+a(i) 1 continue but since I'm currently doing something which requires quite a number of summing, I was wondering is there something shorter ? Any idead appreciated regards Luka
From: Craig Dedo on 19 Apr 2008 21:40 "Luka Djigas" <ldigas@@gmail.com> wrote in message news:pu4l04dnt3apg1hf1a1jvesp0gf3jttiol(a)4ax.com... > What would be the shortest way to sum some elements of an array? For > example from 5 to 50. > > I know the "obvious" way to do it: > s=0. > do 1 i=5,50 > s=s+a(i) > 1 continue > > but since I'm currently doing something which requires quite a number > of summing, I was wondering is there something shorter ? > > Any idead appreciated > > regards > Luka This most likely is a homework assignment. However, if I am mistaken and Luka is new to Fortran . . . There is always the SUM() intrinsic function. It can sum an array along any dimension or the whole array and can even sum those array elements for which the value of a corresponding MASK array is true. RTFM. -- Craig Dedo 17130 W. Burleigh Place P. O. Box 423 Brookfield, WI 53008-0423 Voice: (262) 783-5869 Fax: (262) 783-5928 Mobile: (414) 412-5869 E-mail: <cdedo(a)wi.rr.com> or <craig(a)ctdedo.com>
From: dongyuanxun on 20 Apr 2008 00:10 On Apr 20, 8:58 am, Luka Djigas <ldigas@@gmail.com> wrote: > What would be the shortest way to sum some elements of an array? For > example from 5 to 50. > > I know the "obvious" way to do it: > s=0. > do 1 i=5,50 > s=s+a(i) > 1 continue > > but since I'm currently doing something which requires quite a number > of summing, I was wondering is there something shorter ? > > Any idead appreciated > > regards > Luka You can also prepare a function called sum(),if your IDE doesn't support the sum() intrinsic function.
From: Gerry Ford on 20 Apr 2008 00:12 "Craig Dedo" <cdedo(a)wi.rr.com> wrote in message news:480a9ef9$0$7077$4c368faf(a)roadrunner.com... > "Luka Djigas" <ldigas@@gmail.com> wrote in message > news:pu4l04dnt3apg1hf1a1jvesp0gf3jttiol(a)4ax.com... >> What would be the shortest way to sum some elements of an array? For >> example from 5 to 50. >> >> I know the "obvious" way to do it: >> s=0. >> do 1 i=5,50 >> s=s+a(i) >> 1 continue >> >> but since I'm currently doing something which requires quite a number >> of summing, I was wondering is there something shorter ? >> >> Any idead appreciated >> >> regards >> Luka > > This most likely is a homework assignment. However, if I am mistaken > and Luka is new to Fortran . . . > > There is always the SUM() intrinsic function. It can sum an array > along any dimension or the whole array and can even sum those array > elements for which the value of a corresponding MASK array is true. RTFM. I thought I was gonna clean up the snippet and write an amusing mask. This *is* wrong, but it doesn't look wrong to me. real(dim =50):: a s=0. do 1 i=5,50 s=s+a(i) 1 continue end -- "A belief in a supernatural source of evil is not necessary; men alone are quite capable of every wickedness." ~~ Joseph Conrad (1857-1924), novelist
From: dongyuanxun on 20 Apr 2008 00:19 On Apr 20, 12:12 pm, "Gerry Ford" <ge...(a)nowhere.ford> wrote: > "Craig Dedo" <cd...(a)wi.rr.com> wrote in message > > news:480a9ef9$0$7077$4c368faf(a)roadrunner.com... > > > > > > > "Luka Djigas" <ldigas@@gmail.com> wrote in message > >news:pu4l04dnt3apg1hf1a1jvesp0gf3jttiol(a)4ax.com... > >> What would be the shortest way to sum some elements of an array? For > >> example from 5 to 50. > > >> I know the "obvious" way to do it: > >> s=0. > >> do 1 i=5,50 > >> s=s+a(i) > >> 1 continue > > >> but since I'm currently doing something which requires quite a number > >> of summing, I was wondering is there something shorter ? > > >> Any idead appreciated > > >> regards > >> Luka > > > This most likely is a homework assignment. However, if I am mistaken > > and Luka is new to Fortran . . . > > > There is always the SUM() intrinsic function. It can sum an array > > along any dimension or the whole array and can even sum those array > > elements for which the value of a corresponding MASK array is true. RTFM. > > I thought I was gonna clean up the snippet and write an amusing mask. This > *is* wrong, but it doesn't look wrong to me. > real(dim =50):: a > > s=0. > do 1 i=5,50 > s=s+a(i) > 1 continue > end > -- > "A belief in a supernatural source of evil is not necessary; men alone > are quite capable of every wickedness." > > ~~ Joseph Conrad (1857-1924), novelist- Hide quoted text - > > - Show quoted text - That looks that your Compiler doesn't support the dim sign. Try "real :: a(50)",please.
|
Next
|
Last
Pages: 1 2 3 4 5 6 Prev: Call Array valued Fortran function from C Next: Advice on how to keep track of allocated memory |