[Santa Clara University]
Department of Mathematics
and Computer Science

Machine Problem 4

Math 60 -- Winter, 2006
D. C. Smolarski, S.J.

              
35 points
DUE: NOON: Friday, February 17, 2006

I assume you will create a new subdirectory for this program, as I suggested with the first program, and also create a new makefile.

In file mp4in.inp in my Math 60 directory on math, there is a list of the "inmates" of 5th floor Dunne for 1986-87 (including yours truly). You can copy this file to your desired directory by typing the following command (in bold):

     math 34: cp /home/dsmolars/ma60/mp4in.inp  .
Note: Don't omit the final period after a space! (The file is also available on the web by clicking on the link above.)

As given in this file, this list is not in any particular order. Each name is followed by a room number. All names are given last name first, and the names are at most 30 characters long. The very first item in the file is an integer giving the total number of individuals in the list. There are no more than 100 name-room combinations in the file.

Ideally, you would experiment with allocatable arrays and allocate an array of the proper dimension after you read the very first item in the file. If this seems too difficult, however, you can declare an array of large enough size and only use part of it in the sorting routines.

In this programming assignment you are to sort the list twice, first by room number and a second time alphabetically.

You must remember to keep the right room number associated with the right name throughout the two sorting processes. To do this, you must use Fortran-90/95 derived types. You should put the definition of the derived type in a module and make use of this module in all subprograms that need it.

Your program should print out two different listings: (1) one by room number (in order), with the room number first, and (2) the other alphabetical with name first on each line. Each list should include both name and room number (but in different orders), as well as an initial sequential number (starting at 1) before the other information on each line (for reference purposes).

Turn in the program and output, but not the input file.

SUGGESTIONS AND COMMENTS:

  1. Include two sorting routines, one for character strings, one for integers. You must use derived types (cf. comments 4 and 5).
  2. Fortran provides functions for comparison of character strings. One string is "lexically greater than" another if the first string would follow the second string in the dictionary. There are four functions: LGE (lexically greater than or equal to), LGT (lexically greater than), LLE and LLT, each of which take two parameters, the two strings being compared. See Chapter 13, section 7 of Essentials of Fortran 90/95/2003 for further information.

  3. I suggest you make use of a simple sorting routine, such as bubblesort or selectionsort. You may need to "translate" code from C++ or Pascal into Fortran-90/95/2003. See my Math 10 Notes N12 for code for both sorting routines.

    In your first sorting routine (e.g., sortint), if you use bubblesort your comparison test should look like:

            if(student[index+1]%room < student[index]%room) then
    		Call Switch(student[index],student[index+1])
    
    Thus, in your second sorting routine (e.g., sortchar), if you use bubblesort your comparison test should look like:
    	if(llt(student[index+1]%name,student[index]%name)) then
    		Call Switch(student[index],student[index+1])
    
    (These comparison tests presuppose bubblesort rather than selection sort -- if you use selectionsort, you must make the appropriate adaptations.)
  4. By using derived types, when you switch elements, both the name and the room will automatically stay together. You still need two sorting routines, however.
  5. By using derived types, you need only one Switch routine in which temp is of the type of the derived type that you define.
  6. I do not intend that you separate the last name from the first! I assume that you will use formatted input and read the entire width of the input line reserved for the name (i.e., 30 characters) into a single character variable.
  7. You can either rename the input file to something like ftn10 and use 10 as the "device number" inside your program in READ statements, or you can use the default input device number of 5 and "redirect" input at runtime by using the < sign followed by the input file name. For example, if mp4 is your executable file, then you could type
             mp4 < mp4in.inp > mp4ou.out
    
    to have the program read from input file mp4in.inp and write to mp4ou.out.
  8. Finally, make sure your program looks nice with indentations and comments (and includes functions)! Make sure the output is appropriately labeled, with headers describing the two different listings! Remember also that your name should be included in the output!

Turn in the source file (i.e., the program code file) and the output file. This can be done by using the Unix command

     enscript mp4.f90 mp4ou.out
(assuming these are your actual filenames). You can also use the procedure as in MP 1 to create PostScript (PS) files first and then use the lp command to print out the PS files.

As with the previous programs, your sheets may be turned in to my office, O'Connor 32, or to my mailbox in O'Connor 31. Please staple the pages yourself and do not turn in disconnected pages!


This page is maintained by Dennis C. Smolarski, S.J. dsmolarski@math.scu.edu
Last changed: 16 January 2006.