Problem set for Fortran 90 Version 1.0 11/21/96 RTADS Introduction to Fortran 90 Homework Problems and Lecture Topics 1 Make change using "if" statements also use non advancing write also use input from disk file Cover compilation structures 2 Bank account with full data error checking Recursive N factorial function Cover executable statements 3 Bank account with sort in a module and using a derived type Cover declarations 4 Printer plot of shapes using a module Using library modules such as LUP with matrix operations Cover key words 5 Concentration game for VT100 Cover all intrinsic functions 6 Using pointers for binary trees 7 Balanced binary trees for dynamic sorting 8 Input/Output formatting and namelist Cover formatting and input/output 9 Random access disk files, direct unformatted on a derived type Timing language features 10 Default arguments, named arguments Shared data amoung processes, dining philosophers General Requirements: Use new Fortran 90 style, no "old" statement types Start each file with a set of comments: ! .f90 ! ! ! ! Use indentation in each construct Ask questions of instructor or others, do the programming yourself Ask if you can not fix a compiler error quickly Ask for help in finding bugs ( after you try to fix them ) Use the Fortran 90 Handbook to look up statements Use Compact Summary of Fortran 90 to look up key words Use Fortran Top 90 to look up statements and attributes OpenVMS commands CREATE/DIR [.PROBLEMS] will make a subdirectory SD .PROBLEMS to move to subdirectory F90 MAKE_CHANGE.F90 will compile, you may get errors F90 MAKE_CHANGE same as above F90/CHECK MAKE_CHANGE catches errors at execution time F90/DEBUG/NOOP MAKE_CHANGE with LINK/DEBUG puts you in debugger F90/LIST MAKE_CHANGE writes errors to file MAKE_CHANGE.LIS LINK MAKE_CHANGE builds an executable MAKE_CHANGE.EXE RUN MAKE_CHANGE runs your program, output to screen RUN/OUT=MAKE_CHANGE.OUT MAKE_CHANGE now output goes to a file RUN/INPUT=BANK.DAT/OUT=BANK.OUT BANK input from file LINK MAIN,MODULE1,MODULE2 to link program main that uses module1 and module2 in debugger SET MODULE/ALL SET MODE SCREEN GO GO or STEP or STEP INTO or EX variable program will stop on execution fault, QUIT Kill a running program with CTRL-C or CTRL-Y copy a file with COPY/LOG [SQUIRE]MAKE_CHANGE.F90 [] Create a command file, BANK.COM to run finished homework: $ SET VERIFY $ SET NOON $ F90 BANK.F90 $ LINK BANK $ ASSIGN/USER BANK.DAT SYS$INPUT $ ASSIGN/USER BANK.OUT SYS$OUTPUT $ RUN BANK $ TYPE BANK.F90 $ TYPE BANK.OUT $ SET NOVERIFY Run this command file with @BANK/OUT=BANK.PRT Then $ PRINT/QUE=LP BANK.PRT Note: print *, does NOT work with write(6,*) when getting output on a file. OK on terminal. Unix (DEC Unix, Linux, etc.) commands f90 -o make_change make_change.f90 to compile and link make_change to run make_change > make_change.out output to a file bank < bank.dat > bank.out input from file, output to f90 -c module1.f90 f90 -c module2.f90 f90 -o main main.f90 module1.o module2.o write a floppy disk with tar -cvf /dev/fd0a * read a floppy disk with tar -xvf /dev/fd0a file transfer with ftp caxp10y get file.ext or put file.ext mget *.* or mput *.* ... quit print a file lpr bank.f90 lpr bank.out Problem 1 Make change using "if" statements Write a Fortran 90 program that makes change. Input a number from 1 to 99 that is the number of cents US. Output the most of the biggest coins to equal the input number. 1 Dollar = 100 cents 1 Quarter = 25 cents 1 Dime = 10 cents 1 Nickel = 5 cents 1 Penny = 1 cent Read from keyboard, write to screen Do not print out coin if none (zero) are needed Starter code can be copied from [SQUIRE]MAKE_CHANGE.F90 Test on 99, 90, 76, 75, 74, 55, 41, 30, 25, 10, 5, 1 Then test on bad data -99, -1, 0, 100, 1000 abc 7.7 The output should look like this for input 41 1 Quarter 1 Dime 1 Nickel 1 Penny The output should look like this for input 76 3 Quarter 1 Penny Problem 1a Make change using non advancing write Modify Problem 1 to use non advancing write Put all the results on one line, again do not print zero values Same test data The output should look like this for input 41 1 Quarter 1 Dime 1 Nickel 1 Penny The output should look like this for input 76 3 Quarter 1 Penny Problem 1b Make change using input from a disk file Replace the read statement with a modified read statement that reads from a disk file. Create a file MAKE_CHANGE.DAT Put the test data from Problem 1 into this file, one item per line, the good data followed by the bad data, put 99 at the end to be sure your program read all the data Problem 2 Bank account This will use more input/output, more arrays, internal subprograms, character handling and more. Write a complete Fortran 90 program that is a single file with a program and two contained (internal) procedures that compiles and executes with no errors or warnings. Write a Fortran 90 program to simulate a simple bank. You may use the skeleton program that is attached or do your own from scratch. The skeleton program may be copied from [SQUIRE]BANK.F90, data from [SQUIRE]BANK.DAT. Each input line contains a simple bank activity: JOHN DOE OPEN 10.00 opens an account JIM JONES OPEN 37.50 opens an account JOHN DOE DEPOSIT 5.00 deposits $5 JIM JONES WITHDRAW 7.50 withdraws $7.50 JOHN DOE CLOSE 0.00 closes his account MARY SMITH OPEN 20.00 open another account At the end of the run, print out the status of the open accounts. For the data above, print something like: JIM JONES $ 30.00 MARY SMITH $ 20.00 BE REASONABLE Give error message: if opening an open account if closing a non open account if overdrawing if depositing a negative amount, etc. Physically delete closed accounts to be ready for the next problem. Problem 2a Bank account with full error checking Improve the BANK.F90 program in Problem 5 to take the full data set. Add some more test cases to BANK.DAT with both good and bad data. Test the program. Problem 2b Write an external function "factorial" to compute integer N factorial use the recursive function factorial(n) result(fact) construct 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 etc. Write a main program to test your factorial function. This must be in another file and must have an interface construct Test by using a do loop from 0 to 20 Print out N and N! in the do loop Notice when it starts giving wrong answers! It may even hang in an infinite loop if you did not protect yourself! do not use print *, n, factorial(n) ! it dies in DEC F90 use write(6,fmt="(2i30)")n, factorial(n) ! or some variation Problem 3 Bank account with sort Create a module and use the module in BANK.F90 Take the file HEAPSORT.F90 and run it to be sure you think it works. This is a good quality n log n sort, better than quicksort on some data. This version sorts a single array of integers. Now, create a module with subroutine HeapSort in it. Modify HeapSort to simultaneously sort two arrays, an array of character strings and an array of real numbers. Modify the bank account program from Problem 6 to USE your module to sort the account name / balances data. At the end of the run, print out the status of the open accounts. First unsorted, then sorted by calling the HeapSort subroutine in the module. Add or rearrange data so that unsorted data BANK.DAT is not in sorted order by account name. Remember to compile both BANK.F90 and HEAPSORT.F90 and link both LINK BANK,HEAPSORT Save the results of the final run on a file. RUN/OUT=BANK.OUT/INPUT=BANK.DAT BANK Problem 3a Bank account with derived type Now change your latest version of BANK.F90 to use a single array of fifty (50) elements, where the element is a derived type having an account name and balance. See the example PROG1.F90 and MODULE1.F90 for help with the derived type and module setup. The HeapSort needs to be changed again. Remember to only sort on the account name. When interchanging, only one element is used, closer to the original version of HeapSort. You can start from the original version if you wish. The same data can be used. Problem 4 Add line drawing subroutine to the printer plot module. Be sure to test it on horizontal, vertical, and sloped lines. Problem 4a Add another test case with your data to the LUP decomposition program. Add a print routine that makes all the output matrices look like normal matrices. This can use a named format character string where you change the format based on the size of the matrix. Remember, you can use read(,fmt=) to convert from integer to character string. Problem 5 Concentration game for VT100 Write a Fortran 90 program that simulates the game of "Concentration". The screen initially looks like this: A B C D E Turns ddd 1 X X X X X Found dd 2 X X X X X Won 0 3 X X X X X 4 X X X X X Hidden behind each X is a digit 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9 in some random position. There are exactly two of each digit. The game is played by choosing an X to show the digit behind. The player would type in, for example, 2D, then the program would expose the digit, replace the X by the digit in the second row and fourth column. Suppose the digit was a 7. The player must now guess the row and column of the other 7. If the player guesses correctly, both 7's remain exposed. If the player guesses wrong, the wrong guess is exposed for 3 seconds, then both the 7 and the wrong guess are covered up with an X. After each pair of selections, the number of Turns is incremented by one. After a pair is matched, the Found count is incremented. When all pairs are found, Turns and Found are set to zero and the number of games Won is incremented. The program deals another random set of pairs and the player can start the next game. CTRL-Z, an end of file, ends the game. You are not allowed to write down where the numbers are located. You have to "Concentrate" and remember which numbers are in what locations. Good luck! Sample data for Problems 2,3 BANK.DAT (add more tests as desired) JOHN DOE OPEN 10.00 opens an account JIM JONES OPEN 37.50 opens an account JOHN DOE DEPOSIT 5.00 deposits $5 JIM JONES WITHDRAW 7.50 withdraws $7.50 JOHN DOE CLOSE 0.00 closes his account MARY SMITH OPEN 20.00 open another account AARON ADD OPEN 99.00 should sort first ZEB ZZEPT OPEN 99.00 should sort last BAD TRANS FOUL 1.00 bad transaction NOT OPEN CLOSE 1.00 bad, not open MARY SMITH OPEN 15.00 no Mary, its open MARY SMITH DEPOSIT -5.00 no Mary, try again MARY SMITH WITHDRAW -5.00 no Mary, positive ! JOHN DOE DEPOSIT 5.00 not open JIM JONES WITHDRAW 99.99 too much, reject JOHN DOE OPEN 99.00 back again, OK VERY BAD OPEN -9.99 no Very, positive Others ...