answers for notes compulsory part d.b 2010-11-15

Post on 17-Jan-2016

218 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Answers for notes Compulsory Part D.b

2010-11-15

請注意!• 在紙上畫的 flowchart 與用 RAPTOR 做

出來的符號形狀有些分別。• 特別留意以下符號︰

– Input/Output box– Decision box– Pre-defined process box– 在 program flowchart 不應出現橢圓形內有

Loop 的符號!

Task D.b.01 (p. 3)Get Worktime, RateIf Worktime, > 7, then

Overtime Worktime – 7Else

Overtime 0End IfRegularWage Worktime × RateIf Overtime, > 0, then

OvertimeWage Overtime × Rate × 40%

ElseOvertimeWage 0

End IfTotalWage RegularWage + OvertimeWageOutput TotalWage

START

Get Worktime,Rate

Worktime > 7 ?

Overtime Worktime – 7

Overtime 0

Yes No

RegularWage Worktime × Rate

Overtime > 0 ?

OvertimeWage Overtime × Rate × 40%

Yes No

TotalWage RegularWage + OvertimeWage

OvertimeWage 0

Output TotalWage

END

Short questions on p. 4

• In an assignment, the destination should be a variable/memory location.

• “X + 1” is an expression

• “3” is a numeric value

• “X + Y” is an expression

Not a memory location / variable

Short questions on p. 5

1. When X is 10, the output is 30.2. When X is ‘A’, there is an error (can’t multiply

string).3. [X X + 3]:

• When X is 10, the output is 13.• When X is ‘A’, the output is ‘A3’.

• Explanation: The operation “*” (multiplication) can only operate on numbers. However, the operation “+” can operate on numbers (meaning “addition”) and text (meaning “string concatenation”).

Task D.b.02(p. 5-6)Draw graph windowDraw black backgroundDraw top circle in redDraw middle circle in grayDraw bottom circle in grayDelay for 1 secondDraw middle circle in yellowDelay for 1 secondDraw top circle in grayDraw middle circle in grayDraw bottom circle in green

START

END

Draw graph window

Draw black background

Draw top circle in red

Draw middle circle in gray

Draw bottom circle in gray

Delay for 1 second

Draw middle circle in yellow

Delay for 1 second

Draw top circle in gray

Draw middle circle in gray

Draw bottom circle in green

Raptor:

Short questions on p. 7

1. For ages 16 & 17, the output is “Please buy a child ticket.”For ages 18, 19 & 20, the output is “Please buy an adult ticket.”

2. For ages 16, 17 & 18, the output is “Please buy a child ticket.”For ages 19 & 20, the output is “Please buy an adult ticket.”

• The difference occurs at the age 18. In the first case, the condition “Age ≥ 18” includes the age 18. In the second case, the condition “Age > 18” does NOT include the age 18.

Short questions on p. 9

• The answer is “7” for all questions.

Task D.b.03 (p. 10)START

Get X

X > 0 ?Yes No

END

Output“It is positive.” X = 0 ?

Yes No

Output“It is zero.”

Output“It is negative.”

Get X

If X > 0, thenOutput “It is positive.”

Else If X = 0, thenOutput “It is zero.”

ElseOutput “It is negative.”

End If

1. Positive number: 5 symbols are evaluated.

2. Negative number: 6 symbols are evaulated.

3. Zero: 6 symbols are evaluated.

Task D.b.04 (p. 11)

START

Get A, B, C

A > B ?Yes No

END

B > C?Yes No

A > C ?Yes No

Output A +“ is the largest.”

Output C +“ is the largest.”

Output B +“ is the largest.”

Output C +“ is the largest.”

Get A, B, C

If A > B, then

If A > C, then

Output A + “ is the largest.”

Else

Output C + “ is the largest.”

End IfElse

If B > C, then

Output B + “ is the largest.”

Else

Output C + “ is the largest.”

End If

End If

Short questions on p. 12

1. When Surname = ‘LEUNG’ and Sex = ‘M’, the output is “Mr LEUNG”.

2. When Surname = ‘LUI’ and Sex = ‘F’, the output is “Ms LUI”.

3. When Surname = ‘CHUNG’ and Sex = ‘K’, the output is “Ms CHUNG”.

4. Modified algorithm:Input Surname, Sex

If Sex = ‘M’, then

Display ‘Mr ’ + Surname

Else If Sex = ‘F’, then

Display ‘Ms ’ + Surname

Else

Display ‘Invalid Sex’

End If

START

Get Surname, Sex

Sex = ‘M’ ?Yes No

END

Output‘Mr ’ + Surname Sex = ‘F’ ?

Yes No

Output ‘Ms ’ + Surname

Output‘Invalid Sex’

