Next: Congregates
Up: Local Variables
Previous: Local Variables and Parameters
Contents
Homework assignment, 100 points (due in one week)
The program at http://www.drtak.org/teaches/ARC/cisp365/samples/factor.pas
works, but it is inefficient. To be more specific, the subroutine
find_factor_f_in_n searches for a prime factor from 2 every time.
Start with downloading the program from the link listed above. Similar
to the previous homework assignment, you are to add parameters and
turn find_factor_f_in_n into a function, however, there is a
twist in this homework assignment.
Make the program more efficient by letting find_factor_f_in_n
modify a variable, storing the prime factor, that belongs to the main
program. To do this, you'll need to perform the following changes to
the program:
- turn
find_factor_f_in_n into a function
- specify the return value of the function
- add any necessary parameters to the function
- add any necessary local variables to the function
- change the main program so it calls the function (instead
of a procedure)
- change the main program to pass the appropriate parameters
- remove one of the initializations from
find_factor_f_in_n
- add the initialization to the main program
These are the absolute requirements of the homework assignment. If your
program does not conform to all of these requirements, your
assignment will receive 0 point.
- no global variable can be accessed from
find_factor_f_of_n
find_factor_f_of_n must be a function, not a procedure
find_factor_f_of_n cannot print, it cannot contain
write or writeln
- once the main program has found a prime factor
, all
subsequent calls to find_factor_f_of_n cannot
reexamine all prime factors that are less than
.
- only pass by reference when it is necessary, otherwise, pass by
value (this means that if you pass by reference when you could
have passed by value, points will be deducted!)
- the overall behavior (as far as input and output is concerned)
must remain unchanged from the original program
Next: Congregates
Up: Local Variables
Previous: Local Variables and Parameters
Contents
Tak Auyeung
2003-12-03