Q社のRさんは,チケット販売システムの開発で使用する関数のプログラミングとテストを実施しようとしている。この関数は,大人と子供のチケットの購入希望枚数を基に,合計金額の計算を行う。大人のチケットは3,000円,子供のチケットは1,000円である。一度に購入できる大人と子供のチケットの最高枚数は,それぞれ20枚までであり,関数はこの購入条件を満たしているかどうかをチェックする。また,合計で20枚以上購入する場合,割引処理を行う。関数の仕様と処理内容は次のとおりである。 〔関数の仕様〕 関数に渡される引数 J:大人のチケットの購入希望枚数,K:子供のチケットの購入希望枚数 (J,Kは0以上の整数) 関数の戻り値 L:合計金額 (Lは整数) 〔処理内容〕 (1) 購入希望枚数を基に,購入条件を満たしているかどうかを判定する。購入条件を満たしていない場合,合計金額に−1を代入する。 (2) 購入条件を満たしている場合,引数を基に次のとおりに合計金額の計算を行う。 合計金額 = 大人のチケットの購入希望枚数×3,000+子供のチケットの購入希望枚数×1,000 (3) チケットを合計で20枚以上購入した場合は,合計金額から1割を割り引く。 関数の処理の流れを図1に示す。図1のaに入れる適切な条件式はどれか。 図1 関数の処理の流れ図 - 開始 - 判定 [ a ] : No の場合 → L=−1 → 終了 - 判定 [ a ] : Yes の場合 ↓ - 判定 [ J+K≧20 ] : No の場合 → L=3,000×J+1,000×K → 終了 - 判定 [ J+K≧20 ] : Yes の場合 → L=0.9×(3,000×J+1,000×K) → 終了
平成23年度 特別試験 ITパスポート試験 問93
This question's explanation and your answers so far.
問 93
- ア
正解 The correct answer is ア. First, read the intent of the flow diagram as a whole. This function obtains an amount L from two values, J and K, and only when decision a is No does it assign −1 to L and end. Since −1 is an impossible value for an amount, it is an error value that tells the caller “the amount cannot be calculated”. In other words, decision a is an input check (a validity check) that examines whether the given J and K fall within the correct range for which calculation is allowed: if Yes, they are normal values and the flow proceeds to the amount calculation; if No, they are invalid values and the flow exits with an error. Because it is an input check, the flow must not proceed to the calculation unless both J and K are within range together. Choice ア, “0≦J≦20 and 0≦K≦20”, joins the condition on J (0 or more and 20 or less) and the condition on K (0 or more and 20 or less) with and (logical AND), so it is Yes only when both are satisfied at the same time; if either one is a negative number or a value greater than 20, it is No, that is, L=−1. This is the correct form for an input check. Furthermore, we can confirm that this condition does not contradict the later processing. J and K that pass ア satisfy 0≦J≦20 and 0≦K≦20, so the sum J+K can take values from 0 to 40. Therefore the next decision J+K≧20 can be either Yes or No, and both the branch that applies the discount, L=0.9×(3,000×J+1,000×K), and the branch without the discount, L=3,000×J+1,000×K, are actually executed. Every process written in the flow diagram is reachable, and out-of-range input is reliably rejected. The only choice that satisfies these two at the same time is ア, so the appropriate conditional expression to put in a is ア.
Incorrectイ
“0≦J≦20 or 0≦K≦20” uses or (logical OR), so the decision is Yes and the flow proceeds to the amount calculation whenever either J or K alone is between 0 and 20 inclusive. For example, when J=5 and K=100, the condition on J alone makes it Yes, and the amount is calculated using the out-of-range K=100. The same passing occurs when one value is negative, as with J=−30 and K=10. This fails to fulfil the purpose of an input check, which is to reject out-of-range values, and the branch that returns the error value L=−1 hardly functions at all. It is a typical mistake of confusing and with or.
Incorrectウ
“0≦J+K≦20” is a conditional expression that constrains only the sum, which does not match the purchase condition of at most 20 adult tickets and at most 20 child tickets. Because J and K are integers of 0 or more, a sum of 20 or less also keeps each value at 20 or less, but conversely the condition rejects valid orders that meet the purchase condition. For example, J=15 and K=10 are each within 20 tickets and could be bought, but their sum is 25, so the decision is No and L=−1 is returned. In addition, consistency with the later decision breaks down. When the flow proceeds after satisfying this condition, J+K≦20 is guaranteed, so the next decision J+K≧20 is Yes only at the single point J+K=20, and the branch that applies the discount, L=0.9×(3,000×J+1,000×K), almost loses its meaning. It is insufficient as an input check, and it is also unnatural as branch design for the flow diagram.
Incorrectエ
“J>20 and K>20” mistakes the range that should be regarded as normal; the direction of the condition is reversed. With this expression, ordinary values such as J=1 and K=1 all become No and end as the error L=−1, and the flow proceeds to the amount calculation only when both J and K exceed 20. Moreover, when the flow proceeds with Yes, J>20 and K>20 mean that J+K always exceeds 40, so the next decision J+K≧20 is always Yes. As a result, the branch without the discount, L=3,000×J+1,000×K, is never executed and becomes unreachable processing, which contradicts what is written in the flow diagram.
- How to read a flow diagram (flowchart) — a diamond decision symbol evaluates a condition and branches to either Yes or No. In a question that asks which conditional expression goes in a blank, (1) infer the role of the condition from what is done beyond that branch, and (2) verify whether, with the choice you insert, every process in the flow diagram is reachable. In a function that obtains an amount, a branch that returns an impossible value such as −1 is conventionally read as error handling that represents the failure of an input check (a validity check).
- Choosing between and (logical AND) and or (logical OR) — and is true only when all the conditions it joins are satisfied, while or is true if any one of them is satisfied. In an input check that sets a valid range for each of several variables, join the range condition of each variable with and. If or is used here, a value passes even when one side is out of range, and the check does not work.