Está en la página 1de 14

CPT 168

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.

The first line of a module definition is known as the _______________.

a.
b.
c.
d.

body
introduction
initialization
header

4.

You ______________ the module to execute it.

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 ____________ is a variable that is declared inside a module.

a.
b.
c.
d.

global variable
local variable
hidden variable
none of the above; you cannot declare a variable inside a module

9.

A(n) ____________ is the part of a program in which a variable may be accessed.

a.
b.
c.
d.

declaration space
area of visibility
scope
mode

10.

A(n) _____________ is a piece of data that is sent into a module.

a.
b.
c.
d.

Argument or Actual Parameter


parameter
header
packet

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.

passing an argument by reference


passing an argument by name
passing an argument by value
passing an argument by data type

13.

When _____________, the module can modify the argument in the calling part of the program.

a.
b.
c.
d.

passing an argument by reference


passing an argument by name
passing an argument by value
passing an argument by data type

14.

A variable that is visible to every module in the program is a ____________.

a.
b.
c.
d.

local variable
universal variable
program-wide variable
global variable

15.

When possible, you should avoid using ____________ variables in a program.

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.

Modules make it easier for programmers to work in teams.

TRUE
3.

Module names should be as short as possible.

FALSE
4.

Calling a module and defining a module mean the same thing.

FALSE
5.

A flowchart shows the hierarchical relationships between modules in a program.

FALSE
6.

A hierarchy chart does not show the steps that are taken inside a module.

TRUE
7.

A statement in one module can access a local variable in another module.

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.

Passing an argument by value is a means of establishing two-way communication between


modules.

FALSE

Short Answer
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 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.

A module definition has a Header (the first line) formatted as below:


Module moduleName(DataType varName)
Where moduleName is the name of the module. The parentheses hold the Data Type and Name of
any parameter variable(s) that will be passed to the module. A module can have one, parameter
variable, several parameter variables (separated by commas) or no parameter variables. The module
header is always paired with an ending End Module as the last line, after the body of the module.
The second part of a module definition is the body which contains the statements which the module
will execute.
Very simple example of a module definition:
Module addNums(Integer num1, Integer num2, Integer num3)
Declare Integer total = 0
total = num1 + num2 + num3
Display total
End Module

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.

Why do global variables make a program difficult to debug?

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

2. Declare variable to hold result


3. Compute the product
4. Display the result
5. End Module
Pseudocode:
Module timesTen(Integer someNumber)
Declare Integer product
product = someNumber * 10
Display The product of , someNumber, and 10 is , product, .
End Module
2.

Examine the following pseudocode module header, and then write a statement that calls the
module, passing 12 as an argument.

Module showValue(Integer quantity)


Call showValue(12)
3.

Look at the following pseudocode module header:

Module myModule(Integer a, Integer b, Integer c)


Now look at the following call to myModule:
Call myModule(3, 2, 1)
When this call executes, what value will be stored in a? What value will be stored in b? What value
will be stored in c?
The value of a will be 3.
The value of b will be 2.
The value of c will be 1.
4.

Assume that a pseudocode program contains he following module:

Module display(Integer arg1, Real arg2, String arg3)


Display Here are the values:
Display arg1, , arg2, , arg3
End Module
Assume that the same program has a main module with the following variable declarations:

Declare Integer age


Declare Real income
Declare String name
Write a statement that calls the display module and passes these variables to it.
Call display(age, income, name)
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 theNumber)


Display Enter a number:
Input theNumber
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
13.4
00
13.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
13.4
00.0
00.0
Debugging Exercises
1.

Find the error in the following pseudocode.

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.

Find the error in the following pseudocode.

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.

****Find the potential error in the following pseudocode.

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.

Find the error in the following pseudocode.

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

Call Module getKm(kilometers)


Call Module kmToMiles(kilometers, miles)
Call Module showResult(kilometers, miles)
End Module
Module getKm(Real Ref km)
Display Enter the distance in kilometers:
Input km
End Module
Module kmToMiles(Real km, Real Ref mi)
Set mi = km * 0.6214
End Module
Module showResult(Real km, Real mi)
Display km, kilometers is equal to , mi, .
End Module
4. Automobile Costs
Design a modular program that asks the user to enter the monthly costs for the following expenses
incurred from operating his or her automobile: loan payment, insurance, gas, oil, tires, and
maintenance. The program should then display the total monthly cost of these expenses, and the total
annual cost of these expenses.

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)

Display "Oil: $"


Input addCost
Call Module calcMonthly(addCost, monthly)
Display "Tires: $"
Input addCost
Call Module calcMonthly(addCost, monthly)
Display "Maintenance: $"
Input addCost
Call Module calcMonthly(addCost, monthly)
End Module
Module calcMonthly(Real addCost, Real Ref monthly)
Set monthly = monthly + addCost
End Module
Module calcYearly(Real monthly, Real Ref yearly)
Set yearly = monthly * 12
End Module
Module showResults(Real monthly, Real yearly)
Display "Total monthly automobile expense is $", monthly
Display "Total yearly automobile expense is $", yearly
End Module
7. Calories from Fat and Carbohydrates
A nutritionist who works for a fitness club helps members by evaluating their diets. As part of her
evaluation, she asks members for the number of fat grams and carbohydrate grams that they
consumed in a day. Then she calculates the number of calories that result from the fat, using the
following formula:
Calories from Fat = Fat Grams * 9
Next she calculates the number of calories that result from the carbohydrates, using the following
formula:
Calories from Carbs = Carb Grams * 4
The nutritionist asks you to design a modular program that will make these calculations.
Module Main()
Declare Real
Declare Real
Declare Real
Declare Real

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)

Module getGrams(Real Ref fatGrams, Real Ref carbGrams)


Display Enter the number of fat grams consumed:
Input fatGrams
Display Enter the number of carbohydrate grams consumed:
Input carbGrams
End Module
Module calcFatCals(Real fGrams, Real Ref fCals)
fCals = fGrams * 9
End Module
Module calcCarbGrams(Real cGrams, Real Ref cCals)
cCals = cGrams * 4
End Module
Module showResults(Real fGrams, Real fCals, Real cGrams, Real cCals)
Display fCals, were consumed from , fGrams, of Fat.
Display cCals, were consumed from , cGrams, of Carbohydrates.
End Module

También podría gustarte