Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
BIL106 Introduction to Scientific and Engineering Computing Sample Problems 2 Problem 1: Write a module containing a recursive function that calculates the sum of the first n natural numbers, i.e. (S = 1 + 2 + 3 + …..+ n). Also write a main program to test the subroutine. Solution 1: module mproblem1 public :: total contains recursive function total(n) result(k) integer, intent(in):: n integer :: k select case (n) case (1) k=1 case (2:) k = n + total(n-1) end select end function total end module mproblem1 ! main program program problem1 use mproblem1 integer :: i print *, "Enter an integer number:" read *, i print *, "The sum of the first n natural numbers is:" print *, total(i) end program problem1 Problem 2: Store 10 four digits numbers in an array. Purely by changing the output format, print the numbers as (a) a single column of numbers; (b) three rows of four numbers; (c) a single line of numbers; (d) a single column of numbers in reverse order (e) a single line of numbers in reverse order Your anwers should be a single print statement. The numbers should neither overlap nor be adjacent. Solution 2: program problem2 integer, dimension(12) :: arr integer :: i arr = (/(12345+500*i, i =1, 12)/) print "(i5)", arr print "(4(i5, tr1))", arr print "(12(i5,tr1))", arr print "(i5)", arr(12:1:-1) print "(12(i5,tr1))", arr(12:1:-1) end program problem2 Problem 3: Personal data of the employees of company X are stored in a file named “phonenum.txt” as is given below: Name ------John Ashley Mike ………… ………… ………… Surname -----------Doe Long Young Phone number ----------------2343434 9871233 3178256 The file contains the data of 100 employees. Write a program that reads the data from this file then puts them into alphabetical order with respect to the surnames and then stores the personal data in a new file called “ordered.txt.” Solution 3: program problem3 character(len = 11), dimension(100) :: name, sname, tnum character(len = 11) :: temp integer:: i, j, n open (unit = 10, file = "phonenum.txt", action = "readwrite", status = "old", position="rewind") n = 55 !dosyada sadece 55 veri bulunduğu için burada n = 55 alınmış. do i = 1, n read (unit = 10, fmt="(a10,a11,a7)") name(i), sname(i), tnum(i) end do do i = 1, n-1 do j = i+1, n if (sname(i)>sname(j)) then temp = sname(i) sname(i) = sname(j) sname(j) = temp temp = name(i) name(i) = name(j) name(j) = temp temp = tnum(i) tnum(i) = tnum(j) tnum(j) = temp endif end do end do open (unit = 11, file = "ordered.txt", action = "readwrite", status = "replace", position="rewind") do i = 1, n write(unit = 11, fmt="(3a11)") name(i), sname(i), tnum(i) end do end program files Problem 4: Write a subroutine that multiplies two n-by-n matrices A and B and assign the result to an output array C. Also write a main program to test the subroutine. Solution 4: module mproblem4 public :: multpl contains subroutine multpl(a, b, c) real, dimension(:, :), intent(in) :: a, b real, dimension(:,:), intent(out) :: c integer :: i, j, k, n real :: summ n = size(a,1) do i = 1, n do j = 1, n summ = 0 do k = 1, n summ = summ + a(i, k) * b(k, j) end do c(i, j) = summ end do end do end subroutine multpl end module mproblem4 !main program program problem4 use mproblem4 real, dimension(3, 3) :: f, g, h integer :: i, j do i = 1, 3 do j = 1, 3 f(i,j) = (i - 1.0)* 3 + j * 1.0 g(i,j) = 9.0 + (i - 1.0) * 3.0 + j * 1.0 end do end do call multpl(f, g, h) print *, "matrix F" print *, "--------" print "(3f5.1)", f print *, " " print *, "matrix G" print *, "--------" print "(3f5.1)", g print *, " " print *, "Multiplication of matrix F and G = matrix H" print *, "-------------------------------------------" print "(3f6.1)", h end program problem4 Problem 5: In mathematics, a perfect number is a positive integer that is the sum of its proper positive divisors, that is, the sum of the positive divisors excluding the number itself. Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself). The first perfect number is 6, because 1, 2, and 3 are its proper positive divisors, and 1 + 2 + 3 = 6. Equivalently, the number 6 is equal to half the sum of all its positive divisors: (1 + 2 + 3 + 6) / 2 = 6. (a) Write a function that determines whether an integer number is a perfect number and returns the logical value .true. if it is. (b) Write a main program that finds the perfect numbers lying in the interval [1, 10000]. Solution 5: module mproblem6 public :: perfect contains function perfect(n) result(bool) integer, intent(in) :: n logical :: bool integer :: summ, i summ = 0 do i = 1, n/2 if (modulo(n,i)==0) then summ = summ + i end if end do program problem6 use mproblem6 integer :: i do i = 1, 10000 if (perfect(i)) then print *, i end if end do end program problem6 if (summ ==n) then bool = .true. else bool = .false. end if end function perfect end module mproblem6