Introduction to Programming with Fortran ---------------------------------------- Practical Exercise 10 --------------------- Note that you need all of the information in the lecture to answer all of the questions; even the earlier ones need features introduced towards the end. Question 1 ---------- 1.1 Write and test the following program: PROGRAM Keys PRINT *, 'Abc', 1.23e-6, -456, 'xyz' END PROGRAM Keys 1.2 Change the `PRINT *,' to the following (in turn), try to predict what will happen, and test the program to check: a) WRITE (*, *) b) WRITE (UNIT=*, FMT=*) c) WRITE (UNIT=*, *) d) WRITE (*, FMT=*) e) WRITE (FMT=*, UNIT=*) f) WRITE (*, UNIT=*) g) WRITE (FMT=*, *) Question 2 ---------- 2.1 Using list-directed I/O, write a program that reads in two filenames from the terminal, opens the first one for input and the second for output, reads a CHARACTER(LEN=8) variable, a REAL(KIND(0.0D0), DIMENSION(5) array and an INTEGER variable from the first, and writes them to the second. Check the program works by providing the filenames "Programs/piffle.data" and "piffle.copy", and checking that the latter is being created correctly, with the same values (but not formatting). 2.2 Copy the program, change it to use unformatted I/O (except for reading the filenames, of course), and check it using the files "Programs/huffle.data" and "huffle.copy". Run the command cmp huffle.data huffle.copy to check that the files are identical. [ Note that a very few systems put timestamps etc. in Fortran unformatted files, so this will not always work; however, it usually will, and it will on the system you are using to run this example. ] Question 3 ---------- 3.1 Write a program that opens the file "/dev/tty" (i.e. the screen) for output, and prints the character string 'Kilroy was here' in the form "Kilroy was here" (i.e. immediately surrounded by quotation marks) using list-directed output. Use the DELIM specifer to do this. 3.2 Repeat 3.1, but not using the DELIM specifer (or using DELIM='none', as you prefer). 3.3 Extend the program in 3.2 to read a CHARACTER variable of length 20 using list-directed input, and print it as above. Input 'Kilroy was here', without the apostrophes, surrounded by apostrophes and surrounded by quotation marks. Note the effects. 3.4 Using the same program, input a string so that the result displayed is "/*',",'*/" (11 characters). Question 4 ---------- 4.1 Using the following array declaration: INTEGER :: i INTEGER, PARAMETER, DIMENSION(20) :: ints = (/ (i, i = 1, 20) /) Write a program that prints the odd elements only. Don't worry about the formatting, including whether numbers are on one line or separate lines. Do this three ways: a) Using a DO-loop and writing one element at a time. b) Using an implied DO-loop in the I/O statement. c) Using array sectioning. 4.2 Repeat question 4.1, printing the even elements in reverse order. Question 5 ---------- 5.1 Using the following array declaration and initialisation: INTEGER :: i, j INTEGER, DIMENSION(6, 8) :: ints DO j = 1,8 DO i = 1,6 ints(i,j) = i+10*j END DO END DO Write a program that prints only the elements with both indices even, with the first dimension varying fastest. Don't worry about the formatting, including whether numbers are on one line or separate lines. a) Do this using a DO-loop and writing one element at a time. b) Do this using an implied DO-loop in the I/O statement. c) Do this using array sectioning, and possibly intrinsics. 5.2 Repeat question 5.1, with the second dimension varying fastest. Question 6 ---------- 6.1 Using list-directed I/O, read the file in Programs/people.data, which consists of multiple lines, each of which is an integer, a name as a simple character string of at most 10 characters, and 6 real numbers. Do not count the lines, but read until end of file, and print out each line as you read it in. 6.2 Change the program to print out each line in the form of a table with 4 spaces allowed for the index, and the real numbers printed with 3 digits after the decimal point: ' 987: Dickon 1.234 2.345 3.456 4.567 5.678 6.789' Note the colon. Use a repeat count to do this, and put the format in a CHARACTER constant. 6.3 Change the program to read all of the data into three arrays, an INTEGER one of dimension 20, a CHARACTER*10 one of dimension 20 and a a REAL one of dimension (6,20). Then print the data out as a table in a single WRITE statement. 6.4 Change the format to add a header line "Personnel data"; you still need only a single WRITE statement. 6.5 Change the program in 6.3 to to take the exponent (the EXP function) of the second, fourth and sixth numbers for each entry, and change the format to use ES format for those.