PROGRAM ASSIGNMENT #5

DUE: 1:00 pm, Monday, April 2, 2001
VALUE: 50 pts.

For Program #5, rewrite Program #4 so that it performs essentially the same tasks as before, but employs user-defined procedures and/or functions to modularize the program. You will probably wish to use the Turbo Editor to "cut and paste" existing code in Program #4 to help construct Program #5. Given below is an example of one of the procedure definitions which will print the banner at the beginning the report for each test value read from the input file; also given is an example of what Program #5's main execution module should look like. Your program should emulate the procedure calls as closely as possible to those shown in the example. Note that once an integer is read from the input file, all processing for that integer is accomplished via procedure calls -- none are to be performed in the main program itself.

Your program will declare global constants shown below which will establish limits for the Fibonacci, factorial and multiples routines; for example:

Const
  Max_for_Factorial = 23;
  Max_for_Fibonacci = 46;
  Max_for_Multiples = 500;
  Number_of_multiples_per_line = 15;
{ etc. }
Your program will declare only the following variables at the global level: a variable Test of type INTEGER to hold the integer read from the input file for testing, and the usual TEXT variables for processing input and output text files. All other variables are to be declared local to the procedures in which they are used. No procedure should define parameters spelled the same way as those used in global storage (however, you may use the same name more than once among several procedures, if necessary).

Your program should use the outline supplied here. You will need to design a procedure definition for each one called in the main line code. The procedure Write_nth_Fibonacci is to contain a local, nested procedure which will find and print the correct ordinal ending associated with the test value. For example, if Test = 33, this local procedure will print the "rd" used in the report: "The 33rd Fibonacci number ... ". Each procedure takes as input the value of the integer read from the input file (via the identifier Test) along with the identifier for the output file and, when necessary, identifiers to global constants which contain important fixed parameters for the procedures (for example,

Write_multiples(Test, 
                Outfile, 
                Max_for_multiples, 
                Number_of_multiples_per_line);
is a procedure call which obviously finds and prints the multiples of the integer Test to the output file Outfile; the procedure is also passed the constants Max_for_multiples which defines the upper limit of the multiples of Test to be printed and Number_of_multiples_per_line which defines how many multiples are to be printed per line). Remember the technical point that all Text file parameters must be declared as Variable parameters (if you forget, you will be prompted by the compiler!). In this program, usage inside a procedure of storage global to itself (a "global reference") will be grounds for a redo!

Remember, the goal in Program 5 is to streamline the main program as much as possible; all processing other than opening and closing text files and printing informational messages on the user screen is to be performed via procedure calls! In addition to these procedures, you may wish to create user-defined functions to be used by the procedures in performing their work. For example, the Write_nth_Fibonacci procedure might contain a local function called Fibonacci which returns the nth Fibonacci number when passed n as a parameter. Just be sure to follow the same rules regarding local/global identifers for any such functions you create as for the procedures I require.

TURN IN: a listing of your Program 5 code, a printout of your output file when run against the input file NUMS_PG5.DAT (which will be available sometime on or before 3/23/2001) and a diskette containing the file NUMS_PG5.DAT, your program file named PRG5_S01.PAS and the output file named PROG005.OUT.

SAMPLE CODE:

{ Procedure #2:  Print Test value and banner in output file }

Procedure Write_test_number(Test_number: Integer;
                            Var Outputfile: Text);


{ This procedure prints the value passed in Test_number at the
  beginning of the report in the file Outputfile }

Const
  Banner = '===========================';

Begin
  Writeln(Outputfile);
  Writeln(Outputfile, Banner);
  Writeln(Outputfile, 'Test value is: ', Test_number);
  Writeln(Outputfile, Banner);
  Writeln(Outputfile)
End; { Write_test_number }


Begin { Main }

  < open input and output files here >

  Write_Headings(Outfile);

  While NOT EOF(Infile) Do
    Begin
      Readln(Infile, Test);
      Write_test_number(Test, Outfile);
      If ..... < filter out illegal values here >
      Then
        ....
      Else
        Begin
          Write_nth_Fibonacci(Test, Outfile, Max_for_Fibonacci);
          Write_factorizations(Test, Outfile);
          Write_n_factorial(Test, Outfile, Max_for_factorial);
          Write_multiples(Test, Outfile, Max_for_multiples, Multiples_per_line);
          Write_perfect_squares(Test, Outfile, Squares_per_line)
        End
    End; { while not eof }

  { close all files and issue appropriate screen message. }

End.
REMEMBER: the goal is to have a streamlined, clean main execution loop like the one shown above.
Return to
CS-171
Home Page
    Return to
UW-W
Home Page
   
This page last updated
3/14/2001