Exercise 3.5
- For what value(s) of x will each expression have the value true?
- (x not= 2) or (x not= 3)
- (x = 2) and (x = 3)
- State which of the following comparisons are true.
- "James" <= "Jim"
- "Tom" > "tomorrow"
- "025" >= "25"
|
- "=" <= "=="
- "!*&^~" < "$@?!%"
- "this" < "this "
|
- Assume that you have a program that has the following declarations:
var age : int := 16
var height : int := 175
var weight : int := 70
var sex : char := 'M'
var healthy : boolean := true
Using these values, evaluate each expression.
- (age >= 16) and healthy
- not (weight <= 75 and height >= 180)
- age > 10 or sex = 'F' and height < 170
- not height < 160 and weight < 60
- For each legal expression, state its value. For each illegal expression, state the reason that it is illegal.
- not(3 < 4.5)
- 'r' < 'q'
- '6' -'2' = '4'
|
- not 17 mod 5 = 1
- 'G' = 'g' or 1 + 2 = 3
- 5 < 2 * 5 - 3 < 10
|
- Rewrite the following fragment in as concise a form as possible. Assume that flag is boolean and
x is int.
if true then
if not flag then
x := 1
else
x := 0
end if
end if
- Study the following program and then answer the questions that follow it.
var flavour, style : char
put "Enter flavour and style"
get flavour, style
put flavour, style ..
put " The client preferred " ..
if flavour = 'B' then
put "bar-b-que" ..
elsif flavour = 'V' then
put "vinegar" ..
else
put "other" ..
end if
if style = 'C' then
put ", crinkled" ..
elsif style = 'R' then
put ", regular" ..
end if
put " chips"
What would the program print given each set of data as input?
- Rewrite using nesting.
if p and q then
put "Both true"
end if
if p and not q then
put "Only first true"
end if
if not p and q then
put "Only second true"
end if
if not p and not q then
put "Neither true"
end if
- Consider the following statement.
if (a <= b and b <= c) or (a >= b and b>= c) then
put "Values are in order"
else
put "Values are out of order"
end if
- Describe in a few words the effect of the statement.
- Are the brackets necessary? Justify your answer.
- Write a statement that has the same effect but uses neither and nor or.
- Which statement is more efficient? Justify your answer.
- As part of a program to analyze text, a fragment is required to record the type of character that is now in the variable currChar.
- Write a fragment that sets the char variable charType to the appropriate value given by the following table.
currChar | charType |
vowel | 'V' |
consonant | 'C' |
digit | 'D' |
punctuation mark .,:;?!- | 'P' |
special character ()'" | 'S' |
other character | 'O' |
- Assuming that the instructions of part a) have been executed, write a case statement that will print a message indicating the category into which currChar falls: vowel, consonant, digit, punctuation mark, special character, or other.
- Write a program that makes change for amounts less than one dollar. Input to the program should be a positive integer
less than 100, representing an amount of money, in cents. Output should be the original amount of money together with a set
of coins (quarters, dimes, nickels, pennies) that could make up that amount. The program should produce change containing
the minimum number of coins required for the given amount. The program should print results in a
reasonable, non-stilted way. For example, input of 58 should produce results something like:
58 cents requires 2 quarters, 1 nickel, 3 pennies.
|
rather than
58 cents requires 2 quarters, 0 dimes, 1 nickels, 3 pennies.
|
|
|