answers2.doc
TRANSCRIPT
-
8/8/2019 answers2.doc
1/29
1
Answers Answers
Section 2: Problem-solving and Program Design
End-of-topic questions 1
1. List the main stages you should go through when trying to solve a generalproblem.
A. Stage 1: Define the problem
Stage 2: Analyse the problemStage 3: Propose and evaluate possible solutions
Stage 4: Select and ustify the optimal solutions
Stage !: "mplementation and revie#
2. Variables and constants are both identifiers. Explain the difference betweenthem.
A. $ariables are identifiers that are capable of changing their values%constants are identifiers that have fi&ed values.
3. What is the difference between an integer and a floating point number
A. "ntegers are #hole numbers' positive or negative #ithout decimal places%floating point or real numbers are positive or negative numbers #ithdecimal values.
End-of-topic questions 2
1. What is an algorithm
A. An algorithm is a finite number of accurate' unambiguous steps that solvea problem or tas(.
2. List four characteristics of a good algorithm.
A. )he number of steps must be finite% the steps must be precise% the stepsmust be unambiguous% the steps must terminate.
3. What are the three main steps involved in creating an algorithm
A. "nput step% processing step% output step
-
8/8/2019 answers2.doc
2/29
2
!. What is meant by assignment
A. Assignment is #here values are allocated to variables.
-
8/8/2019 answers2.doc
3/29
3
End-of-topic questions 3
1. "ame three types of control structures that are commonly used inprogramming languages.
A. Se*uencing' selection and repetition.
2. #tate the difference between bounded iteration and unbounded iteration.
A. +ounded iteration is the repetition of a set of instructions a fi&ed number of times% unbounded iteration is repeating a set of steps a number of timesuntil a particular condition becomes false.
3. $ive one example of a statement used in the following control structures%
a. #election
b. &ounded iteration
c. 'nbounded iteration
A. a. if,then,else
b. for,endfor
c. #hile,end#hile
End-of-topic questions 4
1. (or each of the following problem statements%a. identify the input) output and processing steps.
b. write the algorithm.
- Write an algorithm to read three numbers and find their product.
A. a.
b. Step 1:start
Step 2:read num1'num2' num3
Step 3: product num1-num2-num3
Step 4: #rite product
Step !: stop
"nput Processing utput
)hreenumbers/num1'num2'num30
Accept numbers
alculate product
Store results in product
Display product
Product
-
8/8/2019 answers2.doc
4/29
4
- Write an algorithm that displays the area of a rectangle by acceptinglength and width from the user.
A. a.
b. Step 1:start
Step 2:read length' #idth
Step 3: area length - #idth
Step 4: #rite area
Step !: stop
3. Write an algorithm that will read the radius of a circle and calculate anddisplay its perimeter.
A. a.
b. Step 1:start
Step 2: read radius
Step 3: circumference 2 - 3.14- radius
Step 4: #rite circumference
Step !: stop
"nput Processing utput
length' #idth
Accept length' #idth
alculate area
Store results in area
Display area
Area
"nput Processing utput
radius
Accept radius
alculate circumference
Store results incircumference
Display circumference
circumference
-
8/8/2019 answers2.doc
5/29
5
End-of-topic questions 5
1. Write a narrative algorithm that will accept the radius of a circle and
calculate and display its area.
A.
Step 1:Start
Step 2: et radius
Step 3: alculate area by using the formula 3.14 - radius - radius and store itin area
Step 4: Display the area
Step !: Stop
2. Write a pseudocode algorithm that will accept three numbers and calculate
and display the largest of the three. A.
"nput Processing utput
)hreenumbers
Accept threenumbers
alculate the total
Store results
Display the result
Sum
ead a' b' c
Sum abc 5rite sum
"nput Processing utput
radius
Accept radius
alculate area
Store results in area
Display area
area
-
8/8/2019 answers2.doc
6/29
3. Write a pseudocode algorithm that will accept the mar*s in a test for a groupof 2+ students then calculate and display the average mar*.
A.
"nput Processing utput
)hreenumbers
Accept t#enty,fivenumbers
alculate the total
Store results in sum
alculate the average
Display the average
Average
)otal 6 7
8or " 6 1 to 2! ead mar(
)otal 6 total mar(
9ndfor
Average 6 total2!
5rite average
!. ,dd steps to the previous algorithm so it also displays the highest mar*.
A.
;argest 6 7 8or " 6 1 to 2!
ead mar(
"f mar(
-
8/8/2019 answers2.doc
7/29
!
"#ec$ %our progress 1
1. -raw flowcharts for the following programs%
a. rogram to accept the price of an item and calculate its V,/ at 1+0.
start
read price
vat price -.1!
#rite vat
stop
-
8/8/2019 answers2.doc
8/29
&
b. rogram to accept money in '# dollars and convert it to its euivalentlocal currency.
read us
ec us -2.=1
#rite ec
stop
start
-
8/8/2019 answers2.doc
9/29
'
c. rogram to accept the length and width of a rectangle and calculate itsarea.
read length' #idth
area 6 length - #idth
#rite area
stop
start
-
8/8/2019 answers2.doc
10/29
1(
End-of-topic questions
1. -raw flowcharts for the following%
a. , program that accepts 2 numbers and finds their product.
start
product 6 1numcount 67
read num
isnumcount>62
7
product 6 product - numnumcount 6numcount 1
yes
#rite product
no
stop
-
8/8/2019 answers2.doc
11/29
11
b. , program that accepts !+ numbers and finds the number of eroes andnon4eroes in them.
numcount 67 poscount 6 7 negcount 67
read num
is
numcount>64!
yes
#rite product
no
stop
ifnum
-
8/8/2019 answers2.doc
12/29
12
c. , program that accepts a set of integers and finds the number of positiveand negative numbers. /he program stops when the user enters for the
integer value.
numcount 67 poscount 6 7 negcount 67
read num
is
numcount>64!
yes
#rite product
noif
num
-
8/8/2019 answers2.doc
13/29
13
d. , program that accepts a set of numbers and finds the smallest amongthem. /he program stops when the user enters 555 as the number.
read num
no 6 sum num
no =
sum
+
num
isnum6 7555
8snum7smlst
smlst ,1
#rite sum
stop
yes sum num
yes
=
sum
+
num
smlst num
start
read num
-
8/8/2019 answers2.doc
14/29
14
"#ec$ %our progress 2
1. 9reate a trace table to determine the output of the following algorithm%
#tep 1% set x ← +
#tep 2% set y ← 1
#tep 3% set ← 3
#tep !% set x ← x:y
#tep +% set y ← x :
#tep ;% set ← x:y:
#tep
-
8/8/2019 answers2.doc
15/29
15
"#ec$ %our progress 3
9reate a trace table to determine the output of the following algorithm%
#tep 1% set a← 1
#tep 2% set b← 12
#tep 3% set c← 23
#tep !% set a← a:b:c
#tep +% set b← a4b
#tep ;% set c← b4c
#tep
-
8/8/2019 answers2.doc
16/29
1
"#ec$ %our progress 4
1. 9reate a trace table to determine the output of the following algorithm%
#tep 1% set a ← <
#tep 2% set x ← 1
#tep 3% While a67
set x← x : a
set a ← a − 1
#tep !% endwhile
#tep +% write x
A.
a &
=
1
@
14
!
1B
4
23
3
2
2
2@
1
2B
7 )he output #ill be 2B.
-
8/8/2019 answers2.doc
17/29
1!
End-of-topic questions !
1. 9reate a trace table to determine the output of the following algorithms%
#tep 1% x ← +
#tep 2% for i← 1 to 1
set x > x : +
endfor
#tep 3% write x
A.
& i
!
1
17
2
1!
3
27
4
2!
!
37
3!
=
47
@
4! B
!7
17
!!
)he output #ill be !!.
-
8/8/2019 answers2.doc
18/29
1&
"#ec$ %our progress 5
1. What is meant by stepwise refinement or top4down design
A. )op,do#n design or step#ise refinement is converting a bigger probleminto main tas(s and subtas(s. )he maor tas(s are completed first and thesub,tas(s are completed after.
2. List three advantages of using top4down design to solve a problem.
A. eusability C sections of algorithm can be used again in other algorithmsthat re*uire a similar solution' #hich saves time in future development ofalgorithms% 9asy to follo# C as each tas( or subtas( performs only onefunction% 9asy to maintain C as each tas( is by itself' if there is a need for
change in one tas( you need to concentrate on that tas( only.
End-of-topic questions &
1. Explain why a top4down design approach is sometimes used to solveproblems.
A. 5hen #e consider a problem it is often easier to vie# the problem as a#hole' and then brea( it do#n into smaller problems. 5e can thenconcentrate on one of the smaller problems and leave the others untillater.
-
8/8/2019 answers2.doc
19/29
1'
2. , hotel wants to have a room boo*ing system. When a guest arrives thereceptionist would be able to see which rooms are free and allocate one to the
guest. When the guest leaves) the room would be mar*ed as free again. Eachday the cleaners would be given a list of the rooms that are in use. ,t any timethe receptionist would be able to use the room number to find the name andhome address of a guest) or could search the room system for a namedguest. -raw a top4down design diagram to show how the room boo*ingsystem could be developed.
A.
Room booking
Room allocation Cleaning tasks Searches
Search for
name and
address of
guest
Search for
room
occupied by
guest
-
8/8/2019 answers2.doc
20/29
2(
End-of-section questions
)ultiple-c#oice questions
1. /he following are problem4solving stages
8. analyse the problem
88. suggest possible solutions
888. implement and review
8V. define the problem
V. select the best solution
/he correct order of stages is%
, 8) 8V) 88) 888) V
& 8V) 8) 88) V) 8889 8V) 88) 8) V) 888
- 8) 88) 8V) 888) V
A. +
2. Which one of the following is a variable
, x
& ?x@
9 +
- ?+@
A. A
3. /o store fractional values) use the data type%
, integer
& floating point
9 character
- string
A. +
!. Which of the following is not a characteristic of a good algorithm , precise
& finite number of steps
9 ambiguous
- logical flow of control
A.
-
8/8/2019 answers2.doc
21/29
21
+. -iagrammatic representation of an algorithm is%
, flowchart
& data flow diagram
9 algorithm design
- pseudocode
A. A
;. Which of the following isAare control structures
, selection
& seuencing
9 repetition
- all of the above A. D
y9 x7y and x>y
- x6 7y
A.
-
8/8/2019 answers2.doc
22/29
22
1.9onsider the following algorithm segment%
x > x:y
y > x − y
write y) x
#uppose x > 1 and y > 1+) after the completion of the above statements) thevalues of x and y will be%
, 2+ and 1
& 1 and 2+
9 1 and 4+
- 4+ and 1
A. +
-
8/8/2019 answers2.doc
23/29
23
Structured questions
1 a. List the main stages of problem4solving process for computer4basedproblem4solving.
b. (or each of the stages identified in 1a above) briefly describe the activitiesthat are involved in it.
A. a. Stage 1: Definition of the problem
Stage 2: Analyse the problem
Stage 3: Propose and evaluate possible solutions
Stage 4: Develop and represent an algorithm
Stage !: )est and validate the algorithm
Stage : "mplement the algorithm
b. Stage 1: Definition of the problem
"n this stage' the problem is loo(ed at carefully and if it is not phrased properly' it is modified to ensure that it is clearly stated.
Stage 2: Analyse the problem
"n this stage' identify the inputs to be used' outputs re*uired' values tobe stored /if any0' and the processing that needs to be done to get thecorrect outputs.
Stage 3: Propose and evaluate possible solutions
ere identify solutions to the problem and evaluate each option to see
#hich is most appropriate and choose the best option.Stage 4: Develop and represent an algorithm
"n this stage' brea( do#n the problem into simple manageable stepscalled algorithm so that they can be handled easily.
Stage !: )est and validate the algorithm
ere' chec( the algorithm #ritten using some values to ensure that it produces the re*uired results.
Stage : "mplement the algorithm
"n this stage' #rite the steps of algorithm using a programming language so that computer can operate on it.
-
8/8/2019 answers2.doc
24/29
24
2. 8C diagrams can be used to brea* down a problem into its components.
a. #tate what is meant by ?8C diagrams@
b. 9reate an 8C chart for the following problem statement% ?,ccept thevalues of length and width of a rectangle and calculate its area@
A. a. An "P diagram is a table #ith three columns sho#ing the input' outputand processing parts of the problem.
b.
"nput Processing output
length and#idth
Accept length and#idth
alculate area
Store results in areaDisplay area
Area
3. a. #tate the difference between%
i. constants and variablesii. integer data type and floating point data type
iii. character data type and string data type
b. -efine the following terms%
i. identifier
ii. literal
A. a. i. ?9onstants are fixed values while variables identify a memory locationthat is capable of storing values that can be changed.@
ii. "ntegers are #hole numbers' positive or negative #ithout decimal places' e.g. ,4!' and 12% floating point or real numbers are positiveor negative numbers #ith decimal values' e.g. ,!. and .=.
iii. haracters are anything that you can (ey in from a (eyboard such asletters' numbers and special characters% string is a linear group ofcharacters to be stored as one.
b. i. "dentifier is the name used for identifying a variable or a constant.
ii. ;iterals are constants that are #ritten literally as itself rather than as a
value.
!. 9reate a narrative algorithm that will prompt the user to enter two numbersand find their difference.
A. Step 1: Start
Step 2: et t#o numbers
Step 3: Subtract second number from the first number
Step 4: Store the results in difference
Step !: Display difference
-
8/8/2019 answers2.doc
25/29
25
+. Write a pseudocode that prompts the user to enter a value and display itssuare and cube values.
A. Step 1: start Step 2: read num
Step 3: s*uare num - num
Step 4: cube num - num- num
Step!: #rite s*uare' cube
Step : stop
;. -raw a flowchart and write a pseudocode that will prompt the user to entertwo numbers and display their product.
A.
read n1'n2
prod 6 n1 - n2
#rite prod
stop
start
-
8/8/2019 answers2.doc
26/29
2
length FwidthG
A.
Step 1: start
Step 2: read length' #idth
Step 3: area length - #idth
Step 4: #rite area
Step !: stop
read length' #idth
area 6 length - #idth
#rite area
stop
start
-
8/8/2019 answers2.doc
27/29
2!
=. Write a pseudocode that will accept 2+ integers and displays the numberof positive and negative numbers.
A. Step 1: start Step 2: poscount 7' negcount 7
Step 3: for i 1 to 2!
Step 3: read num
Step 4: if num
-
8/8/2019 answers2.doc
28/29
2&
17
3!
17
b. )he algorithm #ill print 17 3! 17
1.a. -raw a trace table for the following algorithm.
b. What is the output of this algorithm
x > 1y > 1+
> 2
x> x:y
y > x:
> x:y:
if x 7y then
x > x :+
else
x > x B 3endif
if y 6 then
y > y:1
else
y > y B 1
endif
print x) y)
A. a.
& y ?
17
1!
2 7
2 !
4
!
-
8/8/2019 answers2.doc
29/29
B7
2 2
! !
b. )he output #ill be 22 !! B7