290 likes | 481 Views
Examples. Introduction to Scientific & Engineering Computing. Program simpmath Real::x,y Print*,»Please enter x and y values» Read*, x, y Print*,x,» in ve », y,» un toplamı = «, x+y Print*, x, « un ve « , y , « un farkı =«, x-y Print*,x,» in ve «,y,» in carpımı = «, x*y
E N D
Examples Introduction to Scientific & Engineering Computing
Program simpmath Real::x,y Print*,»Please enter x and y values» Read*, x, y Print*,x,» in ve », y,» un toplamı = «, x+y Print*, x, « un ve « , y , « un farkı =«, x-y Print*,x,» in ve «,y,» in carpımı = «, x*y Print*,x,» in ve «,y,» in bolumu = «,x/y Print*,x,» in karekoku= «,sqrt(x),»y nin karekoku= «,sqrt(y),» karekoklerı toplamı ise= «,sqrt(x)+sqrt(y) End Program simpmath
Write a program to calculate cumulative sums of the integers from 1 through n (assume n has been assigned a value elsewhere) 1 2 3 … ------- =n*(n+1)/2 program toplamlar integer::i,toplam topla=0 do i=1,100 topla=topla+1 print *,"1den ",i,"ye kadar sayilar toplami=",topla enddo endprogram toplamlar
Program cumulative_suminteger ::i, n, sumdosum = 0 print*,”Please enter a number to calculate cumulative of sum, to exit (0)”read*,nif (n==0) then exit endifdo i = 1, n sum = sum + i write(*,*) “i =“, i,“sum =“, sum end do end doend Program cumulative_sum
Write a program to find the sum of the successive odd integers 1, 3, ..., 399.
Write a program for a library in order to find the location of a book arranged with respect to the first letter of its title. Do not neglect the titles beginning with a number. "A":"F“ on the shelf 10. "G":"L“ on the shelf 20. "M":"R“ on the shelf 30. "S":"Z“ on the shelf 40. Others on the shelf 50. Select the correct Fortran arithmetic expression for the given algebraic expression. 1)abx + cy + 3d a) a * b ** x + c ** y + 3 * d b) a * b * x + c * y + 3d c) ab**x + c**y + 3 * d d) abx + cy + d*3 e) None of them
program library_case character(len=1)::title !Why (len=1)? print*,“Please enter the title of the book you are searching for:" read*,title select case (title) case("A":"F") print*,"You can find it on the shelf 10." case ("G":"L") print*,"You can find it on the shelf 20." case ("M":"R") print*,"You can find it on the shelf 30." case ("S":"Z") print*,"You can find it on the shelf 40." case default print*,"You can find it on the shelf 50." end select end program library_case
Calculate the surface area of a cylinder Surface Area = (2 • π • r²) + (2 • π • r • height) Volume = π • r² • height = ¼ • π • d² • height
program cylinder ! Calculate the surface area of a cylinder. ! Declare variables and constants. ! constants=pi ! variables=radius squared and height implicit none ! Require all variables to be explicitly declared integer :: ierr character(len=1) :: yn real :: radius, height, area real, parameter :: pi = 3.141592653589793 interactive_loop: do ! Prompt the user for radius and height ! and read them. write (*,*) "Enter radius and height." read (*,*,iostat=ierr) radius,height The main reason for learning a programming language is to use the computer to solve…………………. ……… ……………………………… problems
! If radius and height could not be read from input, ! then cycle through the loop. if (ierr /= 0) then write(*,*) "Error, invalid input." cycle interactive_loop end if ! Compute area. The ** means "raise to a power." area = 2 * pi * (radius**2 + radius*height) ! Write the input variables (radius, height) ! and output (area) to the screen. write (*,"(1x,a7,f6.2,5x,a7,f6.2,5x,a5,f6.2)") + "radius=",radius,"height=",height,"area=",area yn = " " yn_loop: do write(*,*) "Perform another calculation? y[n]" read(*,"(a1)") yn if (yn=="y" .or. yn=="Y") exit yn_loop if (yn=="n" .or. yn=="N" .or. yn==" ") exit interactive_loop end do yn_loop end do interactive_loop end program cylinder
Two examination papers are written at the end of the course. The final mark is either the average of the two papers, or the average of the two papers and the class record mark (all weighted equally), whichever is the higher. The program should reads in the class record mark and the marks of the papers, computes the average, and shows PASS (>= 50%) or FAIL (< 50%).
! ------------------------------------------------------------- ! Two examination papers are written at the end of the course. ! The final mark is either the average of the two papers, or ! the average of the two papers and the class record mark (all ! weighted equally), whichever is the higher. The program ! should reads in the class record mark and the marks of the ! papers, computes the average, and shows PASS (>= 50%) or FAIL (< 50%). ! ------------------------------------------------------------- PROGRAM FinalMark IMPLICIT NONE REAL :: Mark1, Mark2 ! the marks of the papers REAL :: Final ! the final marks REAL :: ClassRecordMark ! the class record mark REAL, PARAMETER :: PassLevel = 50.0 ! the pass level PRINT*,"Please enter ClassRecordMark, Mark1, Mark2" READ(*,*) ClassRecordMark, Mark1, Mark2 Final = (Mark1 + Mark2) / 2.0 IF (Final <= ClassRecordMark) THEN Final = (Mark1 + Mark2 + ClassRecordMark) / 3.0 END IF WRITE(*,*) 'Class Record Mark : ', ClassRecordMark WRITE(*,*) 'Mark 1 : ', Mark1 WRITE(*,*) 'Mark 2 : ', Mark2 WRITE(*,*) 'Final Mark : ', Final IF (Final >= PassLevel) THEN WRITE(*,*) 'Pass Status : PASS' ELSE WRITE(*,*) 'Pass Status : FAIL' END IF END PROGRAM FinalMark
There had been given SO2 air pollution data of Izmir in “izmso2.dat” file like the following table. Read the data from file. Write a program to calculate average and sum of the SO2 for yearly and monthly. Show the result and also write it a file (“izmso2.son”).
program aveofso2 character (len=9),dimension(15)::title_col,title_row real,dimension(12,5)::so2 real,dimension(8)::ave_col real,dimension(15)::ave_row integer::sutun,satir,i,j,row_num,col_num,ba row_num=12 col_num=5 !the file description and reading the first row (titles) open(unit=1,file="f:\Belgelerim\dersnotlari\fortran\so2.txt",status="old",action="read") read(unit=1,fmt=*)title_col(1:col_num+3) !reading data from file Names used to identify programs, constants, variables, etc. Identifiers must begin with a letter. This can be followed by up to ….. …. letters, digits, underscores
do i=1,row_num read(unit=1,fmt=*)title_row(i),so2(i,1:col_num) end do read(unit=1,fmt=*)title_row(row_num+1:row_num+2) !calculating the sum do i=1,row_num ave_col(i)=sum(so2(i,1:col_num)) end do !calculating the averages do i=1,row_num ave_row(i)=sum(so2(i,1:col_num)) enddo !printig on screen print"(a10,3x,8a8))",title_col(1:col_num+3) do i=1,row_num print"(a,8f8.2)",title_row(i),so2(i,1:col_num),ave_row(i)/col_num,ave_row(i) enddo print"(a,8f8.2))",title_row(row_num),ave_row(1:col_num+2)
open(unit=2,file="f:\Belgelerim\dersnotlari\fortran\so2.ort",status="unknown", action="write") !writing the file write(unit=2,fmt="(a10,8a8)")title_col(1:col_num+3) do i=1,row_num write(unit=2,fmt="(a,8f8.2)")title_row(i),so2(i,1:col_num),ave_row(i)/col_num,ave_row(i) enddo write(unit=2,fmt="(a,8f8.2)")title_row(row_num),ave_row(1:col_num+2) end program aveofso2
1.Write a program which will read up to 20 integer numbers and print them in the reverse order to that in which they were typed. program invers_order integer::i integer, dimension(20)::j do i=1,20 read*,j(i) end do do i=20,1,-1 print*,j(i) enddo end program invers_order
1.Write a program which produces a table of sin x and cos x for angles x from 0° to 360° in steps of 5°. program tableofsincos integer::i do i=0,360,5 print*,"For degrees ",i," sin =",sin(i*1.0)," ","cos =",cos(i*1.0) enddo end program tableofsincos
1.A chemist makes fifty measurements of the rates of three different reactions like below. Write a program that calculates the average of each reaction itself and all of them using an array. Reac.1 Reac.2 Reac.3 221.3 316.2 164.1 233.2 120.6 106.3 ……. …… …….
Write a program, which asks the user student number and print the faculty name of the student attending using first two digits of the number as follows; (01) Civil Eng. (02) Architecture, (03) Mechanical, (04) Electrical and Electronic, (05) Mines, (06) Chemical and Metallurgical, (07) Naval Arch. (08) Science and Letters, (09) Management (10) Conservatory, (11) Aeronautics and Astronautics, (12) Maritime Faculty (13) Textile Technologies and Design,
Write a program to calculate energy of an object. Main program will read the following data. g=9.81 h=0.23 m=1.1 v=5 And will print input values, kinetic and potential energy, A function subprogram calculate only potential energy A subroutine subprogram calculate only kinetic energy
program ex_1 real, parameter :: pi = 3.141693 ! Exponential form of real numers real, parameter :: e = 1.6e-19, eps = 8.86e-12 integer :: z real :: r, phi !read distance from particle print*, "Enter distance from particle =" read*, r !read charge of particle print*, "Enter charge of particle =" read*, z !calculate Coulomb potential phi = z * e / (4.0 * pi * eps * r) !print results with print print*, "Coulomb Potential at a distance",r, "from a particle with a charge of",& z, "= " , phi end program ex_1
program notlar integer :: not,i character(len=1)::harf print *, "sinif mevcudunu giriniz" read *, i do j=1,i do print *, j,". ogrencinin notunu giriniz" read *, not if (not>=0.and.not<=100) then exit else print *, "uygun aralikta not girin 0-100" cycle end if end do select case (not) case(85:100) !print *, "A" harf="A" case(70:84) harf="B" case(60:69) harf="C" case(45:59) harf="D" case(0:44) harf="F" case default print *, "uygun aralikta sayi girmediniz" end select print *, "girilen notun", not, " harf karsiligi=", harf end do end program notlar
(Use starting values n = 4.14159265 Є = 0.00001 a = 2.5 )
module motion public :: harmonic real,public :: n, eps, a contains subroutine harmonic(t,pos,vel,acc) real,intent(in)::t real,intent(out)::pos,vel,acc pos=a*sin(n*t+eps) vel=n*a*cos(n*t+eps) acc=-a*n**2*sin(n*t+eps) end subroutine harmonic end module motion program eqn use motion real :: t n=4.14159265 eps=0.01 a=2.5 print *, "please give the value of t :" read *,t call harmonic(t,pos,vel,acc) print *, "position is", pos print *, "velocity is", vel print *, "acceleration is", acc end program eqn
1. You are given the following data file named “input.dat”: 0.024543693 0.030679616 0.036815539 0.042951463 0.049087386 0.055223309 0.061359232 0.067495156 0.073631079 0.079767002 0.085902925 0.092038849 0.098174772 Write a FORTRAN program that will read this file treating each line as a separate data point, and then evaluate the function f x : x ex sin x for every point. The results should be written to the screen in two columns: one with x and the other one with f x . Also calculate and print out the average value obtained from all function evaluations. The function f x should be implemented as a FUNCTION.