computer network - yll.edu.hk · web viewthere are several data type in programs. they include:...
TRANSCRIPT
![Page 1: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/1.jpg)
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;
Y = 20;
Y = 2X;
2X = Y 3;
Syntax error / logical error
How to fix?
document.output(“Who are you?”); Syntax error / logical error
How to fix?
1
![Page 2: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/2.jpg)
window.ResizeTo(100,200); Syntax error / logical error
How to fix?
for (i=0,i<10,i++) {
alert(“hello!”);
}
Syntax error / logical error
How to fix?
for (i=0;i>10;i++) {
alert(“hello!”);
}
Syntax error / logical error
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++) {
alert(“hello!”);
}
0010001010111000001010100
1010100010000100111111011
1000011000010101010111110
1111111101010001011110...
2
![Page 3: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/3.jpg)
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 program terminates abnormally
- eg: division by zero, out of memory
3. Error
- it is mainly caused by an incorrect
- it produces an result
- it be detected by the translator / compiler
- eg: the calculated average mark of a test is greater than 100
Pseudocode Sample 1:
SET total to zero
REPEAT
READ Temperature
IF Temperature > Freezing THEN
INCREMENT total
END IF
UNTIL Temperature < zero
Description of the program:
3
![Page 4: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/4.jpg)
Print total
Pseudocode Sample 2:
Input a number N
X <- 1
WHILE (X < N) AND (N < 5)
X <- X + 2
END
Output X, N
State the output of the program with input as
(i) 4
(ii) 14
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:
4
![Page 5: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/5.jpg)
function timeout1() {var x = 10;for (i=1;i<x;i++) { alert(i);}}
function timeout2() {var Limit = 10;for (counter=1;counter<Limit;counter++) { // a FOR LOOP to popup a dialogue box.
alert(i);}
}
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:
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?
Data type Example DescriptionReal 2.534, 0, -28 Decimal numbersString ‘abc’, ‘2.354’, ‘28abc’ Alphanumeric or simply numericInteger 15, 27, 91, -11 Integer Boolean / Logical TRUE, FALSE Logical data
5
Comment
![Page 6: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/6.jpg)
Exercise:
1. 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.
2. 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.
6
![Page 7: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/7.jpg)
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.
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 symbolsBegin / End
Process
Input / Output
Decision
Pre-defined Process
Decision box: It has two common features. One is Branching and the other one is Looping.
Branching / IF / Conditional Statement Looping
7
START END
New <- New + 1
Input X, Y
Output X
Y = X ?
Yes
No
Find the remainder (R) of Y divided by X
Yes
![Page 8: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/8.jpg)
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.
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:
8
START
END
Find the remainder (R) of X divided by 2
?Yes
No
Y = X ?
No
Y = X ?
Yes
No
![Page 9: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/9.jpg)
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 ( ) {
…….}
Finish the following flowchart so that it can let users input a number, then it will output the maximum
divisor.
9
X=2
Yes
No
START
Find the remainder (R) of Y divided by X
END
![Page 10: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/10.jpg)
Exercise 3: Create a flowchart so that it can find the H.C.F. of two inputted numbers.
10
START
Input X, Y
END
![Page 11: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/11.jpg)
In JavaScript, we can find the H.C.F. in the following manner:
<script language= "JavaScript">function findHCF() {var data1 = parseInt(num1.value); //parseInt() is a JavaScript functionvar data2 = parseInt(num2.value); //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);
}
WHILE – Loop
11
Yes
No
Comment
![Page 12: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/12.jpg)
i = 0;x = “Numbers include ”;while (i<5) { x = x + ", " + i; i++;}x = x + “ and “ + i + “.”document.write(x);
For the following two flowcharts, dry run them with the input 3 and 5 respectively.
(i) (ii)
X = 3 Output =
No. of iteration =
Output =
No. of iteration =
X = 5 Output = Output =
ProcessYes
No
Input X
S = 1N = 2
S = S + N
N = N + 1
N < X?
Output N
Input X
S = 1N = 2
S = S + NN = N + 1
N < X?
Output N
YES
YES
12
Yes
No
?
![Page 13: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/13.jpg)
No. of iteration = No. of iteration =
Description of the flowchart:
REPEAT UNTIL Loop
With respect to the diagram on the right,
(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
summation of all the multiplications of 3 between 0 and 100?
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.
Process
No
Yes
?
13
I = 1S = 0
S = S + I
I > 10?
I = I + 3No
1
2
3
4
What is the different between a WHILE LOOP and a REPEAT UNTIL LOOP?
![Page 14: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/14.jpg)
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) for (i=1;i<=100;i++) ?
What is the value of i in (i) and (ii) respectively?
3. Multiplication from 1 to 10.Investigate what is wrong in the codes below:
result = 0;
i = 1;
While (i<10) {
Result = result * i;
i = i + 1;
}
Modification :
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 arraylimit = number;
14
![Page 15: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/15.jpg)
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>");}
}</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) {
if (data1 < 0) { data1 = -data1}if (data2 < 0) { data2 = -data2}
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 == false) {
HCF = 1}return HCF;}
function findLCM(x,y) {value1 = parseInt(x.value);value2 = parseInt(y.value);
LCM = 1;
HCF = findHCF(value1, value2);if (HCF != 1) {
LCM = LCM * HCF;value1 = value1 / HCF;value2 = value2 / HCF;
} LCM = value1 * value2 * HCF;return LCM;
}
function showLCM(In1, In2) {lcm = findLCM(In1, In2);alert (lcm);
}function DoFraction(N1, D1, N2, D2, operation) {
LCM = findLCM(D1, D2);
Numerator1 = parseInt(N1.value);Numerator2 = parseInt(N2.value);Denominator1 = parseInt(D1.value);Denominator2 = parseInt(D2.value);
Denominator = LCM;if (operation == '+') {
Numerator = Numerator1*LCM/Denominator1 + Numerator2*LCM/Denominator2;
15
![Page 16: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/16.jpg)
} else {Numerator = Numerator1*LCM/Denominator1 -
Numerator2*LCM/Denominator2;}
Fraction31.value = Numerator;Fraction32.value = Denominator;
HCF = findHCF(Numerator, Denominator);Fraction41.value = Numerator / HCF;Fraction42.value = Denominator / HCF;
}
<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>
16
![Page 17: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/17.jpg)
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.
17
![Page 18: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/18.jpg)
Exercise:
1. Which of the following statements consist of
syntax errors?
(1) x + 1 4
(2) x 5 + 8 * 2
(3) x "5" + 5
A. (1) only B. (3) only
C. (1) and (3) only D. (2) and (3) only
2. Which of the following will give a true result?
A. (x > x + 5) AND (1 > 2)
B. (3 > -7) OR (-7 > 3)
C. (5 >= 5) AND (4 >= 5)
D. (7+3 < 8) OR (-7 > -6)
3. Consider the following program:INPUT xIF x > 5 THEN
X 5END IF
The purpose of the program is to
A. allow 5 to be entered only.
B. allow numbers less than or equal to 5 to
be entered only.
C. ensure that the value of X is greater than
5.
D. ensure that the value of X is less than or
equal to 5.
4. How many times will the following WHILE-loop
iterate?i 6WHILE i <10 DO
…i i + 1
END WHILEA. 0 B. 4
C. 5 D. 6
5. How many times will the following REPEAT…
UNTIL-loop iterate?i 6REPEAT
…
i i + 1UNTIL i > 10
A. 0 B. 4
C. 5 D. 6
6. Which line of the following pseudocode
segment can be deleted without affecting the
result?
Line number Statement
10 C = 60
20 E = 70
30 M = 80
40 T = 95
50 M = M – T
60 INPUT C
70 C = C + E
80 OUTPUT (C + E)
90 OUTPUT M
A. Line 10 B. Line 20
C. Line 30 D. Line 40
7. An algorithm (算法) is
A. a program
B. a set of steps used to solve a problem
C. a description of how the problem has
been solved
D. a clear diagram showing the existing
problem
8. In a report card, the average mark of a student
is outputted as -20. What kind of error is it?
A. syntax error B. run-time error
C. logical error D. printing error
9. Which of the following is/are advantage(s) of
compiler over interpreter?
(1) Compiled programs run faster
(2) Source codes need not be present during
execution.
18
![Page 19: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/19.jpg)
(3) Compiled programs are easier to develop.
A. (1) only B. (2) only
C. (1) and (2) only D. (2) and (3) only
10. Object programs are free of
(1) syntax errors (2) runtime errors
(3) logical errors
A. (1) only B. (2) only
C. (1) and (2) only D. (1), (2) and (3)
11. Which of the following would lead to a logical
error?
(1) A “>” sign is mistyped as “<” in a
conditional statement
(2) The assignment statement x x + 1 is
mistyped as x x + 2
(3) The reserved (保留字) word “if” is
mistyped as “fi”.
A. (1) only B. (2) only
C. (1) and (2) only D. (2) and (3) only
12. Which of the following is/are high level
language(s)?
(1) JAVA (2) PASCAL
(3) machine language
A. (1) only B. (2) only
C. (1) and (2) only D. (2) and (3) only
13. Which of the following is machine
independent?
A. assembler
B. assembly language
C. machine language
D. C source programs
14. Consider the following program:
10 P 1
20 FOR i =6 to 8 DO
30 P P * i
What is the value of P just before the
execution finishes?
A. 21 B. 42
C. 48 D. 336
15. Debugging is to
A. find out any hidden errors
B. make corrections to the program so that it
is error free
C. kill any bugs or bacteria
D. kill a virus using an anti-virus software
16. Documentation
A. describe what has been done and how the
problem is solved
B. use a word processing software to
produce a document
C. write a letter to the clients telling them the
problem is solved
D. re-write the program t make it more
versatile.
17. If we are going to add up all the even numbers
from 1 to 100, which of the following
constructs is most likely to be adopted?
A. Looping B. Branching
C. Sequencing D. Sorting
18. What does a compiler do?
A. A compiler translates a program written in
a high-level language into machine-
executable instruction.
B. A compiler debugs errors found in a
program written in a high-level language.
C. A compiler translates high-level language
into an intermediate form, which is then
executes.
D. A compiler makes a program written in a
high-level language run on a computer.
19. Which of the following is the initial step in
19
![Page 20: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/20.jpg)
problem solving?
A. Designing an Algorithm
B. Problem Analysis
C. Problem Identification
D. Documentation
20. Which of the following is/are advantage(s) of
breaking a problem into smaller tasks?
(1) Programs are easier to develop.
(2) Programs are easier to debug.
(3) Programs are faster.
A. (1) only B. (1) and (2) only
C. (2) and (3) only D. (1), (2) and (3)
21. An algorithm is
A. a computer program.
B. a set of ordered steps used to solve a
problem.
C. a definition of problem.
D. a graphical representation of the steps.
22. Pseudocodes may consist of
(1) assignment statements
(2) conditional statements
(3) variable type definition
A. (1) only B. (2) only
C. (1) and (2) only D. (2) and (3) only
23. Which of the following errors are usually
discovered the earliest in program
development?
A. syntax errors B. logical errors
C. runtime errors D. data errors
24. Which of the following errors are usually
discovered by using test data?
(1) syntax errors (2) logical errors
(3) runtime errors
A. (1) only B. (2) only
C. (1) and (2) only D. (2) and (3) only
25. Which of the following belong(s) to runtime
errors?
(1) division by zero (2) out of memory
(3) using a reserved word as variable name
A. (1) only B. (2) only
C. (1) and (2) only D. (2) and (3) only
26. Which of the following would lead to a logical
error?
(1) A ">" sign is mistyped as "<" in a
conditional statement.
(2) The assignment statement x <- x + 1 is
mistyped as x<- x+2.
(3) The reserved word "if' is mistyped as "fi".
A. (1) only B. (2) only
C. (1) and (2) only D. (2) and (3) only
27. If there is no testing, a computer program may
contain
(1) bugs. (2) viruses.
(3) worms.
A. (1) only B. (1) and (2) only
C. (1) and (3) only D. (2) and (3) only
28. Debugging means
A. correcting mistakes in a program.
B. writing comments for a program.
C. removing small insects from a computer.
D. converting an algorithm into a program
flowchart.
Questions 29 to 31:
Consider the following algorithm:
step 1 input x
step 2 if x > 0 then
step 2.1 increase x by 1
else
step 2.2 decrease x by 1
20
![Page 21: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/21.jpg)
29. Which of the following can replace Step 2.1?
A. x <- 1 B. x <- x - 1
C. x <- x + 1 D. x <- x + x
30. Step 2 represents
A. a for loop
B. a while loop
C. a conditional statement
D. an expression
31. If the input data is -5, what will be the value of
x?
A. -4 B. -5
C. -6 D. 5
32. Y = 5 * x - 5
Given that X is a number between 0 and 1
inclusively. What is the range of Y?
A. between 0 and 1 inclusively
B. between -5 and 0 inclusively
C. between 0 and 5 inclusively
D. between 5 and 10 inclusively
33. Given that P = 6. What is the value of P after
line 20?
10 P <- P + 1
20 P <- P - 2
A. 4 B. 5
C. 6 D. 7
34. Given that P = 3 and Q = 5. What are the
values of P and Q after line 20?
10 P <- P + Q
20 P <- P - QP Q
A. 8 0 B. -2 5C. 3 5 D. 3 0
35. Given that P = 3 and Q = 5. What are the
values of P and Q after line 20?
10 P <- P * Q
20 P <- P/QP Q
A. 15 1 B. 3 5C. 3 1 D. 5 3
36. Given that P = 1.
10 P <- P * Q
20 P <- P * Q
30 P <- P * Q
What would be the result stored in P after line
30?
A. the value of Q B. the square of P
C. the square of Q D. the cube of Q
37. Consider the following program:
10 INPUT X
20 IF X >= 0 THEN Y <- 1
30 IF X < 0 THEN Y <- -1
Which of the following statements is true?
A. Y is always a positive number.
B. Y is the absolute value of X.
C. The product of Y and X is always a
positive number.
D. The product of Y and X is always a
negative number.
38. Consider the following program:
10 INPUT P, Q
20 IF 2*P - Q > 0 THEN
30 OUTPUT P
40 ELSE
50 OUTPUT Q
60 ENDIF
If the input data are 5 and 10, the output will
be
A. P B. Q
C. 5 D. 10
21
![Page 22: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/22.jpg)
39. The following program finds the reciprocal of
an input number:
100 INPUT X
200 OUTPUT 1/X
Which of the following lines should be added
to avoid division by zero error?
A. 50 IF X < 0 THEN GOTO 100
B. 150 IF X < 0 THEN GOTO 100
C. 150 IF X <= 0 THEN GOTO 100
D. 150 IF X = 0 THEN GOTO 100
40. Given that P = 1 and Q = 2. Which of the
following logical expressions is TRUE?
A. (P>1) AND (Q>2)
B. (P > 1) OR (Q > 2)
C. (P >= 1) AND (Q > 2)
D. (P >= 1) OR (Q > 2)
41. Given that p and q are boolean variables
storing TRUE and FALSE respectively. Which
of the following expressions is FALSE?
A. p OR q
B. (NOT p) AND q
C. p OR (NOT q)
D. (NOT p) OR (NOT q)
42. Which of the following logical expressions is
TRUE?
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))
D. (6 - 1 < 2) AND (NOT (1 > 2) OR (3 - 5 >
4) )
2006 CIT#28
43. Which of the following characteristics of high
level language can reflect the concept of
program modules?
A. Subprogram B. Comment
C. Condition D. Looping
2005 CIT#25
44. Which line in the following pseudo-code
segment can he deleted without affecting the
result?
Line number Content
10 A = 30
20 B = 40
30 C = 50
40 D = 60
50 C = C – D
60 Input A
70 A = A + B
80 Output (A + B)
90 Output C
A. Line 10 B. Line 20
C. Line 30 D. Line 40
45. Consider the following program flowchart:The value of X and Y just before the end of execution are
X YA. 3 5B. 3 2C. 2 5D. 2 3
22
START
X 3Y 5
X>Y?
Y Y - X X X - Y
END
no yes
![Page 23: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/23.jpg)
46. Consider the following program flowchart:The purpose of the flowchart is toA. find the sign of an input number.B. find the product of two numbers.C. find the absolute value of an input number.D. reverse the sign of an input number.
47. Consider the following program flowchart:
The purpose of the flowchart is to make sure that the input number isA. greater than zero.B. less than zero.C. less than or equal to zero.D. not equal to zero
48. Consider the following program flowchart:
The output will be A. 2B. 3 C. 5 D. -22
49. Consider the following program flowchart:The values of S and N just before the end ofexecution are
S NA. 0 0B. 42 12
23
START
Input X
X > 0?
Y –1 Y 1
X Y * X
END
yesno
START
Input X
X > 0?
X = 0?
END
yes
yesno
noSTART
P 27Q 5
P >Q?
P P – Q
Output Q – P
END
Yes
No
START
S 0N 0
S < 50?
N N+2S S+N
END
YesNo
![Page 24: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/24.jpg)
C. 50 12D. 56 14
2005 CIT#2750. Study the following flowchart.
If the input value is 0, what is the output value?A. 0B. 8C. 10D. 12
2006 CIT#3251. Study the following flowchart.
If six numbers, 1, 4, 3, 2, 8 and -1 are to be subsequentlyinput, what will be the final value of S?A. 5B. 13C. 15D. 18
200 7 CIT# 28 52. What is the purpose of the above flowchart?
A. Count the numbers between 1 and XB. Find (1+2+3+…+X).C. Find (1+2+3+…+X)/X.D. Count the number of input values before -1 is entered.
200 8 CIT# 30 53. Consider the following flowchart.
Which of the following values will never be displayed?A. -4 B. 0C. 6 D. 8
24
![Page 25: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/25.jpg)
Conventional Questions:
1. Determine the number of 1’s printed in each of the following programs:(a) (b) (c)
10 X 0 10 X 1 10 X 120 OUTPUT 1 20 OUTPUT 1 20 IF X < 5 THEN30 X X + 1 30 X X + 1 30 X X + 140 IF X <= 5 THEN 40 IF X < 5 THEN 40 OUTPUT 150 GOTO 20 50 GOTO 20 50 GOTO 2060 END IF 60 END IF 60 END IF
No. of 1’s = No. of 1’s = No. of 1’s =
2. Determine the output of the following programs:(a) (b) (c)
10 X 2 10 X 2 10 X 220 FOR i = 2 to 8 20 REPEAT 20 WHILE X < 2030 X X + 3 30 X X + 1 30 X X + 340 i = i +1 40 UNTIL X > 20 40 END WHILE50 OUTPUT X 50 OUTPUT X 50 OUTPUT X
Output = Output = Output =
3. The following program is designed to find the sum of 12 + 22 + 32 + … + 102 .
S 0__________________________
________________________________________________________
OUTPUT S
4. (a) The following program is for validation of two input, x and y.
_____________INPUT xINPUT y
____________________________
(b) Should the programmer use Repeat-Loop or While-Loop? Why?
25
Program Flowchart:
Program Flowchart:
![Page 26: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/26.jpg)
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 xSUM = _______________________
______ _________________
6. Consider the following program segments:(a) (b) (c)
10 X 10 10 X 10 10 S 1020 S 100 20 S 100 20 S S / 230 IF X <2 THEN 30 IF 2*X < 15 THEN 30 IF S/2 > 2.5 THEN40 S S – 10 40 S S / 10 40 S S / 250 ELSE 50 ELSE 50 ELSE60 S S + 10 60 S S * 10 60 S S * 270 END IF 70 END IF 70 END IF
S = S = S =
7. 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 XINPUT Y_______________________________
_____________________________________________________________________
______________OUTPUT XOUTPUT 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___________________________
_______________________
26
Program Flowchart:
Program Flowchart:
Program Flowchart:
![Page 27: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/27.jpg)
________________OUTPUT X
9. Consider the following program segment.10 S 020 X 030 X X + 140 S S + X50 IF X < 10 THEN60 GOTO 3070 END IF80 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 0X 0_________________________________________________________________________________________________________________________________________________OUTPUT S
10. The following is a program segment written in pseudocode.x 4REPEAT
x x - 3UNTIL x < -20OUTPUT 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?
27
Program Flowchart:
Program Flowchart:
![Page 28: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/28.jpg)
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
28
![Page 29: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/29.jpg)
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 zero20
30
40 DO WHILE
50 ACCUMULATE total60 INCREMENT the counter70 READ a mark from the input80 END DO LOOP90 CALCULATE the average by dividing total by the counter
a) Please fill in the blanks in the above algorithm to complete the program.b) 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.
29
![Page 30: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/30.jpg)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20C 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 40B 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 60B 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 0For i = 1 to 10 do
S S + i * iOUTPUT S
4. (a) RepeatINPUT XINPUT 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 0While x <> -1 do
INPUT xSUM = SUM + x
End while
6. (a) S = 110 (b) S = 1000 (c) S = 10
7. INPUT XINPUT YIf X > Y then
Z X
30
![Page 31: Computer Network - yll.edu.hk · Web viewThere are several data type in programs. They include: Data type Example Description Real 2.534, 0, -28 Decimal numbers String ‘abc’,](https://reader034.vdocument.in/reader034/viewer/2022051812/602f5e0061fba245016c16b4/html5/thumbnails/31.jpg)
X YY Z
End IfOUTPUT XOUTPUT Y
8. INPUT XIf X < 0 then
X -1 * XEnd IfOUTPUT 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 0While X < 10 do
X X + 1S S + X
End whileOUTPUT S
10. (a) -23(b) 9(c) Statements inside repeat-loop are executed at least once.
11. (a) (i) 5 x(ii) division by zero / out of memory(iii) the average mark is greater than 100
(b) (i) to make sure that it would give expected result(ii) 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.
31