Short questions on p. 13

• Pass the Chinese test AND the English test

1. Chinese test: Fail; English test: Fail – No

2. Chinese test: Fail; English test: Pass – No

3. Chinese test: Pass; English test: Fail – No

4. Chinese test: Pass; English test: Pass – Yes

• Pass the Chinese test OR the English test

1. Chinese test: Fail; English test: Fail – No

2. Chinese test: Fail; English test: Pass – Yes

3. Chinese test: Pass; English test: Fail – Yes

4. Chinese test: Pass; English test: Pass – Yes

Short questions on p. 14

• Not pass English test:1. Fail – Yes

2. Pass – No

1. Truth table for ‘AND’

X Y X AND Y

FALSE FALSE FALSE

FALSE TRUE FALSE

TRUE FALSE FALSE

TRUE TRUE TRUE

2. Truth table for ‘OR’

X Y X AND Y

FALSE FALSE FALSE

FALSE TRUE TRUE

TRUE FALSE TRUE

TRUE TRUE TRUE

3. Truth table for ‘NOT’

X NOT X

FALSE TRUE

TRUE FALSE

Exercise D.b.02 (p. 15)

Inputs X > 10? Y < 20? not (Y < 20)? X > 10 and not (Y < 20) ? Output

X = 5, Y = 10 FALSE TRUE FALSE FALSE 0

X = 5, Y = 30 FALSE FALSE TRUE FALSE 0

X = 15, Y = 10 TRUE TRUE FALSE FALSE 0

X = 15, Y = 30 TRUE FALSE TRUE TRUE 1

X = 10, Y = 20 FALSE FALSE TRUE FALSE 0

1. Algorithm A

Inputs X > 10? Y < 20? not (Y < 20)? X > 10 or not (Y < 20) ? Output

X = 5, Y = 10 FALSE TRUE FALSE FALSE 0

X = 5, Y = 30 FALSE FALSE TRUE TRUE 1

X = 15, Y = 10 TRUE TRUE FALSE TRUE 1

X = 15, Y = 30 TRUE FALSE TRUE TRUE 1

X = 10, Y = 20 FALSE FALSE TRUE TRUE 1

2. Algorithm B

Task D.b.05(p. 16-17)

START

Get Size

Size = “S” or Size = “s” ?

Yes No

END

Output“The price is $70.”

Size = “M” or Size = “m” ?

Yes No

Output“The price is $75.”

Size = “L” or Size = “l” ?

Yes No

Output“The price is $80.”

Output“Invalid size.”

Get Size

Select case Size:“S”, “s”: Output “The price is $70.”

“M”, “m”: Output “The price is $75.”

“L”, “l”: Output “The price is $80.”

Default: Output “Invalid size.”

End Select

Task D.b.06(p. 17-18)START

Get Mth

Mth = 1 or Mth = 3 orMth = 5 or Mth = 7 or Mth = 8 or

Mth = 10 or Mth = 12 ?

Yes No

END

Ndays 31Mth = 4 or Mth = 6 orMth = 9 or Mth = 11 ?

Yes

Ndays 30

No

Mth = 2 ?Yes

Ndays 28 Ndays 0

No

Output “In the year 2009, there are ”+ Ndays + “ days in the month ” + Mth

Get Mth

Select case Mth:1, 3, 5, 7, 8, 10, 12: Ndays 31

4, 6, 9, 11: Ndays 30

2: Ndays 28

Default: Ndays 0

End Select

Display “In the year 2009, there are ” + Ndays + “ days in the month ” + Mth

Task D.b.07

By ICT2013-C 4D 何美善

Task D.b.07 (p. 20)Get Surname, Sex

While Sex = “M” or Sex = “m” and Sex = “F” and Sex = “f”, do

Output “Incorrect sex. Enter again.”

Get Sex

End While

If Sex = “F” or Sex = “f”, then

Output “Ms ” + Surname

Else

Output “Mr ” + Surname

End If

START

Get Surname, Sex

Sex = “M” or Sex = “m” or Sex = “F” or Sex = “f” ?

Yes

No

END

Output“Incorrect sex. Enter again.”

Sex = “F” or Sex = “f” ?

Output“Ms ” + Surname

Output“Ms ” + Surname

Yes No

Get Sex

Task D.b.07 (p. 20)Get Surname, Sex

While Sex ≠ “M” and Sex ≠ “m” and Sex ≠ “F” and Sex ≠ “f”, do

Output “Incorrect sex. Enter again.”

Get Sex

End While

If Sex = “F” or Sex = “f”, then

Output “Ms ” + Surname

Else

Output “Mr ” + Surname

End If

START

Get Surname, Sex

