Created
May 23, 2013 10:51
-
-
Save garaemon/5635248 to your computer and use it in GitHub Desktop.
for @shibu_tan_i
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program lesson1 | |
integer i, j, k, l | |
real a, b, c | |
real mean, geo_mean | |
print *, "Please input 4 integers:" | |
read *, i, j, k, l | |
print *, "i=", i, "j=", j, "k=", k, "l=", l | |
print *, "Please input 3 real numbers:" | |
read *, a, b, c | |
print *, "a=", a, "b=", b, "c=", c | |
mean = (i + j + k + l) / 4.0 | |
geo_mean = (a * b * c) ** (1.0 / 3.0) | |
print *, "mean of i, j, k and l = ", mean | |
print *, "geometric mean of a, b and c = ", geo_mean | |
end program lesson1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program lesson2_a | |
integer M, N | |
integer tmp | |
print *, "Please input 2 integers:" | |
read *, M, N | |
if (M < N) then | |
call swap(M, N) | |
end if | |
! ensure M >= N | |
do while (N /= 0) | |
tmp = N | |
N = mod(M, N) | |
M = tmp | |
end do | |
print *, M | |
end program lesson2_a | |
subroutine swap(x, y) | |
integer z, x, y | |
z = x | |
x = y | |
y = z | |
end subroutine swap |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program lesson2_b | |
integer A, B, C, D | |
integer AB, CD, ABCD | |
print *, "Please input 4 integers 100~500:" | |
read *, A, B, C, D | |
call GCD(A, B, AB) | |
call GCD(C, D, CD) | |
call GCD(AB, CD, ABCD) | |
print *, ABCD | |
end program lesson2_b | |
subroutine GCD(I, J, R) | |
integer I, J, R | |
integer M, N | |
integer tmp | |
M = I | |
N = J | |
if (M < N) then | |
call swap(M, N) | |
end if | |
! ensure M >= N | |
do while (N /= 0) | |
tmp = N | |
N = mod(M, N) | |
M = tmp | |
end do | |
R = M | |
end subroutine GCD | |
subroutine swap(x, y) | |
integer z, x, y | |
z = x | |
x = y | |
y = z | |
end subroutine swap |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program lesson3 | |
integer A, B, C, D | |
integer, dimension(4) :: array, i_array | |
i_array(1) = 1 | |
i_array(2) = 2 | |
i_array(3) = 3 | |
i_array(4) = 4 | |
print *, "Please input 4 integers" | |
read *, A, B, C, D | |
array(1) = A | |
array(2) = B | |
array(3) = C | |
array(4) = D | |
call qsort(array, i_array, 1, 4) | |
print *, i_array | |
print *, array | |
end program lesson3 | |
recursive subroutine qsort(a, b, first, last) | |
integer first, last | |
integer, dimension(4) :: a | |
integer, dimension(4) :: b | |
integer i, j | |
integer x, t | |
x = a((first + last) / 2) !choose pivot | |
i = first | |
j = last | |
do | |
do while (a(i) < x) | |
i = i + 1 | |
end do | |
do while (x < a(j)) | |
j = j - 1 | |
end do | |
if (i >= j) exit | |
call swap(a(i), a(j)) | |
call swap(b(i), b(j)) | |
i=i+1 | |
j=j-1 | |
end do | |
if (first < i - 1) then | |
call qsort(a, b, first, i - 1) | |
end if | |
if (j + 1 < last) then | |
call qsort(a, b, j + 1, last) | |
endif | |
end subroutine qsort | |
subroutine swap(x, y) | |
integer z, x, y | |
z = x | |
x = y | |
y = z | |
end subroutine swap |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: hello lesson1 lesson2_a lesson2_b lesson3 | |
hello: hello.f90 | |
gfortran $< -o $@ | |
lesson1: lesson1.f90 | |
gfortran $< -o $@ | |
lesson2_a: lesson2_a.f90 | |
gfortran $< -o $@ | |
lesson2_b: lesson2_b.f90 | |
gfortran $< -o $@ | |
lesson3: lesson3.f90 | |
gfortran $< -o $@ | |
clean: | |
rm -f hello lesson1 lesson2_a lesson2_b lesson3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment