Beruflich Dokumente
Kultur Dokumente
Fall 2012
Homework #9
Chapter 3: Multiple Choice, T/F, Debugging Exercises, Programming Exercises 1, 4 & 7
Multiple Choice
1.
a.
b.
c.
d.
2.
A group of statements that exist within a program for the purpose of performing a specific task
is a(n) _____________.
block
parameter
module
expression
A benefit of using modules that helps to reduce the duplication of code within a program is
______________.
a.
b.
c.
d.
code reuse
divide and conquer
debugging
facilitation of teamwork
3.
a.
b.
c.
d.
body
introduction
initialization
header
4.
a.
b.
c.
d.
define
call
import
export
5.
a.
A _____________ point is the memory address of the location in the program that the
computer will return to when a module ends.
termination
b.
c.
d.
6.
module definition
return
reference
design technique that programmers use to break down an algorithm into modules is known as
______________.
a.
b.
c.
d.
7.
top-down design
code simplification
code refactoring
hierarchical subtasking
A _____________ is a diagram that gives a visual representation of the relationships between
modules in a program.
a.
b.
c.
d.
flowchart
module relationship chart
symbol chart
hierarchy chart
8.
a.
b.
c.
d.
global variable
local variable
hidden variable
none of the above; you cannot declare a variable inside a module
9.
a.
b.
c.
d.
declaration space
area of visibility
scope
mode
10.
a.
b.
c.
d.
11.
A(n) is a special variable that receives a piece of data when a module is called.
a.
b.
c.
d.
argument
Parameter or Formal Parameter
header
packet
12.
When _____________ , only a copy of the arguments value is passed into the parameter
variable.
a.
b.
c.
d.
13.
When _____________, the module can modify the argument in the calling part of the program.
a.
b.
c.
d.
14.
a.
b.
c.
d.
local variable
universal variable
program-wide variable
global variable
15.
a.
b.
c.
d.
local
global
reference
parameter
True or False
1.
The phrase divide and conquer means that all of the programmers on a team should be
divided and work in isolation.
FALSE
2.
TRUE
3.
FALSE
4.
FALSE
5.
FALSE
6.
A hierarchy chart does not show the steps that are taken inside a module.
TRUE
7.
FALSE
8.
In most programming languages, you cannot have two variables with the same name in the
same scope.
TRUE
9.
Programming languages typically require that arguments be of the same data type as the
parameters that they are passed to.
TRUE
10.
Most languages do not allow you to write modules that accept multiple arguments.
FALSE
11.
When an argument is passed by reference, the module can modify the argument in the calling
part of the program.
TRUE
12.
FALSE
Short Answer
1.
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 its needed by calling it and passing the
appropriate argument(s) for that instance.
2.
Name and describe the two parts that a module definition has in most languages.
Another example of a module definition would be the function below (a function being a module that
returns a value--never going to forget that now!) which has a slightly different format and is called in a
different manner:
Function Integer addNums(Integer num1, Integer num2, Integer num3)
Declare Integer total = 0
total = num1 + num2 + num3
return total
End Function
As shown in the example above the function definition begins with Function (rather than Module).
Because a function returns a value it specifies the Data Type of the value it will return (in this case
Integer) before the name of the function--addNums, and then in parentheses any parameter variables.
As with non-function modules, if it doesnt take any arguments there will simply be an empty set of
parentheses following the name of the function.
The body is the same as that of the Module except that the body of a function must have a return
statement. (The value returned must be of the data type specified for the function in the header.)
As with the module definition, the function header must be paired with an ending End Function.
While the Module would be called with the pseudocode:
Call Module addNums(num1, num2, num3)
Visual Basic example:
addNums(num1, num2, num3)
the function would be called with the pseudocode:
// Calls function addNums and assigns the value returned to the
// variable totalSum
Set totalSum = addNums(num1, num2, num3)
Visual Basic example:
totalSum = addNums(num1, num2, num3
3.
When a module is executing, what happens when the end of the module is reached?
When the end of the module is reached the program returns control to the return point (the computer
saves the memory address of the location it should return to), usually the statement immediately after
the module call.
4.
What is a local variable? What statements are able to access a local variable?
A local variable is a variable declared within a module. It can be accessed by any statement within the
module in which it was declared that comes after the variable is declared. Statements within the same
module that come before the variable is declared cannot access the variable (since it doesnt exist
yet!)
5.
In most languages, where does a local variables scope begin and end?
In most languages a local variables scope begins at the variables declaration and ends at the end of
the module in which it was declared.
6.
What is the difference between passing an argument by value and passing it by reference?
Passing an argument by value passes a copy of the value to the module it is passed to. Any
manipulations or changes made to the value within the called module only affect that copy of the
value within the module and do not change the value in the calling module. Upon returning to the
calling module the value of the argument passed by value is unchanged.
Passing an argument by reference passes a reference to the location of the actual value (like a
shortcut to a program on your computer desktop). Any manipulations or changes made to the value
within the module are performed on the actual variable in the calling module. Upon returning to the
calling module the value of an argument passed by reference reflects any changes made by the
called module.
7.
Since any statement in the program can access and change the value of a global variable, if there is
an error in the variable value the programmer(s) have to track down every single statement in the
program that accesses the global variable to figure out where the problem is. In a large program this
is a lot of code to sift through!
Algorithm Workbench
1.
Design a module named timesTen. The module should accept an integer argument. When he
module is called, it should display the product of its argument multiplied times 10.
Algorithm:
1. Module header
Examine the following pseudocode module header, and then write a statement that calls the
module, passing 12 as an argument.
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 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
13.4
00
13.4
7.
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
13.4
00.0
00.0
Debugging Exercises
1.
Module main()
Declare Real mileage
Call getMileage()
Display Youve driven a total of , mileage, miles.
End Module
Module getMileage()
Display Enter your vehicles mileage.
Input mileage
End Module
Variable mileage was not declared in module getMileage() nor was it passed to the module. It is
not available to statements within the module.
2.
Module main()
Call getCalories()
End Module
Module getCalories()
Declare Real calories
Display How many calories are in the first food?
Input calories
Declare Real calories
Display How many calories are in the second food?
Input calories
End Module
The variable calories is declared twice in the module getCalories(). You cannot have two
variables with the same name in the same scope. It will cause an error.
3.
Module main()
Call squareNumber(5)
End Module
Module squareNumber (Integer Ref number)
Set number = number ^ 2
Display number
End Module
Why does this say potential error? You cant normally pass a non-variable argument into a reference
variable. It causes an error. So thats the potential about. Wont the module cause an error as soon
as it tries to change the value of number?
4.
Module main()
Call raiseToPower(2, 1.5)
End Module
Module raiseToPower(Real value, Integer power)
Declare Real result
Set result = value ^ power
Display result
End Module
There is a data type mismatch between the 2nd argument being passed to the module
raiseToPower() which is of the type Real and the data type of the parameter variable which is
Integer. Arguments and parameter variables must be of the same data type. (Although some
languages will allow you to pass an argument into a parameter variable of a different type, this would
not apply here since even in such languages it is only allowed in instances where no data will be lost.
In the example above the fractional portion of the value would be lost.)
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 = 0
Declare Real miles = 0
Module main()
Declare Real expense = 0
Declare Real monthlyTotal = 0
Declare Real yearlyTotal = 0
Call Module getCosts(expense, monthlyTotal)
Call Module calcYearly(monthlyTotal, yearlyTotal)
Call Module showResults(monthlyTotal, yearlyTotal)
End Module
Module getCosts(Real Ref addCost, Real Ref monthly)
Display "Enter the monthly expenses for each category."
Display "Loan payment: $"
Input addCost
Call Module calcMonthly(addCost, monthly)
Display Insurance: $
Input addCost
Call Module calcMonthly(addCost, monthly)
Display "Gasoline: $"
Input addCost
Call module calcMonthly(addCost, monthly)
fatGrams = 0
carbGrams = 0
fatCals = 0
carbCals = 0
Call Module
Call Module
Call Module
Call Module
End Module
getGrams(fatGrams, carbGrams)
calcFatCals(fatGrams, fatCals)
calcCarbCals(carbGrams, carbCals)
showResults(fatGrams,fatCals, carbGrams, carbCals)