Vous êtes sur la page 1sur 4

Chris trimble

Unit 4 Assignment
1. How do modules help you to reuse code in a program?
If a specific operation is performed in several places in a program, a module can
be written once to perform that operation, and then be executed any time it is
needed.
2. Name and describe the two parts that a module definition has in most languages.
A header and a body; header indicates starting point, and 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 main program where it originated
from.
4. What is a local variable? What statement are able to access a local variable?
A variable declared inside a local module, only statement within a module
5. In most languages, where does a local variables scope begin and end?
Begins at the variables declaration within a module and 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 value only a copy of the arguments value is passed. By reference its passed
into a special modification parameter.
7. Why do global variables make a program difficult to debug?
Any statement in a program can change the value of a global variable. If you find
that the wrong value is being stored in a global variable, you have to track down
every statement that accesses it to determine where the bad value is coming from.
In a program with thousands of lines of code, this can be difficult.
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 timesTen( Integer value)
Declare Integer result
Set result = value * 10
Display result
End Module

Chris trimble

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( Real Ref number)
Display "Enter a number between 1-100."
Input number
End Module

6. 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 a, Real b)
Set a = 0
Set b = 0
Display a, " ", b
End Module
Display:
1 3.4
00
1 3.4
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
Display
1 3.4

Chris trimble
0 0.0
1 3.4
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 convert()
Set miles = 0
Set km = 0
Display Enter a speed in Kilometers:
Input km
Set mph = kph * 0.6214
Display You entered , km , km. That is equal to , miles ,
mph
End Module
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 sale()
Display What is the amount of your purchase?
Input purchase
Call stateTax()
Display Today, your purchase was purchase
Display The state sales tax is stateTax
Display The county sales tax is countyTax
Display Your total sales tax is totalSalesTax
Call total()
Display Your total today is totalOfSale
End Module
Module stateTax()
Set stateTax = 0.04
Set countyTax = 0.02
Set Integer totalSalesTax = stateTax * countyTax
End Module
Module total()
Set Integer totalOfSale = (purchase * totalSalesTax) + purchase
End Module

Chris trimble

Vous aimerez peut-être aussi