Download programming notes

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Elementary mathematics wikipedia , lookup

Halting problem wikipedia , lookup

Transcript
ICT Chp 21,22,23,24 Problem-solving procedures and Algorithm
Problem-solving concepts
1.
What is problem-solving?
Problem-solving is a process of analyzing the description of problem until we reach a solution. To
solve complex problems, we can divide the problem into smaller parts (i.e. modules) and solve them
one by one.
2.
Problem-solving procedure
Before analyzing, we have to know exactly what the problem is. So, the first step is:
1.
Problem Identification
After that, we should do
2.
Problem Analysis
Then, make a draft framework
3.
Algorithm Design
Then, try to solve the problem by
4.
Developing a Solution
Now, we have a solution, but we are not sure it is workable or not, so, we should perform
5.
Debugging and Testing
Finally, a solution is developed, but it is not the end, to make improvement become smooth, we should
remarks any
6.
Documentation
Algorithm Design –
In the state of Algorithm Design, Pseudocode and Flowchart are two commonly used techniques
to do the algorithm.
`
will have no standard format (syntax) and so a segment of Pseudocode will have no syntax
error but logical error only.
What is syntax error?
JavaScript is a programming language with standard format, so it may result syntax error. E.g.
X = 10;
Syntax error
Y = 20;
How to fix?
/
logical error
/
logical error
/
logical error
Y = 2X;
2X = Y  3;
document.output(“Who are you?”);
Syntax error
How to fix?
window.ResizeTo(100,200);
Syntax error
1
How to fix?
for (i=0,i<10,i++) {
Syntax error
alert(“hello!”);
/
logical error
/
logical error
How to fix?
}
for (i=0;i>10;i++) {
Syntax error
alert(“hello!”);
How to fix?
}
Syntax error / logical error / run-time error
Apart from syntax error and logical error, there is another source of error. It is run-time error. A
run-time error occurs when a program crashes. For example,
X = 2;
Y = 3;
Z = (X – Y) / (X + Y – 5);
Obviously, the 3rd line of code will create run-time error. (OR in this case, it is a divided-by-zero
error.)
Apart from divided-by-zero error, can you think of any other type of run-time error?
Example:
X = 5;
Y = 10;
WHILE (X > 0) {
X = X + 1;
Y = X*Y;
}
What kinds of error will it create? Select the correct answer:
Infinite Looping
/
data overflow error
/
Out of memory stack error
Compilation (translator / compiler)
So, writing a program should avoid errors include syntax error, logical error and run-time error. As a
matter of fact, a program has to be translated into machine code before execution. This process is
called compilation.
Below shows the process:
High Level Programming Language
->
Compiler
->
Low Level Programming Language
for (i=0;i>10;i++) {
0010001010111000001010100
alert(“hello!”);
1010100010000100111111011
}
1000011000010101010111110
1111111101010001011110...
2
i.e.
Source program
Compiler
Object program
High level programming language is the programming language that human being can understand,
example include JavaScript, Java, C++, Pascal, etc.
But for computer, it can only recognize machine code like 10010101 represents delete file, or
11001010 represents create folder, etc. However, since each machine code may be different for
different OS (operating system), so, Low level programming language is not portable / compatible.
A compiler is a software program that it will translate a high level programming language into a
machine language. So, a compiler should usually be platform oriented (machine dependent).
And so, an executable file should be built according to the OS of the computer. So, an executable
file should not be portable but the high level programming language may be portable.
Conclusion:
1.
Error
-
a violation of the grammatical rule of a programming language
-
can be checked by the
-
eg: wrong spelling of some reserved word, using an unknown variable
2.
Error
-
it occurs when a
-
eg: division by zero, out of memory
3.
program terminates abnormally
Error
-
it is mainly caused by an incorrect
-
it produces an
-
it
-
eg: the calculated average mark of a test is greater than 100
result
be detected by the translator / compiler
Pseudocode Sample 1:
SET total to zero
Description of the program:
REPEAT
READ Temperature
IF Temperature > Freezing THEN
INCREMENT total
END IF
UNTIL Temperature < zero
Print total
Pseudocode Sample 2:
3
Input a number N
State the output of the program with input as
X <- 1
WHILE (X < N) AND (N < 5)
X <- X + 2
(i)
4
(ii)
14
END
Output X, N
Remember, there is no specific syntax for Pseudocode and so it will have no syntax error.
Note that in writing program, logical comparison is very useful and important. Below show some
operations:
X = TRUE; Y = FALSE;
NOT X
NOT(NOT Y)
X and Y
NOT X AND NOT Y
(X OR NOT(X AND Y))
TRUE
FALSE
Flowchart is a way to represent the logic and actions of an algorithm graphically.
Flowchart Sample:
Developing a Solution –
It is a top-down approach that divides a complex solution into some manageable subprograms
(module).
To develop a solution, usually we would create a program based on algorithm in “algorithm design”
stage. That is, the output of this stage is a computer program. Below shows two simple programs
in JavaScript:
function timeout1() {
var x = 10;
for (i=1;i<x;i++) { alert(i);}
4
}
function timeout2() {
var Limit = 10;
for (counter=1;counter<Limit;counter++) {
alert(i);
}
}
Comment
// a FOR LOOP to popup a dialogue box.
By investigating the above two computer programs, we can find that there are several techniques
which are important in programming design.
1.
Make good use of comment.
2.
Define meaning variables.
3.
Proper indent.
4.
Modular approach
Other than the above techniques, it is also important to set variables with appropriate data type.
There are several data type in programs. They include:
Data type
Example
Description
Real
2.534, 0, -28
Decimal numbers
String
‘abc’, ‘2.354’, ‘28abc’
Alphanumeric or simply numeric
Integer
15, 27, 91, -11
Integer
Boolean / Logical
TRUE, FALSE
Logical data
Note that Real and Integer in real life is unlimited. However, in the world of computer, it is limited by
the number of memory allocated to these data. For example, if 8 bits is set for a data type of an
integer, then, it ranges –128 to 127 only. As a result, calculation of those data type may cause
system crash and it is what we called overflow error or run-time error.
However, as a matter of fact, it is not often to encounter data out of the range, so, there are a
number of different data type, for example, INTEGER use 16 bits, LONG INTEGER use 32 bits, etc.
The case of real is similar.
Below is an example of a program with two different data types:
<script language= "JavaScript">
x = "12";
y = "34";
z = x + y;
document.write (z + "<br>");
a = pareseInt(x);
b = parseInt(y);
c = a + b;
document.write (c);
</Script>
What would be the expected output?
5
Exercise:
1.
2.
The purpose of adding comments to a program is
A.
to evaluate the program.
B.
to increase functionality.
C.
to increase the speed of the program.
D.
to assist program maintenance.
The purpose of using meaningful variable names in a program is
A.
to make the program more readable.
B.
to increase the program development time.
C.
to help translating programs into machine codes.
D.
to test the program more thoroughly.
Debugging and Testing
It is a checking process. Here, “Debugging” refers to the process of locating and fixing defects in a
program. “Testing” is to ensure a program works and is free of error.
To test a program, the program itself should have already compiled successfully. That is, it will not
contain any syntax error. So, basically, testing focuses mainly on finding out logical errors and
run-time error.
To discover logical error, we should test the program with relevant input data and check whether it
will give correct output data.
To discover run-time error, then, should we just test the program with relevant input data? Yes / No
If No, then, what else should be used as the input data? Give an example to support your answer.
Documentation
It aims to describe in detail what a certain program is for and how it is designed, developed and
tested. Although documentation appears at the last stage of the problem-solving procedure, it
should be carried out throughout the whole process.
There are two main categories of written documentation for computer programs: user manuals and
program manuals.
Function of documentation:

It can help programmers maintain the program in the future.
6

Other programmers may have to maintain the program or make changes to it at a later time.

It helps programmers discover errors in the program.

Users can learn how to use the program through a user manual like

how to install

what functions are provided by the program

how to handle a particular task, etc.
Introduction to Algorithm Design
Flowchart symbols
Begin / End
END
START
Process
New <- New + 1
Input / Output
Input X, Y
Output X
Decision
Yes
Y=X?
No
Pre-defined Process
Find the remainder
(R) of Y divided by X
Decision box: It has two common features. One is Branching and the other one is Looping.
Branching / IF / Conditional Statement
No
Looping
Yes
Y=X?
Y=X?
Yes
No
Exercise 1: Arrange the following flowchart symbols to form a program that can determine whether an
inputted data is an odd number or an even number.
7
START
Find the remainder
(R) of X divided by 2
?
Yes
END
No
Exercise 2: Arrange the following flowchart symbols to form a program that can find the maximum divisor
of a number. (Examples: 27 = 3x9, so, 9 would be the maximum divisor.)
To achieve the goal, we should think how to find a maximum divisor by ourselves. That is, what is the
algorithm in our own brain to find the maximum divisor?
Take 27 be the example, the step to find the maximum divisor would be:
Step 1:, Divide 27 by 2, check the remainder.
Step 2: If remainder=0, then 2 would be a divisor.
Step 3: Divide 27 by 3, check the remainder.
Step 4: If remainder=0, then 3 would be a divisor.
Step 5: Do the steps above again and again until it reaches 26 (27 – 1) to find all the divisors.
Step 6: Output the maximum divisor.
To investigate the above steps, you will see it is in fact not 6 steps. It is like infinite steps it does 3 main
steps over and over again. These 3 main steps would be:
1: Runs a counter (like 2, 3 and so on) from 2 to 27 – 1.
2: Divide 27 by the counter and check the remainder.
3: Store the divisor when found one.
These 3 main core steps involve repeated operations, we called looping or
Iteration. The structure of a looping by flowchart is shown on the right:
X=2
Finish the following flowchart so that it
runs a counter from 2 to 27 – 1. It is what
we called a FOR-LOOP.
In JavaScript, it is
For (
){
Yes
…….}
No
8
Finish the following flowchart so that it can let users input a number, then it will output the maximum
divisor.
START
Find the remainder (R)
of Y divided by X
END
9
Exercise 3: Create a flowchart so that it can find the H.C.F. of two inputted numbers.
START
Input X, Y
END
10
In JavaScript, we can find the H.C.F. in the following manner:
<script language= "JavaScript">
function findHCF() {
var data1 = parseInt(num1.value);
var data2 = parseInt(num2.value);
Comment
//parseInt() is a JavaScript function
//to find the value of a text string
if (data1 < data2) { min = data1;}
else {min = data2;}
HCF_found = false;
for (i=2;i<=min;i++) {
R1 = data1 % i;
R2 = data2 % i;
if (R1 == 0 && R2 == 0) {
HCF = i;
HCF_found = true;
}
}
if (HCF_found) {
alert (HCF) }
else { alert("No HCF found!");}
}
</script>
<body>
First Number:<input type=text name=num1><br>
Second Number:<input type=text name=num2><input type=button value=HCF
onClick=findHCF()>
</body>
FOR– Loop
For (i=1;i<10;i++) {
Alert(i);
}
Yes
No
WHILE – Loop
Yes
?
Process
No
11
i = 0;
x = “Numbers include ”;
while (i<5) {
x = x + ", " + i;
i++;
}
x = x + “ and “ + i + “.”
document.write(x);
Yes
No
For the following two flowcharts, dry run them with the input 3 and 5 respectively.
Input X
Input X
S=1
N=2
S=1
N=2
S=S+N
S=S+N
N=N+1
N=N+1
N < X?
N < X?
YES
Output N
Output N
X=3
X=5
(i)
(ii)
Output =
Output =
No. of iteration =
No. of iteration =
Output =
Output =
No. of iteration =
No. of iteration =
Description of the flowchart:
12
YES
REPEAT UNTIL Loop
What is the different between a WHILE
LOOP and a REPEAT UNTIL LOOP?
Process
No
?
Yes
With respect to the diagram on the right,
I=1
S=0
(i)
how many iterations will it make?
(ii)
what is the value of S at the end of the process?
(iii)
what is the value of I at the end of the process?
(iv)
how to modify the iterations so that it will find the
1
2
S=S+I
4
summation of all the multiplications of 3 between 0 and 100?
3
I > 10?
No
I=I+3
Common programming techniques:
1.
Swapping data:
The following program codes is supposed to exchange the data between X and Y. What would be
the output and how to fix it?
X = 3 ;
Y = 5;
X = Y;
Y = X;
After the end of this program, X =
and Y =
The correct program code would be:
2.
Summation from 1 to 100:
We can find the summation of 1 to 100 easily by a FOR-LOOP.
sum = 0;
for (i=1;i<100;i++) {
sum = sum + i;
}
The question is should it be
(i)
for (i=1;i<100;i++)
OR
(ii)
What is the value of i in (i) and (ii) respectively?
13
for (i=1;i<=100;i++) ?
.
3.
Multiplication from 1 to 10.
Investigate what is wrong in the codes below:
Modification:
result = 0;
i = 1;
While (i<10) {
Result = result * i;
i = i + 1;
}
4.
To output the absolute value of an input X.
if (X > 0) {
Y=
}
Else { Y =
X=
5.
}
;
Data type problem, suppose
var X = ‘a’;
var Y = 10;
var Z = ‘c’;
What would be the result of the following?
(i)
Z = X + Z;
(ii)
Y = Y – 5.78;
(iii)
Z = X + Y;
Advance Techniques:
1.
Array + Passing variables (http://www.yll.edu.hk/cs/resources/ict/htm/javascript/reOrder.htm)
<script language= "JavaScript">
function ReOrder(number) {
//pass a number to this function.
var myArray = new Array(); //create a new array
limit = number;
for (i=0;i<limit;i++) {
counter = i + 1;
statement = "Enter name " + counter + ":" ;
response = window.prompt(statement,"");
myArray[i] = response; //store the response into the array
}
document.write ("Original array is shown below:<br>");
for (i=0;i<limit;i++) { document.write(myArray[i]+"<br>");}
document.write ("The sorted array is shown below:<br>");
myArray.sort();
for (i=0;i<limit;i++) { document.write(myArray[i]+"<br>");}
14
}
</script>
<body>
Input different names and it will sort them to give you an ascending list.<br>
<input type=button value="3 numbers" onClick="ReOrder(3)">
<input type=button value="5 numbers" onClick="ReOrder(5)">
<input type=button value="7 numbers" onClick="ReOrder(7)">
</body>
2.
Modular Approach
A program may contain thousands lines of codes or more. So, smaller modules have to be adopted to
form a real life program. The process of splitting up a whole program into modules is called “Modular
approach”. Below shows a sample program that contains modules. Try to discover what the advantages
of using modular approach are. (http://www.yll.edu.hk/cs/resources/ict/htm/javascript/doFraction.htm)
function findHCF(data1, data2) {
function findLCM(x,y) {
value1 = parseInt(x.value);
if (data1 < 0) { data1 = -data1}
value2 = parseInt(y.value);
if (data2 < 0) { data2 = -data2}
LCM = 1;
if (data1 < data2) { min = data1;}
else {min = data2;}
HCF = findHCF(value1, value2);
if (HCF != 1) {
HCF_found = false;
LCM = LCM * HCF;
for (i=2;i<=min;i++) {
value1 = value1 / HCF;
R1 = data1 % i;
value2 = value2 / HCF;
R2 = data2 % i;
}
if (R1 == 0 && R2 == 0) {
LCM = value1 * value2 * HCF;
HCF = i;
return LCM;
HCF_found = true;
}
}
}
if (HCF_found == false) {
function showLCM(In1, In2) {
HCF = 1}
lcm = findLCM(In1, In2);
return HCF;
alert (lcm);
}
}
function DoFraction(N1, D1, N2, D2, operation) {
LCM = findLCM(D1, D2);
Numerator1 =
Numerator2 =
Denominator1
Denominator2
Denominator =
if (operation
Numerator
} else {
Numerator
}
parseInt(N1.value);
parseInt(N2.value);
= parseInt(D1.value);
= parseInt(D2.value);
LCM;
== '+') {
= Numerator1*LCM/Denominator1 + Numerator2*LCM/Denominator2;
= Numerator1*LCM/Denominator1 - Numerator2*LCM/Denominator2;
Fraction31.value = Numerator;
Fraction32.value = Denominator;
HCF = findHCF(Numerator, Denominator);
Fraction41.value = Numerator / HCF;
Fraction42.value = Denominator / HCF;
}
15
<body>
Calculator 1: Finding LCM
<table width=700>
<tr height=100><td width=200>Number 1:<input type=text name="input1" size=5></td><td
width=200>Number 2:<input type=text name="input2" size=5></td><td><input type="button"
value="Find LCM" onClick="showLCM(input1,input2)"></td>
</table>
Calculator 2: Fraction summation
<table width=500>
<tr height=100><td width=60><table><tr><td><input size=5
name="Fraction11"></td></tr><tr><td><hr></td></tr><tr><td><input size=5
name="Fraction12"></td></tr></table></td><td width=15>+</td>
<td><td width=60><table><tr><td><input size=5
name="Fraction21"></td></tr><tr><td><hr></td></tr><tr><td><input size=5
name="Fraction22"></td></tr></table></td>
<td><input type=Button
onClick="DoFraction(Fraction11,Fraction12,Fraction21,Fraction22,'+')" value="Summation
of fractions"></td>
</table>
<table>
<tr bgcolor="yellow"><td width=100>The result is:</td>
<td width=60><table><tr><td><input size=5
name="Fraction31"></td></tr><tr><td><hr></td></tr><tr><td><input size=5
name="Fraction32"></td></tr></table></td>
<td width=15> OR </td>
<td width=60><table><tr><td><input size=5
name="Fraction41"></td></tr><tr><td><hr></td></tr><tr><td><input size=5
name="Fraction42"></td></tr></table></td>
</tr>
</table>
</body>
From the above program code, we can discover that modular approach should be used:
1.
when some standard procedures are frequently used.
2.
because of easier debugging / understanding of the program
3.
because of effective division of labour (i.e. each programmer can focus on particular task.)
4.
for large scale project so that it can be divided into manageable size modules.
16
Exercise:
1.
UNTIL i > 10
Which of the following statements consist of
syntax errors?
2.
3.
(1)
x + 1  4
(2)
x  5 + 8 * 2
(3)
x  "5" + 5
6.
A. (1) only
B. (3) only
C. (1) and (3) only
D. (2) and (3) only
A. 0
B. 4
C. 5
D. 6
Which line of the following pseudocode
segment can be deleted without affecting the
result?
Line number
Statement
10
C = 60
A. (x > x + 5) AND (1 > 2)
20
E = 70
B. (3 > -7) OR (-7 > 3)
30
M = 80
C. (5 >= 5) AND (4 >= 5)
40
T = 95
D. (7+3 < 8) OR (-7 > -6)
50
M=M–T
60
INPUT C
70
C=C+E
80
OUTPUT (C + E)
90
OUTPUT M
Which of the following will give a true result?
Consider the following program:
INPUT x
IF x > 5 THEN
X  5
END IF
The purpose of the program is to
A.
Line 10
B.
Line 20
C.
Line 30
D.
Line 40
A. allow 5 to be entered only.
B. allow numbers less than or equal to 5 to be
7.
entered only.
An algorithm (算法) is
A. a program
C. ensure that the value of X is greater than
B. a set of steps used to solve a problem
5.
C. a description of how the problem has been
D. ensure that the value of X is less than or
solved
equal to 5.
D. a clear diagram showing the existing
problem
4.
How many times will the following WHILE-loop
iterate?
i 6
WHILE i <10 DO
…
ii+1
END WHILE
A. 0
B. 4
C. 5
D. 6
8.
In a report card, the average mark of a student
is outputted as -20. What kind of error is it?
9.
A. syntax error
B. run-time error
C. logical error
D. printing error
Which of the following is/are advantage(s) of
compiler over interpreter?
5.
How many times will the following
(1) Compiled programs run faster
REPEAT…UNTIL-loop iterate?
i 6
REPEAT
…
i  i + 1
(2) Source codes need not be present during
execution.
(3) Compiled programs are easier to develop.
17
10.
A. (1) only
B. (2) only
A. 21
B. 42
C. (1) and (2) only
D. (2) and (3) only
C. 48
D. 336
Object programs are free of
(1) syntax errors
15.
(2) runtime errors
Debugging is to
A. find out any hidden errors
(3) logical errors
B. make corrections to the program so that it
A. (1) only
B. (2) only
C. (1) and (2) only
D. (1), (2) and (3)
is error free
C. kill any bugs or bacteria
D. kill a virus using an anti-virus software
11.
Which of the following would lead to a logical
error?
16.
(1) A “>” sign is mistyped as “<” in a
A. describe what has been done and how the
conditional statement
problem is solved
(2) The assignment statement x  x + 1 is
B. use a word processing software to
mistyped as x  x + 2
produce a document
(3) The reserved (保留字) word “if” is
C. write a letter to the clients telling them the
mistyped as “fi”.
12.
problem is solved
A. (1) only
B. (2) only
C. (1) and (2) only
D. (2) and (3) only
D. re-write the program t make it more
versatile.
Which of the following is/are high level
17.
language(s)?
If we are going to add up all the even numbers
from 1 to 100, which of the following constructs
(1) JAVA
(2) PASCAL
is most likely to be adopted?
(3) machine language
A. (1) only
B. (2) only
C. (1) and (2) only
D. (2) and (3) only
18.
13.
Documentation
Which of the following is machine
A. Looping
B. Branching
C. Sequencing
D. Sorting
What does a compiler do?
A. A compiler translates a program written in
independent?
a high-level language into
A. assembler
machine-executable instruction.
B. assembly language
B. A compiler debugs errors found in a
C. machine language
program written in a high-level language.
D. C source programs
C. A compiler translates high-level language
into an intermediate form, which is then
14.
Consider the following program:
10
P1
20
FOR i =6 to 8 DO
30
executes.
D. A compiler makes a program written in a
high-level language run on a computer.
PP*i
What is the value of P just before the
19.
execution finishes?
Which of the following is the initial step in
problem solving?
18
A. Designing an Algorithm
20.
25.
Which of the following belong(s) to runtime
B. Problem Analysis
errors?
C. Problem Identification
(1) division by zero
D. Documentation
(3) using a reserved word as variable name
Which of the following is/are advantage(s) of
(2) out of memory
A. (1) only
B. (2) only
C. (1) and (2) only
D. (2) and (3) only
breaking a problem into smaller tasks?
(1)
Programs are easier to develop.
(2)
Programs are easier to debug.
error?
(3)
Programs are faster.
(1) A ">" sign is mistyped as "<" in a
A. (1) only
B. (1) and (2) only
C. (2) and (3) only
D. (1), (2) and (3)
26.
Which of the following would lead to a logical
conditional statement.
(2) The assignment statement x <- x + 1 is
mistyped as x<- x+2.
21.
An algorithm is
(3) The reserved word "if' is mistyped as "fi".
A. a computer program.
A. (1) only
B. (2) only
B. a set of ordered steps used to solve a
C. (1) and (2) only
D. (2) and (3) only
problem.
C. a definition of problem.
27.
D. a graphical representation of the steps.
If there is no testing, a computer program may
contain
(1) bugs.
22.
(2) viruses.
Pseudocodes may consist of
(3) worms.
(1)
assignment statements
A. (1) only
B. (1) and (2) only
(2)
conditional statements
C. (1) and (3) only
D. (2) and (3) only
(3)
variable type definition
A. (1) only
B. (2) only
C. (1) and (2) only
D. (2) and (3) only
28.
Debugging means
A. correcting mistakes in a program.
B. writing comments for a program.
23.
Which of the following errors are usually
C. removing small insects from a computer.
discovered the earliest in program
D. converting an algorithm into a program
development?
flowchart.
A. syntax errors
B. logical errors
C. runtime errors
D. data errors
Questions 29 to 31:
Consider the following algorithm:
24.
Which of the following errors are usually
step 1
input x
discovered by using test data?
step 2
if x > 0 then
(1) syntax errors
step 2.1
increase x by 1
(2) logical errors
(3) runtime errors
else
A. (1) only
B. (2) only
C. (1) and (2) only
D. (2) and (3) only
step 2.2
29.
19
decrease x by 1
Which of the following can replace Step 2.1?
30.
A. x <- 1
B. x <- x - 1
C. x <- x + 1
D. x <- x + x
20
A.
B.
C.
D.
Step 2 represents
P <- P/Q
P
15
3
3
5
Q
1
5
1
3
A. a for loop
36.
B. a while loop
Given that P = 1.
C. a conditional statement
10 P <- P * Q
D. an expression
20 P <- P * Q
30 P <- P * Q
31.
32.
If the input data is -5, what will be the value of
What would be the result stored in P after line
x?
30?
A. -4
B. -5
A. the value of Q
B. the square of P
C. -6
D. 5
C. the square of Q
D. the cube of Q
37.
Y = 5 * x - 5
Consider the following program:
Given that X is a number between 0 and 1
10 INPUT X
inclusively. What is the range of Y?
20 IF X >= 0 THEN Y <- 1
A. between 0 and 1 inclusively
30 IF X < 0 THEN Y <- -1
B. between -5 and 0 inclusively
Which of the following statements is true?
C. between 0 and 5 inclusively
A. Y is always a positive number.
D. between 5 and 10 inclusively
B. Y is the absolute value of X.
C. The product of Y and X is always a
33.
positive number.
Given that P = 6. What is the value of P after
D. The product of Y and X is always a
line 20?
10
P <- P + 1
20
P <- P - 2
negative number.
A. 4
B. 5
C. 6
D. 7
38.
Consider the following program:
10 INPUT P, Q
20 IF 2*P - Q > 0 THEN
34.
Given that P = 3 and Q = 5. What are the
30
values of P and Q after line 20?
40 ELSE
10
P <- P + Q
20
P <- P - Q
P
Q
8
0
-2
5
3
5
3
0
A.
B.
C.
D.
35.
50
OUTPUT P
OUTPUT Q
60 ENDIF
If the input data are 5 and 10, the output will be
A. P
B. Q
C. 5
D. 10
Given that P = 3 and Q = 5. What are the
values of P and Q after line 20?
10
39.
P <- P * Q
The following program finds the reciprocal of
an input number:
20
100 INPUT X
D. (6 - 1 < 2) AND (NOT (1 > 2) OR (3 - 5 >
200 OUTPUT 1/X
4) )
Which of the following lines should be added to
40.
avoid division by zero error?
2006 CIT#28
A. 50 IF X < 0 THEN GOTO 100
43.
Which of the following characteristics of high
B. 150 IF X < 0 THEN GOTO 100
level language can reflect the concept of
C. 150 IF X <= 0 THEN GOTO 100
program modules?
D. 150 IF X = 0 THEN GOTO 100
A. Subprogram
B. Comment
C. Condition
D. Looping
Given that P = 1 and Q = 2. Which of the
following logical expressions is TRUE?
2005 CIT#25
A. (P>1) AND (Q>2)
44.
Which line in the following pseudo-code
B. (P > 1) OR (Q > 2)
segment can he deleted without affecting the
C. (P >= 1) AND (Q > 2)
result?
D. (P >= 1) OR (Q > 2)
41.
42.
Line number
Content
10
A = 30
Given that p and q are boolean variables
20
B = 40
storing TRUE and FALSE respectively. Which
30
C = 50
of the following expressions is FALSE?
40
D = 60
A. p OR q
50
C=C–D
B. (NOT p) AND q
60
Input A
C. p OR (NOT q)
70
A=A+B
D. (NOT p) OR (NOT q)
80
Output (A + B)
90
Output C
Which of the following logical expressions is
TRUE?
A. Line 10
B. Line 20
C. Line 30
D. Line 40
A. (NOT (6 > 3)) AND ((4 < 2 + 1) OR (2 + 3 >
1) )
B. NOT ((2 + 3 >= 5) OR (7 - 9 < 2) )
C. (NOT (7 - 3 < 3) ) AND (NOT (3-1 < -2))
START
45.
Consider the following program flowchart:
The value of X and Y just before the end of execution are
X
Y
A. 3
5
B. 3
2
C. 2
5
D. 2
3
X3
Y5
no
X>Y?
YY-X
XX-Y
END
21
yes
START
46. Consider the following program flowchart:
The purpose of the flowchart is to
A.
B.
C.
D.
Input X
find the sign of an input number.
no
find the product of two numbers.
find the absolute value of an input number.
reverse the sign of an input number.
Y  –1
yes
X > 0?
Y1
XY*X
END
START
47. Consider the following program flowchart:
Input X
The purpose of the flowchart is to make sure that the input number is
A.
B.
C.
D.
greater than zero.
less than zero.
less than or equal to zero.
not equal to zero
yes
X > 0?
no
X = 0?
no
START
END
P  27
Q5
48. Consider the following program flowchart:
P >Q?
The output will be
A. 2
B. 3
C. 5
D. -22
Yes
PP–Q
No
Output Q – P
END
49. Consider the following program flowchart:
The values of S and N just before the end of
execution are
S
N
A. 0
0
B. 42
12
C. 50
12
D. 56
14
START
S0
N0
S < 50?
No
END
22
Yes
N  N+2
S  S+N
yes
2005 CIT#27
50. Study the following flowchart.
If the input value is 0, what is the
output value?
A. 0
B. 8
C. 10
D. 12
2006 CIT#32
51. Study the following flowchart.
If six numbers, 1, 4, 3, 2, 8 and -1 are to be subsequently
input, what will be the final value of S?
A. 5
B. 13
C. 15
D. 18
2007 CIT#28
52. What is the purpose of the above flowchart?
A.
B.
C.
D.
Count the numbers between 1 and X
Find (1+2+3+…+X).
Find (1+2+3+…+X)/X.
Count the number of input values before -1 is entered.
2008 CIT#30
53. Consider the following flowchart.
Which of the following values
will never be displayed?
A. -4
B. 0
C. 6
D. 8
23
Conventional Questions:
1.
Determine the number of 1’s printed in each of the following programs:
(a)
10
20
30
40
50
60
X0
OUTPUT 1
XX+1
IF X <= 5 THEN
GOTO 20
END IF
No. of 1’s =
2.
10
20
30
40
50
60
X1
OUTPUT 1
XX+1
IF X < 5 THEN
GOTO 20
END IF
No. of 1’s =
FOR i = 2 to 8
XX+3
i = i +1
OUTPUT X
Output =
20
30
40
50
(c)
10
20
30
40
50
60
X1
IF X < 5 THEN
XX+1
OUTPUT 1
GOTO 20
END IF
No. of 1’s =
Determine the output of the following programs:
(a)
(b)
10 X  2
10 X  2
20
30
40
50
3.
(b)
REPEAT
XX+1
UNTIL X > 20
OUTPUT X
Output =
(c)
10
X2
20
30
40
50
WHILE X < 20
XX+3
END WHILE
OUTPUT X
Output =
The following program is designed to find the sum of 12 + 22 + 32 + … + 102 .
Program Flowchart:
S0
__________________________
____________________________
____________________________
OUTPUT S
4.
(a) The following program is for validation of two input, x and y.
Program Flowchart:
_____________
INPUT x
INPUT y
____________________________
(b) Should the programmer use Repeat-Loop or While-Loop? Why?
24
5.
The following program is for adding all positive number inputted by the user. When the user
finished inputting and want to calculate the result, he/she can input –1 to terminate the
program.
SUM  0
_________ ____________________ ______
INPUT x
SUM = _______________________
______ _________________
6.
Consider the following program segments:
(a)
(b)
10 X  10
10 X  10
(c)
10
S  10
20
30
40
S  100
IF X <2 THEN
S  S – 10
20
30
40
S  100
IF 2*X < 15 THEN
S  S / 10
20
30
40
SS/2
IF S/2 > 2.5 THEN
SS/2
50
60
70
ELSE
S  S + 10
END IF
50
60
70
ELSE
S  S * 10
END IF
50
60
70
ELSE
SS*2
END IF
S=
7.
Program Flowchart:
S=
S=
The following is a program to accept two inputs from the user and will arrange them into
ascending order. Fill in the following blanks in pseudocode to finish the program segment.
INPUT X
Program Flowchart:
INPUT Y
_______________________________
_______________________
_______________________
_______________________
______________
OUTPUT X
OUTPUT Y
8.
The following is a program to accept an input from the user and return the absolute value of
the input to the user. Fill in the following blanks in pseudocode to finish the program segment.
INPUT X
Program Flowchart:
___________________________
_______________________
________________
OUTPUT X
25
9.
Consider the following program segment.
10
S0
20
30
40
50
60
70
80
Program Flowchart:
X0
XX+1
SS+X
IF X < 10 THEN
GOTO 30
END IF
OUTPUT S
(a) What is the purpose of this program?
(b) What is the output of this program?
(c) If Line 50 is changed to “IF NOT(X >= 10) THEN”, will it have any difference on the
whole program segment? Explain.
(d) Suppose we do not want to use the above Branching Statements to finish the above task
and would like to use Looping, fill in the following program segment to write the same
program segment using WHILE-looping.
S0
X0
_____________________________
_____________________________
_____________________________
_____________________________
_____________________________
OUTPUT S
10. The following is a program segment written in pseudocode.
x4
Program Flowchart:
REPEAT
xx-3
UNTIL x < -20
OUTPUT x
(a) What is the output of the program?
(b) How many times will the statement inside the REPEAT…UNTIL loop run?
(c) What is the characteristics of REPEAT…UNTIL loop compared to other looping method?
26
11. (a) Give an examples for each of the following errors
(i) syntax error
(ii) runtime error
(iii) logical error
(b) Explain why a programmer should test a program with both
(i) valid data, and
(ii) invalid data
12. What are the outputs of the following flowchart?
X = ____________________________
Y = ____________________________
13. Consider the following programming flowchart:
State the output from the program if the inputs are:
(a) 10, 20, 60, 10, 20
(b) 120, 130, -2, 200
27
2006 CIT#MC34 (Modified)
14. The following algorithm is used to calculate the average mark of students with a sequence of
input mark until a negative value to end the input process. Study the following algorithm and
answer the following questions.
10
INITIALIZE the counter to zero
20
30
40
50
60
70
80
90
DO WHILE
ACCUMULATE total
INCREMENT the counter
READ a mark from the input
END DO LOOP
CALCULATE the average by dividing total by the counter
a)
b)
Please fill in the blanks in the above algorithm to complete the program.
State the situation that this algorithm design will create run-time error.
c)
The above algorithm is done by using Pseudocode, other than this approach what
else technique can be used to design an algorithm?
d)
After the algorithm design stage finished, programmer can starting doing the
developing a solution by writing a program. Below shows some program codes:
i = 0;
…
while (x >= 0) {
t = t + x;
…
}
ave = t / i;
Give two suggestions to the above program.
28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
C
B
D
B
C
A
B
C
C
A
C
C
D
D
B
A
A
A
C
B
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
B
C
A
D
C
C
A
A
C
C
C
B
B
C
B
D
C
D
D
D
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
B
C
A
A
B
C
B
B
D
D
A
D
D
1.
(a) 6
(b) 4
(c) 4
2.
(a) 23
(b) 21
(c) 20
3.
S0
For i = 1 to 10 do
SS+i*i
OUTPUT S
4.
(a) Repeat
INPUT X
INPUT Y
Until (X > 0) AND (Y > 0)
(b) Repeat loop. Because the input procedure should be executed at least once. Statements
inside a Repeat-Loop are executed at least once, which satisfy the condition.
5.
SUM  0
While x <> -1 do
INPUT x
SUM = SUM + x
End while
6.
(a) S = 110
7.
INPUT X
(b) S = 1000
INPUT Y
If X > Y then
ZX
XY
29
(c) S = 10
YZ
End If
OUTPUT X
OUTPUT Y
8.
INPUT X
If X < 0 then
X  -1 * X
End If
OUTPUT X
9.
(a) 1 + 2 + 3 + … + 10 / Calculate the consecutive numbers from 1 to 10.
(b) 55
(c) No. Logically, NOT (X >= 10) is equivalent to X < 10.
(d) S  0
X0
While X < 10 do
XX+1
SS+X
End while
OUTPUT S
10. (a) -23
(b) 9
(c) Statements inside repeat-loop are executed at least once.
11. (a) (i)
(ii)
(iii)
(b) (i)
(ii)
5x
division by zero / out of memory
the average mark is greater than 100
to make sure that it would give expected result
to make sure that it would trap (找出) the error
12. x = 5, y = 1
13. (a) 110
(b) 148
14. Refer to the hidden text.
30