Software Development Modular Design and Algorithm Analysis
Precondition and Postcondition To create a good algorithm, a programmer must be able to analyse a precondition (starting state) and a postcondition (ending state). Once a programmer gets more experienced, they can add preconditions and postconditions to their algorithms. However, doing so is not always a good practice because algorithms should only take care of one piece of functionality.
Defining a Precondition A precondition is a condition or predicate that must always be true just prior to the execution of some section of code or before an operation in a formal specification. If a precondition is violated, the effect of the section of code becomes undefined and thus may or may not carry out its intended work. Security problems can arise due to incorrect preconditions.
Often, preconditions are simply included in the documentation of the affected section of code. Preconditions are sometimes tested using assertions within the code itself, and some languages have specific syntactic constructions for doing so. For example: the factorial is only defined for integers greater than or equal to zero. So a program that calculates the factorial of an input number would have preconditions that the number be an integer and that it be greater than or equal to zero.
Preconditions and inheritance In the presence of inheritance, the routines inherited by descendant classes (subclasses) do so with their preconditions in force. This means that any implementations or redefinitions of inherited routines also have to be written to comply with their inherited contract. Preconditions can be modified in redefined routines, but they may only be weakened. That is, the redefined routine may lessen the obligation of the client, but not increase it.
Postcondition A postcondition is a condition or predicate that must always be true just after the execution of some section of code or after an operation in a formal specification. Postconditions are sometimes tested using assertions within the code itself. Often, postconditions are simply included in the documentation of the affected section of code.
For example: The result of a factorial is always an integer and greater than or equal to 1. So a program that calculates the factorial of an input number would have postconditions that the result after the calculation be an integer and that it be greater than or equal to 1. Another example: a program that calculates the square root of an input number might have the postconditions that the result be a number and that its square be equal to the input.
Postconditions and inheritance In the presence of inheritance, the routines inherited by descendant classes (subclasses) do so with their contracts, that is their preconditions and postconditions, in force. This means that any implementations or redefinitions of inherited routines also have to be written to comply with their inherited contracts. Postconditions can be modified in redefined routines, but they may only be strengthened. That is, the redefined routine may increase the benefits it provides to the client, but may not decrease those benefits.
Recursion Recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem (as opposed to iteration). The approach can be applied to many types of problems, and recursion is one of the central ideas of computer science. Recursive functions need special stop conditions. Otherwise they will infinitely continue calling themselves. With the ByRef keyword, you can provide a way to stop a recursive function.
Example The Recursive() Function receives two arguments. The first argument is a value that is doubled on each call. The second argument is received ByRef it exists in one memory place and it is incremented on every function invocation.
Output
Pitfalls in recursive functions Infinite recursion Infinite recursion is a special case of an infinite loop that is caused by recursion. Limiting Conditions You must design a recursive procedure to test for at least one condition that can terminate the recursion, and you must also handle the case where no such condition is satisfied within a reasonable number of recursive calls. Without at least one condition that can be met without fail, your procedure runs a high risk of executing in an infinite loop.
Memory Usage Your application has a limited amount of space for local variables. Each time a procedure calls itself, it uses more of that space for additional copies of its local variables. If this process continues indefinitely, it eventually causes a StackOverflowException error. Efficiency You can almost always substitute a loop for recursion. A loop does not have the overhead of passing arguments, initializing additional storage, and returning values. Your performance can be much better without recursive calls.
Mutual Recursion You might observe very poor performance, or even an infinite loop, if two procedures call each other. Such a design presents the same problems as a single recursive procedure, but can be harder to detect and debug. Calling with Parentheses When a Function procedure calls itself recursively, you must follow the procedure name with parentheses, even if there is no argument list. Otherwise, the function name is taken as representing the return value of the function.
Testing If you write a recursive procedure, you should test it very carefully to make sure it always meets some limiting condition. You should also ensure that you cannot run out of memory due to having too many recursive calls.
Fibonacci number In mathematics, the Fibonacci numbers or Fibonacci sequence are the numbers in the following integer sequence: By definition, the first two numbers in the Fibonacci sequence are 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.
Activity 3.6 Write and algorithm that calculates part of a Fibonacci series. The user must input the starting number and how many numbers they want to display. As an extra bonus, code your function in VB. Save your work as john_s_3_6_algofib under the appropriate folder for your class in the DropOff folder on the X: drive