1. How do modules help you to reuse code in a program?
It reduces the duplication of a code within a program by reusing the module that was written once.
2. Name and describe the two parts that a module definition has in most languages. The Header and a Body
First the Header indicates a starting point
Second the Body is a list of statements
3. When a module is executing, what happens when the end of the module is reached? Its executed and returned back to the point in the main program where it was sidetracked from
4. What is a local variable? What statements are able to access a local variable? A variable is declared inside a local module in which it is the only statement within a module
5. In most languages, where does a local variable’s scope begin and end? It begins at the variables declaration within a module and it ends at the end of the module in which the variable is declared.
6. What is the difference between passing an argument by value and passing it by reference? By the value only a copy of the arguments value is passed and by reference it is passed into a special modifications parameter.
7. Why do global variables make a program difficult to debug? It is because the global variables is used throughout all modules and plus they are hard to track.
Algorithm Workbench
1. Design a module named timesTen. The module should accept an Integer argument. When the module is called, it should display the product of its argument multiplied times 10.
Module Main ()
Call timesTen
Module timesTen (Integer Value)
Declare integerValue
Set result = value*10
Display result
End Module
5. Design a module named getNumber, which uses a reference parameter variable to accept an Integer argument. The module should prompt the user to enter a number and then store the input in the reference parameter variable.
Module getNumber (Integer Ref value)
Display “Display a number”
Input number
End Module
Module main ()
Declare Integer number x = 1
Declare Real number y = 3.4
Display (x, “ ” ,y)
Call changeUS (x, y)
Display (x, “ ” ,y)
End module
6. What will the following pseudocode program display?
Declare Integer x = 1
Declare Real y = 3.4
Display x, ” “, y
Call changeUs( x, y)
Display x, ” “, y
End Module
Module changeUs( Integer a, Real b)
Set a = 0
Set b = 0
Display a, ” “, b
End Module
It will not display anything since there is nothing within the quotation marks
7. What will the following pseudocode program display?
Module main()
Declare Integer x = 1
Declare Real y = 3.4
Display x, ” “, y
Call changeUs( x, y)
Display x, ” “, y
End Module
Module changeUs( Integer Ref a, Real Ref b)
Set a = 0
Set b = 0.0
Display a, ” “, b
End Module
As far as the module you would think that the displays would show something. But in both strings within the quotations marks both are blank to display.
Programming Exercises
1. Kilometer Converter Design a modular program that asks the user to enter a distance in kilometers, and then converts that distance to miles. The conversion formula is as follows: Miles = Kilometers × 0.6214 Module main ()
Declare Real Kilometers
Display “Enter a distance in kilometers”
Input kilometers
Call conversion (kilometers)
End Module
Module conversion (Realvalue)
Declare Realmiles
Set miles = value*0.6214
Display miles
2. Sales Tax Program Refactoring Programming Exercise 6 in Chapter 2 was the Sales Tax program. For that exercise you were asked to design a program that calculates and displays the county and state sales tax on a purchase. If you have already designed that program, refactor it so the subtasks are in modules. If you have not already designed that program, create a modular design for it. Module main ()
Declare Realpurchase
Display “Enter the amount of purchase”
Input purchase
Call Module totalState (purchase)
Call Module totalCounty(purchase)
Declare Real totalTax
Declare Real totalSale
Set totalTax = totalState + totalCounty
Set totalSale = purchase + totalTax
Display “Your total state tax is”, totalState
Display “Your total county tax is”, totalCounty
Display “Your total tax is”, totalTax
Display “Your total of your sale is”, totalSale
End Module
Module totalState (real Ref purchase)
Set totalState sales tax = purchase*0.04
End Module
Module totalCounty (real Ref purchase)
Set totalCounty sales tax = purchase*0.02
End Module