Use new Fortran 95 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 95 Handbook to look up statements Use Compact Summary of Fortran 95 to look up key words
Unix (IRIX, AIX, HPUX, Linux, etc.) commands mkdir homework # create a subdirectory named homework cd homework # change directory to homework rm -f junk.txt # delete file junk.txt mv abc.txt pqr.f90 # rename a file cp abc.txt pqr.f90 # copy a file emacs make_change.f90 # pick an editor and learn it xemacs make_change.f90 vi make_change.f90 vim make_change.f90 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/fd0 * read a floppy disk with tar -xvf /dev/fd0 file transfer with ftp a.b.c.dget 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 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 bank.f90, data from 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. 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 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 test_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 f90 -o bank.f90 heapsort.f90 Save the results of the final run on a file. bank < bank.dat > bank.out 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!
Quiz covers: HW1 through HW4 Reading assignments Lectures 1 trhough 5
The quiz covers: HW1 through HW9 Reading assignments Lecture material
Last updated 4/14/06