Fortran 千本ノック
1本目 Int の配列をつくって1,2,3を入れる!
integer,dimension(3)::arr = (/1,2,3/)
2本目 Int の10個の配列をつくって2から5まで1あとは0に!
integer,dimension(10)::arr arr = 0 arr(2:5) = 1
3本目 Int の3x3の配列をつくって真ん中は1あとは0に!
integer,dimension(3,3)::arr arr = 0 arr(2,2) = 1 ! 0 0 0 ! 0 1 0 ! 0 0 0
4本目 Int の1x4の配列をつくって2x2の配列に!
integer,dimension(4)::arr integer,dimension(2,2)::arr2 arr = (/1,2,3,4/) arr2 = reshape(arr,(/2,2/)) ! 1 2 ! 3 4
5本目 文字を連結!
character(5) a character(5) b a = "hello" b = "world" print *,a//b ! helloworld
6本目 最初の2文字を交換!
character(5) a character(5) b character(5) tmp a = "hello" b = "world" tmp(1:2) = a(1:2) a(1:2)=b(1:2) b(1:2)=tmp(1:2) print *,a//b ! wolloherld
7本目 文字の配列!
character(5),dimension(2)::ch_arr ch_arr(1) = 'abc' ch_arr(2) = 'def'
8本目 文字を検索!
a = "hello" print *, scan(a,'l') ! <--- 3
9本目 namelistを活用!
data
&data a=1 b=2 /
data.f90
integer::a integer::b integer,parameter::f=10 namelist/data/a,b open(unit=f, file='data') read(unit=f, nml=data) print *, a, b ! <-- 1, 2 close(unit=f)
10本目 subroutineを使う!
subroutine my_sub(b) integer b b = b + 1 end subroutine program main implicit none integer::a a = 1 call my_sub(a) print *, a end program