Programmng n Fortran 90 : 2017/2018 Programmng n Fortran 90 : 2017/2018 Exercse 1 : Evaluaton of functon dependng on nput Wrte a program who evaluate the functon f (x,y) for any two user specfed values x and y, where the functon f (x,y) s defned as follows : x + y f x 0 and y 0 x + y f (x,y) = 2 f x 0 and y < 0 x 2 + y f x < 0 and y 0 x 2 + y 2 f x < 0 and y < 0 Exercse 2 : Crcle and Sphere Wrte a program that read the radus of a crcle at the prompt et gve the area and volume of the correspondng crcle and sphere (πr 2 and 4/3πr 3 ). Exercse 3 : System soluton Wrte a program whch resolve the folowng system of 2 equatons wth 2 unknows x and y : a 1 x + b 1 y = c 1 a 2 x + b 2 y = c 2 Exercse 4 : Do loops What are the teraton counts of the followng DO loops, the values of the loop varable nsde the loop, and the value of the loop varable after the DO construct? do = 1, 5 do = 5, 0, -1 do = 10, 1, -2 do = 0, 30, 7 do = 3, 2, 1 Wrte a program prntng nsde and outsde the loop to verfy your results. Exercse 5 : Temperature converson Wrte a program n a do loop whch gves a Celsus (T c ) to Fahrenhet (T f ) converson table n unts steps from 50 to 50 Celsus. The converson formula s : T c = 5 9 (T f 32) 1
Programmng n Fortran 90 : 2017/2018 Exercse 6 : Seres Wrte a program whch read an nteger value n, then compute and wrte on the screen the n frst terms of the seres u n+1 = 2u n + 3, wth u 0 = 1. Same queston for the Fbonacc seres u n+1 = u n + u n 1, wth u 0 = u 1 = 1. Exercse 7 : π computaton Formula 1 : Wrte a program whch compute π wth followng formula : π/4 = arctan(1) = n= n=0 ( 1) n /(2n + 1) Fnd the number of teratons needed to obtan the followng precson : 3.1415926535897 Formula 2 : Do the same wth ths formula wth : π/4 = 4 arctan(1/5) arctan(1/239) arctan(x) = n= n=0 ( 1) n x2n+1 2n + 1 and compare the number of teratons of the two formulas for the same precson. Exercse 8 : Quadratc equaton solver Wrte a program to calculate the roots of a quadratc equatons of the form ax 2 + bx + c = 0 The algorthm : Read a, b and c. Check that a 0. If a = 0, the equaton s lnear. Compute the soluton. Prnt the soluton. 2
Programmng n Fortran 90 : 2017/2018 Test your program for each case : one root, two real roots, two complex roots. Hnt : The program has a sngle root f b 2 4ac = 0 two real roots f and magnary roots f b 2 4ac > 0 b 2 4ac < 0 Exercse 9 : Arrays Gven the followng declaratons: real(4), dmenson(1:10,1:20) :: a real(4), dmenson(10,-5:10) :: b real(4), dmenson(0:5,1:3,6:9) :: c real(4), dmenson(1:10,2:15) :: d What s the rank, sze, bounds, and extents of a,b,c and d? Gven the followng declaratons: real(4), dmenson(-1:5,3,8) :: alpha real(4), dmenson(-3:3,0:2,-7:0) :: beta Are the two arrays conformable? Gven the followng array declaraton real(4): a(0:5,3) whch of the followng references are legal? a(2,3), a(6,2), a(0,3), a(5,6), a(0,0) What s the array element order of the followng array? nteger, dmenson(-1:1,2,0:1) :: alpha The followng array declaratons are gven: real(4), dmenson(50) :: alpha real(4), dmenson(60) :: beta whch of the followng statements are vald? 3
Programmng n Fortran 90 : 2017/2018 alpha=beta alpha(3:32)=beta(1:60:2) alpha(10:50)=beta alpha(10:49)=beta(20:59) alpha=beta(10:59) alpha(1:50:2)=beta beta=alpha beta(1:50)=alpha Exercse 10 : Selecton sortng Sortng a lst s a classcal algorthm. Several methods exsts, some are more effcent than others. The algorthm works as follows: Fnd the mnmum value n the lst Swap t wth the value n the frst poston Repeat the steps above for the remander of the lst (startng at the second poston and advancng each tme) 4
Programmng n Fortran 90 : 2017/2018 mn 18 3 10 25 3 18 10 25 9 3 11 13 23 8 mn 9 3 11 13 23 8 mn 3 3 10 25 9 18 11 13 23 8 mn 3 3 8 25 9 18 11 13 23 10 mn 3 3 8 9 25 18 11 13 23 10 mn 3 3 8 9 10 18 11 13 23 25 mn 3 3 8 9 10 11 18 13 23 25 mn 3 3 8 9 10 11 13 18 23 25 mn 3 3 8 9 10 11 13 18 23 25 18 3 10 25 9 3 11 13 23 8 Wrte a program that create (wth the random_number ntrnsc subroutne) an 1D-array contanng nteger between 0 and 10, sort ths array wth the selecton sortng algorthm and wrte the sorted result on the screen. Exercse 11 : Smple example of a Subroutne Wrte a subroutne wth three parameters, two real numbers a and b as nput and a real number c as output. The subroutne should return c as the sum of a and b. Wrte the program callng the subroutne. Exercse 12 : Smple example of a Functon Wrte a functon wth two parameters, two real numbers a and b. The functon should return c as the sum of a and b. Wrte the program callng the functon. 5
Programmng n Fortran 90 : 2017/2018 Exercse 13 : Smple Statstcs Wrte a man program and ths two functons : functon 1 : returns the mean of a sequence of values (x, = 1,n) : m = 1 n n x =1 functon 2 : returns the varance of a sequence of values (x, = 1,n) usng the frst functon to compute the mean. v = 1 n n =1 (x m) 2 Use the random_number ntrnsc subroutne to create the sequence of values. Exercse 14 : Bubble sortng Bubble sort s a smple sortng algorthm that works by repeatedly steppng through the lst to be sorted, comparng each par of adjacent tems and swappng them f they are n the wrong order. The pass through the lst s repeated untl no swaps are needed, whch ndcates that the lst s sorted. The algorthm gets ts name from the way smaller elements "bubble" to the top of the lst. Because t only uses comparsons to operate on elements, t s a comparson sort. Wrte a subroutne bubble_sort(...) wth the array you want to sort and the number of elements as nput. The array should be sorted when you leave the subroutne. Wrte a man program wch create the array and fll t wth the random_number ntrnsc subroutne and sort t wth the bubble_sort(...) subroutne and wrte the sorted result n a fle. Exercse 15 : Sequental search Lnear search or sequental search s a method for fndng a partcular value n a lst, that conssts of checkng every one of ts elements, one at a tme and n sequence, untl the desred one s found. Wrte a subroutne whch mplement ths algorthm. The nputs should be the lst and t length, the outputs beng the answer (yes or no the elements s n the lst) and f the element s n the lst, the poston n the lst. If the element s present more than one tme, only the frst element wll be reported. The man program wll have to read the lst from a fle and output the results. Gve also the number of comparsons needed proportonally to the number of elements of the lst, n order to have an estmate of the effcency of the algorthm. Exercse 16 : Dchotomc search A dchotomc search s a search algorthm that operates by selectng between two dstnct alternatves (dchotomes) at each step. Here, we wll explot the fact that the lst has been already sorted (wth one of the algorthm already done). The frst step s to compare the mddle element of the lst wth the key element. If t s the same, we have found t and we can stop. If not, we repeat ths operaton on half of the lst, dependng f the key element s greater or lower than the mddle element. 6
Programmng n Fortran 90 : 2017/2018 Wrte a subroutne whch mplement the algorthm. Gve also the number of comparsons needed proportonally to the number of elements of the lst, n order to have an estmate of the effcency of the algorthm and compare t wth the sequental search. Exercse 17 : Matrx transpose A s a square matrx of real numbers of sze N N. Wrte a subroutne verb transmat whch takes as arguments N and A. The matrx A has to be transposed when gong out of the subroutne. The only array used n verb transmat should be A. Wrte a subroutne read_matrx that read a matrx from a fle and a subroutne wrte_matrx that wrte the result n another fle. Wrte the man program usng the three subroutnes. The fles should be formatted ths way : a 11 a 21 a 31... a 12 a 22 a 32......... Exercse 18 : Matrx multplcaton Wrte a subroutne perfomng the matrx multplcaton of two matrx A of sze (m,n) and B of sze (n, p). The subroutne should return the resultng matrx C of sze (m, p). Wrte a subroutne read_matrx that read a matrx from a fle and a subroutne wrte_matrx that wrte the result n another fle. Wrte the man program usng the three subroutnes. The fles should be formatted n the same format that the prevous exercse. 7