Assignment 1: Fortran

Background

This assignment uses Fortran restricted to the constructs in Fortran-II. Fortran does not allow recursion. However, the g77 compiler allows indirect recursion, which you may use if you must (you get more points if you can avoid recursion). You may use logical IF, but you must not use any form of IF that includes THEN. You may use GOTO, of course.

It isn't very pleasant to represent linked structures in Fortran. You can use an array, where the index of the array is used instead of a pointer to refer to an element. You can represent a set of instances of a C struct or Pascal record in Fortran by building a separate, parallel array for every field; all arrays have the same set of indices.

Requirements

Write a program in Fortran that (1) reads an integer, which we call nodeCount, then (2) reads nodeCount integers (one per line), inserting them one by one into an initially empty sorted binary tree, resolving ties by moving the right, then (3) prints the tree in preorder, one value per line, then (4) reads an integer, which we call probeCount, then (5) reads probeCount integers, and for each one, prints the integer and either "yes" or "no", depending on whether the integer is present in the tree. You may assume that the input data are well formed (there are enough integers, for example) and that nodeCount is not higher than 1000.

Test your program both on your own data and on the data in http://www.cs.uky.edu/~raphael/courses/CS450/asg.fortran.data.

Logistics

The Fortran you will use is called g77. It is available both in the Multilab and in the CSLab. It is a traditional compiler, which means you should use a text editor (like vi) to create your program in a file, whose name must end in .f. Then you invoke the compiler as follows:

g77 -g -Wall filename.f -o filename
The -g flag causes the compiler to place extra information in the output file that assists you in debugging later. The -Wall flag tells the compiler to warn you about any apparent programming errors, even those that don't prevent correct compilation. You then invoke the program itself as
filename
You may use the gdb debugger if you need:
gdb filename
You can give the debugger the help command to see what it can do for you.

Extra credit

  1. Output the tree in breadth-first order and postorder. Again, try to avoid using recursion.
  2. Insert the integers into a heap in addition to the tree and print the integers in order by repeatedly deleting the smallest element of the heap.

Due date

This assignment is due at the start of class time on the day indicated in the syllabus. See the syllabus for the late policy. Submit the assignment by email to raphael @cs.uky.edu.