Sex ≠ “M” and Sex ≠ “m” and Sex ≠ “F” and

Sex ≠ “f” ?

Yes

No

END

Output“Incorrect sex. Enter again.”

Sex = “F” or Sex = “f” ?

Output“Ms ” + Surname

Output“Ms ” + Surname

Yes No

Get Sex

Task D.b.07 (p. 20)Get Surname, Sex

While Sex ≠ “M” and Sex ≠ “m” and Sex ≠ “F” and Sex ≠ “f”, do

Output “Incorrect sex. Enter again.”

Get Sex

End While

If Sex = “F” or Sex = “f”, then

Output “Ms ” + Surname

Else

Output “Mr ” + Surname

End If

START

Get Surname, Sex

Sex ≠ “M” and Sex ≠ “m” and Sex ≠ “F” and

Sex ≠ “f” ?

No

Yes

END

Output“Incorrect sex. Enter again.”

Sex = “F” or Sex = “f” ?

Output“Ms ” + Surname

Output“Ms ” + Surname

Yes No

Get Sex

Task D.b.07 (p. 20)Get Surname, Sex

While Sex ≠ “M” and Sex ≠ “m” and Sex ≠ “F” and Sex ≠ “f”, do

Output “Incorrect sex. Enter again.”

Get Sex

End While

If Sex = “F” or Sex = “f”, then

Output “Ms ” + Surname

Else

Output “Mr ” + Surname

End If

START

Get Surname, Sex

Sex = “M” or Sex = “m” or Sex = “F” or Sex = “f” ?

Yes

No

END

Output“Incorrect sex. Enter again.”

Sex = “F” or Sex = “f” ?

Output“Ms ” + Surname

Output“Ms ” + Surname

Yes No

Get Sex

Task D.b.08 (p. 22) START

Yes

No

END

Output Count,Sum, Max, Min

Count 0 Sum 0

Get Num

Max Num Min Num

Num ≠ 0 ?

Count Count + 1

Sum Sum + Num

Num > Max ?

Max Num

Yes

No

Num < Min ?

Min Num

Get Num

Yes

No

Count 0

Sum 0

Get Num

Max Num

Min Num

While Num ≠ 0, do

Count Count + 1

Sum Sum + Num

If Num > Max, then

Max Num

End If

If Num < Min, then

Min Num

End If

Get Num

End While

Output Count, Sum, Max, Min

Task D.b.09 (p. 24)Get P, R, n

For I from 1 to n, do

P P × (1 + R ÷ 100)

Display “Year ” + I + “: Amount = $” + P

Next I

START

Get P, R, n

END

I 1

I > n ?

P P × (1 + R ÷ 100)

Display“Year ” + I + “: Amount = $” + P

I I + 1

Yes

No

Exercise D.b.03 (p. 25-26)

1. Ask for the scores of the ten studentsFor I from 1 to 10, do

Get Data[I]

Next I

2. Display the scores of all the studentsFor I from 1 to 10, do

Display Data[I]

Next I

Task D.b.10 (p. 26)

Array input Array output

Raptor:

I 1

I > N ?

Get Data[I]

I I + 1

Yes

No

I 1

I > N ?

DisplayData[I]

I I + 1

Yes

No

Exercise D.b.04 (p. 27)

For I from N down to K, do

Data[I + 1] Data[I]

Next I

Data[K] NewItem

N N + 1

Task D.b.11 (p. 28)

I N

I < K ?

I I – 1

Get NewItem, K

Yes

No

Data[I + 1] Data[I]

N N + 1

Data[K] NewItem

Raptor:

Exercise D.b.05 (p. 29)

For I from K + 1 to N, do

Data[I – 1] Data[I]

Next I

N N – 1

Task D.b.12 (p. 29)

I K + 1

I > N ?

I I + 1

Get K

Yes

No

Data[I – 1] Data[I]

N N – 1

Raptor:

Exercise D.b.06 (p. 30-31)

1. Increase the score of each student by 5 marks

For I from 1 to 10, doData[I] Data[I] + 5

Next I

2. Display the sum of all the scores and the average score of the studentsSum 0For I from 1 to 10, doSum Sum + Data[I]

Next IAverage Sum ÷ 10Display Sum, Average

3. Display the maximum scoreMax 0For I from 1 to 10, do

If Data[I] > Max, thenMax Data[I]

End IfNext IDisplay Max

4. Find the student having the maximum scoreM 1For I from 1 to 10, do

If Data[I] > Data[M], thenM I

End IfNext IDisplay “Student ” + M

5. List all students who fail the testFor I from 1 to 10, do

If Data[I] < 50, thenDisplay “Student ” + I

End IfNext I

Task D.b.13 (p. 32)

top related