100 likes | 193 Views
AND , OR. A AND B A.and.B EX: N is greater than 1 and less than 3 (N.gt.1).and.(N.lt.3) Q: Leap year ( 閏年 )
E N D
AND , OR • A AND B A.and.B • EX: N is greater than 1 and less than 3 (N.gt.1).and.(N.lt.3) • Q: Leap year (閏年) • “year that could be divided by four, except for the year that could be divided by 100 but could not be divided by 400”(Hint 1: 1900 and 2100 is not a leap year)(Hint 2: a mod b mod(a,b))Ans:(mod(N,400).eq.0).or.((mod(N,4).eq.0).and.(mod(N,100).gt.0))
Introduction to “Array(陣列)” • Calculate standard deviation PROGRAM WITHOUTARRAY C LEARN ARRAY IN FORTRAN (1) C CALCULATE MEAN VALUE C 2005/05/18 Y. FUKUSHIMA implicit none real*8 S1,S2,S3,S4,S5 real*8 mean, std write (*,*) '#1 >>' ; read (*,*) S1 write (*,*) '#2 >>' ; read (*,*) S2 write (*,*) '#3 >>' ; read (*,*) S3 write (*,*) '#4 >>' ; read (*,*) S4 write (*,*) '#5 >>' ; read (*,*) S5 C Calculate mean mean=(S1+S2+S3+S4+S5)/5 C Calculate STD std = ( (S1-mean)**2 + (S2-mean)**2 1 + (S3-mean)**2 + (S4-mean)**2 1 + (S5-mean)**2 )**0.5d0 write (*,*) 'mean: ', mean write (*,*) 'standard deviation: ', std stop end We cannot use loops … Inconvenient !!
Introduction to “Array(陣列)” • “Array” can save you !! mean=sum/5.0d0 C Calculate variation and std var=0 do i=1,5 var=var+(S(i)-mean)**2 end do std=var**0.5 write (*,*) 'mean: ', mean write (*,*) 'standard deviation: ', std stop end PROGRAM WITHARRAY C LEARN ARRAY IN FORTRAN (2) C Calculate Statistics C 2005/05/18 Y. FUKUSHIMA implicit none integer i real*8 S(5) real*8 mean, std, sum, var do i=1,5 write (*,*) '#',i,'>>' ; read (*,*) S(i) end do C Calculate sum and mean sum=0 do i=1,5 sum=sum+S(i) end do real*8 s1,s2,s3,s4,s5 real*8 s(5)
Indentation • In order to express structures, please use appropriate“indentation” C Calculate variation and std var=0 do i=1,5 var=var+(S(i)-mean)**2 end do std=var**0.5 if n.gt.0 then sum = sum + n else if n.eq.999 then goto 10 else write (*,*) ‘invalid number!’ end if
Standard input / output • Unix (including Cygwin) system has “standard input” and “standard output” • Standard input : keyboard • Standard output: console display • Ex) ls command shows output to standard output 福島康裕@mango ~ $ ls -l total 330 -rwxr-xr-x 1 福島康裕 None 70125 May 16 23:24 a.exe -rwxr-x--- 1 福島康裕 None 2692 Oct 11 2004 all.dem -rwxrwxrwx 1 福島康裕 None 613 May 16 22:28 approx.f -rwxr-xr-x 1 福島康裕 None 71658 May 18 20:11 array1.exe -rwxrwxrwx 1 福島康裕 None 696 May 18 21:28 array1.f -rwxr-xr-x 1 福島康裕 None 70854 May 18 21:38 array2.exe -rwxrwxrwx 1 福島康裕 None 604 May 18 21:38 array2.f
Redirection of standard output • We can change the standard input/output by using “redirection” • Ex) Change standard output for ls command to a file 福島康裕@mango ~ $ ls –l > list.txt 福島康裕@mango ~ $ cat list.txt total 332 -rwxr-xr-x 1 福島康裕 None 70125 May 16 23:24 a.exe -rwxr-x--- 1 福島康裕 None 2692 Oct 11 2004 all.dem -rwxrwxrwx 1 福島康裕 None 613 May 16 22:28 approx.f -rwxr-xr-x 1 福島康裕 None 71658 May 18 20:11 array1.exe -rwxrwxrwx 1 福島康裕 None 696 May 18 21:28 array1.f -rwxr-xr-x 1 福島康裕 None 70854 May 18 21:38 array2.exe redirection of stdout : > stdin : <
Redirection of standard output • Ex) Copy a file using cat and redirection 福島康裕@mango ~ $ cat list.txt > list2.txt 福島康裕@mango ~ $ cat list2.txt total 332 -rwxr-xr-x 1 福島康裕 None 70125 May 16 23:24 a.exe -rwxr-x--- 1 福島康裕 None 2692 Oct 11 2004 all.dem -rwxrwxrwx 1 福島康裕 None 613 May 16 22:28 approx.f -rwxr-xr-x 1 福島康裕 None 71658 May 18 20:11 array1.exe -rwxrwxrwx 1 福島康裕 None 696 May 18 21:28 array1.f -rwxr-xr-x 1 福島康裕 None 70854 May 18 21:38 array2.exe redirection of stdout : > stdin : <
Redirection of standard input • Running program witharray • by inputting from keyboard (=standard input) • by inputting from a file (redirected standard input) 福島康裕@mango ~ $ ./array2.exe # 1>> 80 # 2>> 90 # 3>> 67 # 4>> 50 # 5>> 70 mean: 71.4 standard deviation: 29.9866637 福島康裕@mango ~ $ 福島康裕@mango ~ $ ./array2.exe < in.dat # 1>> # 2>> # 3>> # 4>> # 5>> mean: 71.4 standard deviation: 29.9866637 福島康裕@mango ~ $ 80 90 67 50 70 in.dat
Pipe • We can connect two commands with a pipe command | command 福島康裕@mango ~ $ ls –al | less total 332 -rwxr-xr-x 1 福島康裕 None 70125 May 16 23:24 a.exe -rwxr-x--- 1 福島康裕 None 2692 Oct 11 2004 all.dem -rwxrwxrwx 1 福島康裕 None 613 May 16 22:28 approx.f -rwxr-xr-x 1 福島康裕 None 71658 May 18 20:11 array1.exe -rwxrwxrwx 1 福島康裕 None 696 May 18 21:28 array1.f -rwxr-xr-x 1 福島康裕 None 70854 May 18 21:38 array2.exe Result was put into less command and now we can go up and down !
Summary • AND and OR • Array in FORTRAN • Indentation: A format for letting the source code look more structured. • Unix (cygwin): Standard input and output • Unix (cygwin): Redirection • Unix (cygwin): Pipe