Introduction to Programming with Fortran ---------------------------------------- Practical Exercise 6 -------------------- Question 1: Simple Example of Subroutine ---------------------------------------- 1.1 Write a main program and internal subroutine that takes three REAL arguments and returns, as its first argument, the third argument subtracted from the second. Keep the code as simple as possible. Test this by including calls to subtract the following pairs of numbers: 1.23 4.56 -7.89 3.42 4.29 -0.98 1.2 Add INTENT to the above code and check that it works. 1.3 Change the call to using keywords and change the order of the arguments; check that this does not change the results. Save this code in a file. Note: To avoid getting confused with keywords, you are strongly advised to use different names for the variables in the main program and internal subroutine. Question 2: Simple example of a Function ---------------------------------------- 2.1 Write a main program and an internal function that takes two REAL arguments and returns the second argument subtracted from the first. Keep the code as simple as possible. Test this by including calls to add the following pairs of numbers: 1.23 4.56 -7.89 3.42 4.29 -0.98 2.2 Add INTENT and ELEMENTAL to the above code and check that it works. 2.3 Change the call to using keywords and reverse the order of the arguments; check that this does not change the results. Question 3: Random Number Generation ------------------------------------ 3.1 Write a function which simulates a throw of two dice and returns the total score. Include it as an internal procedure in a main program, which prints out the result of the throw of two dice 10 times. Compile and run it. Do not expect the output to have the same values as in the specimen answers, as Fortran random numbers do not work like that. The following code fragment will set Z to a pseudo-random number uniformly distributed between 0 and 1: REAL :: Z CALL RANDOM_NUMBER(Z) Keep a copy of this program. Hiatus ------ The above questions cover all of the basic facilities covered before the hiatus. You should leave the subsequent questions until after the rest of the lecture. Question 4: Arrays ------------------ 4.1 Write a program with an internal subroutine that takes two assumed shape 2-dimensional REAL array arguments, with INTENT(OUT) and INTENT(IN), checks that their shapes are the same, and allocates two 1-dimensional REAL arrays the same size as the first and second dimensions. 4.2 Add the code to set the two automatic arrays to the row and column averages, and shift the rows and columns so that all row and column totals are zero. To do this, set the first argument to the value of the second with the relevant row and column averages subtracted from it and the overall average added to it. Test this by providing it with the matrix: 2.35 2.82 4.55 7.83 3.97 6.75 7.62 8.36 8.97 0.74 2.70 2.49 Question 5: CHARACTER Arguments ------------------------------- 5.1 Write a program with an internal subroutine that takes one INTEGER and three assumed size CHARACTER arguments, prints out each of the CHARACTER ones on a separate line, and sets the integer argument to the sum of their lengths. Then print that length. Use the following data to test it: 'Kilroy' 'was' 'here' Question 6: CHARACTER Functions ------------------------------- 6.1 Write a program with an internal function that takes one assumed size CHARACTER argument, and returns the value argument with leading and trailing spaces removed and padded to the same length as its argument with '.'s. Test this with the following strings: 'How, now, brown cow?' ' Spaced out ' [ 3 leading and 5 trailing spaces ] Question 7: Save Attribute -------------------------- 7.1 Write a subroutine with one INTEGER argument that returns 7 the first time it is called, 8 the next time, 9 the next time, and so on. Declare the argument INTENT(OUT). Test it however you prefer. Question 8: Newton-Raphson -------------------------- 8.1 Write a program containing an internal subroutine and two functions. This question is not hard, but needs more programming and numerical skills than the others. Some people may want to leave it for later. The subroutine should accept a value of X*LOG(X) greater than 1.0 and print the corresponding value of X, by calling the functions and using Newton-Raphson iteration. The first function should return the value of X*LOG(X), where x is its argument. The second function should return the value of LOG(X)+1.0, where x is its argument [LOG(X)+1.0 is the first derivative of X*LOG(X)]. The program should call that subroutine with the following values: 1.23 456.7 8.9e5 [ Hint 1: Newton-Raphson solution of F(X) = A takes an approximate solution X0 and improves it by calculating X1 = X0 - (F(X0)-A)/F'(X0) ] [ Hint 2: use the function value as the initial guess at X ] [ Hint 3: stop when the solution is the same as either its predecessor or the one before that ]