Exercise 6.5
- The procedure shown below has poor identifiers and no comments. Rewrite the procedure correcting these defects.
procedure a (b : real, c : real, d : int)
const x := 12
const y := 2
var e : real
put "Old balance: ", b:x:y
if b < d then
put "Balance too low - no interest given"
else
e := b * c
put "Interest given: ", e:x:y
put "New balance: ", b + e:x:y
end if
end a
- The following procedure uses global variables. Replace these with local variables
and parameters.
%************************************************************
% This procedure prints the tax set at a rate given by *
% taxRate and the total price of quantity items, each *
% priced at unitCost. A flag, taxable, is used to indicate *
% whether or not the items are taxable. *
%************************************************************
procedure computeTaxAndPrice
preTaxPrice := quantity * unitCost
if taxable then
tax := preTaxPrice * taxRate
totalPrice := preTaxPrice + tax
else
tax := 0.0
totalPrice := preTaxPrice
end if
put "Tax = $", tax
put "Total Price = $", totalPrice
end computeTaxAndPrice
- Explain the difference between arguments and parameters.
- Write and test a procedure that has a single string parameter the contents of which is a single word with,
possibly, leading and/or trailing blanks. The procedure is to print only the non-blank characters in the string.
- Write a function called countZeroes which will count how many zeroes there are in an array. The procedure will have the following heading:
funtion countZeroes(a : array 1 .. * of int) : int
For example this fragment:
var nums: array 1 .. 5 of int := init(0, 1, 2, 0, 3)
var values : array 1 .. 10 of int := init(0, 1, 2, 3, 4, 0, 0, 0, 9, 10)
put "There were ", countZeroes(nums), " zeroes."
put "There were ", countZeroes(values), " zeroes."
should produce output of:
There were 2 zeroes.
There were 4 zeroes.
|
- What is the difference in form between a call to a function and
a call to a procedure?
- Subprograms are to be written for each of the following tasks.
State, with reasons, whether a function or a procedure is better suited
to each task. Do not write the subprograms.
- Examine two integer arrays and produce an array that contains
only the elements that appear in both of the other arrays.
- Replace all punctuation marks in a string by blanks.
- Find the median value in a real array.
- Count the number of occurences of the value zero in an integer array.
- Print a title page with the purpose of a program and the author's
name .
- Complete the definition of the function digit with heading
function digit(number, position : int) : int
so that the function returns the value of the digit that is position
places from the right in number. As examples:
digit(8247, 3) = 2
digit(89, 4) = 0
- Write a character-valued function convertToGrade that has an
integer-valued parameter mark. The function should return the grade that
corresponds to mark according to the following table.
Mark
--------
0 - 49
50 - 59
60 - 69
70 - 79
80 - 100
Others | Grade
-----
E
D
C
B
A
X |
- Write a boolean-valued function leapYear with a single int
parameter year. The function should return the value true if and
only if year is a leap year.
|
|