introduction to computers -3rd exam- 授課教授:李錫智. q1 what will the web page look like...

25
Introduction to Computers -3rd exam- 授授授授 授授授

Upload: helena-gilmore

Post on 26-Dec-2015

238 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Computers -3rd exam- 授課教授:李錫智. Q1 What will the web page look like if the user type 100 、 20 in the numberbox1 、 numberbox2 respectively?

Introduction to Computers-3rd exam-授課教授:李錫智

Page 2: Introduction to Computers -3rd exam- 授課教授:李錫智. Q1 What will the web page look like if the user type 100 、 20 in the numberbox1 、 numberbox2 respectively?

Q1

• What will the web page look like if the user type 100、20 in the numberbox1、numberbox2 respectively?

<html> <head> <script type="text/javascript"> function Classify(){ num1 = document.getElementById('numberbox1').value; num2 = document.getElementById('numberbox2').value; if (num1 > num2){

document.getElementById('outputDiv').innerHTML="num1 is bigger"; } else if (num1 < num2){

document.getElementById('outputDiv').innerHTML="num2 is bigger"; } else if (num1 == num2){

document.getElementById('outputDiv').innerHTML="equal"; } } </script> </head> <body> num1:<input type="text" id="numberbox1" size=6 value=""></br> nmu2:<input type="text" id="numberbox2" size=6 value=""></br> <input type="button" value="To compare" onclick="Classify();"/></br> <div id="outputDiv"></div></br> </body></html> 

Page 3: Introduction to Computers -3rd exam- 授課教授:李錫智. Q1 What will the web page look like if the user type 100 、 20 in the numberbox1 、 numberbox2 respectively?

Solution of Q1

• Answer : num2 is bigger

• The value of numberboxs are strings so that the IF condition would compare with each first character.

• When we type 100 and 20, the condition would compare ‘1’ with ‘2’. In ASCII codes, the value of ‘2’ is bigger than ‘1’, and the result will show “num2 is bigger”.

Page 4: Introduction to Computers -3rd exam- 授課教授:李錫智. Q1 What will the web page look like if the user type 100 、 20 in the numberbox1 、 numberbox2 respectively?

Q2

• Please write a program to show a student’s final score.

• The web page should contain three boxes: mid-exam score box, final-exam score box, and final score box.

• When the student clicks the button after he/she types the mid-exam score and final-exam score, the web page will show his/her final score in the final score box. If the final score is lower than 60, the web page will alert “This subject may not pass.”

• (Final score = mid exam score*40% + final exam score*60%)

Page 5: Introduction to Computers -3rd exam- 授課教授:李錫智. Q1 What will the web page look like if the user type 100 、 20 in the numberbox1 、 numberbox2 respectively?

Solution of Q2

ZenLong He
<html><head><script type="text/javascript">function score(){mid = parseFloat(document.getElementById('box1').value);final = parseFloat(document.getElementById('box2').value);score = mid*0.4+final*0.6;document.getElementById('box3').value = score;if (score < 60){ alert("This subject may not pass.");}}</script></head><body>mid exam score:<input type="text" id="box1" size=6 value=""></br>final exam score:<input type="text" id="box2" size=6 value=""></br><input type="button" value="To estimate your score" onclick="score();"/></br>final score:<input type="text" id="box3" size=6 value=""></br></body></html>
Page 6: Introduction to Computers -3rd exam- 授課教授:李錫智. Q1 What will the web page look like if the user type 100 、 20 in the numberbox1 、 numberbox2 respectively?

Q3

• A black-and-white bitmap is a way to store an image without colors.

• In such an image, each pixel has only two values, white and black.

• Therefore, one bit is enough to represent a pixel, with 1 indicating white and 0 indicating black.

• Suppose we have a 8×10 black-and-white image represented by a bitmap of 8×10 bits. The content of this bitmap is represented by {8, 62, 28, 42, 93, 4, 8, 62, 8, 24}

• in decimal from the top row to the bottom row of the image. Please draw the image.

Page 7: Introduction to Computers -3rd exam- 授課教授:李錫智. Q1 What will the web page look like if the user type 100 、 20 in the numberbox1 、 numberbox2 respectively?

Solution of Q3

• 00001000

• 00111110

• 00011100

• 00101010

• 01011101

• 00000100

• 00001000

• 00111110

• 00001000

• 00011000

Page 8: Introduction to Computers -3rd exam- 授課教授:李錫智. Q1 What will the web page look like if the user type 100 、 20 in the numberbox1 、 numberbox2 respectively?

Q4

• The characters of the ASCII codes are less than 27.In order to make each code a byte, a bit is added in front for checking certain transmission errors.

• Please describe what errors are to be detected and how to detect them?

Page 9: Introduction to Computers -3rd exam- 授課教授:李錫智. Q1 What will the web page look like if the user type 100 、 20 in the numberbox1 、 numberbox2 respectively?

Solution of Q4

• Answer :

• The additional bit is that we call “parity bit” or “check bit”. We can detect the one bit may be changed unexpectedly in the transmission.

• The parity bit for each unit is set so that all bytes have either an odd number or an even number of set bits.

• In the case of even parity, the number of bits whose value is 1 in a given set are counted. If that total is odd, the parity bit value is set to 1 . If the count of ones in a given set of bits is already even, the parity bit's value remains 0. When we receive the byte, the count of ones is odd, we can detect the byte must be wrong.

Page 10: Introduction to Computers -3rd exam- 授課教授:李錫智. Q1 What will the web page look like if the user type 100 、 20 in the numberbox1 、 numberbox2 respectively?

Q5-(a)

• What is the output produced by each of the following programs?

var x = 1;

var y = 30;

while (x < y)

{

document.write(x + " " + y + "<br />");

x =2* x + 1;

y = y – 2;

}

document.write("DONE WITH SECOND!");

Page 11: Introduction to Computers -3rd exam- 授課教授:李錫智. Q1 What will the web page look like if the user type 100 、 20 in the numberbox1 、 numberbox2 respectively?

Solution of Q5-a

• Answer :

1 30

3 28

7 26

15 24

DONE WITH SECOND!

Page 12: Introduction to Computers -3rd exam- 授課教授:李錫智. Q1 What will the web page look like if the user type 100 、 20 in the numberbox1 、 numberbox2 respectively?

Q5-(b)

var x = 5;var y = 5;var z = 10;while (x <= y){

document.write(x + " " + y + "<br />");y = y + 5;while ((x=z) == y){

z = z * 2;y = y + 10;document.write(x + " " + y + "<br />")

}x = x * 4;

}document.write("Finish!");

Page 13: Introduction to Computers -3rd exam- 授課教授:李錫智. Q1 What will the web page look like if the user type 100 、 20 in the numberbox1 、 numberbox2 respectively?

Solution of Q5-b

• Answer :

5 5

10 20

20 30

Finish!

Page 14: Introduction to Computers -3rd exam- 授課教授:李錫智. Q1 What will the web page look like if the user type 100 、 20 in the numberbox1 、 numberbox2 respectively?

Q6

• Please answer the following questions:• (a)Convert the following double-precision bit sequence

to a real value:0100000000011110100000000000000000000000000000000000000000000000• (b)Convert the following real value to a double-precision

bit sequence:-16.5 = ?

Page 15: Introduction to Computers -3rd exam- 授課教授:李錫智. Q1 What will the web page look like if the user type 100 、 20 in the numberbox1 、 numberbox2 respectively?

Solution of Q7-a

• Answer : (1.11101)*22 7.625

• Sign : 0 Positive

• Exponent : (10000000001)2 – 1023 = 2

• Fractional part : 1.11101

• (1.11101)*22 = 111.101

• Integer : 111 7

• Floating : 0.101 0.625

Page 16: Introduction to Computers -3rd exam- 授課教授:李錫智. Q1 What will the web page look like if the user type 100 、 20 in the numberbox1 、 numberbox2 respectively?

Solution of Q7-b

• Answer :11000000 00110000 10000000 00000000 00000000 00000000 00000000 00000000

• Sign bit : Negative 1

• Integer : 16 (10000)2

• Floating : 0.5 (0.1)2

• 1.00001 offset 4bits

• Fractional part : 1.00001

• Exponent : 1023+4 = 1027 10000000011

Page 17: Introduction to Computers -3rd exam- 授課教授:李錫智. Q1 What will the web page look like if the user type 100 、 20 in the numberbox1 、 numberbox2 respectively?

Q7

• Suppose we have the following machine instructions for a simulated computer:

• You want to write a program to calculate 3x-2y where the x value is stored in the location of memory address 20, the y value is stored in the location of memory address 21, and the result is going to be stored in the location of memory address 22. Please show your program.

Page 18: Introduction to Computers -3rd exam- 授課教授:李錫智. Q1 What will the web page look like if the user type 100 、 20 in the numberbox1 、 numberbox2 respectively?

Solution of Q7

• LOAD R0 20100000010 00 10100

• LOAD R1 21100000010 01 10101

• ADD R2 R0 R01010000100 10 00 00

• ADD R2 R2 R01010000100 10 10 00

• SUB R2 R2 R11010001000 10 10 01

• SUB R2 R2 R11010001000 10 10 01

• STORE 22 R2100000100 10110 10

• HALT1111111111111111

Page 19: Introduction to Computers -3rd exam- 授課教授:李錫智. Q1 What will the web page look like if the user type 100 、 20 in the numberbox1 、 numberbox2 respectively?

Q8-1

• Please answer the following questions:

• (a)What would happen if you place the number 19 in R0 and the number 21 in R1, and set the knobs so that A bus = R0, B Bus = R1, ALU = A + B, and C Bus = R2?

• (b)Describe the settings that would cause the value stored in R0 to be cleared, i.e., the content of R0 would be zero, after execution.

Page 20: Introduction to Computers -3rd exam- 授課教授:李錫智. Q1 What will the web page look like if the user type 100 、 20 in the numberbox1 、 numberbox2 respectively?

Solution of Q8-1

• (a)Answer :

• (b)Answer : A bus = R0,

B Bus = R0,

ALU = A - B,

C Bus = R0

R0 19

R1 21

R2 40

Page 21: Introduction to Computers -3rd exam- 授課教授:李錫智. Q1 What will the web page look like if the user type 100 、 20 in the numberbox1 、 numberbox2 respectively?

Q8-2

• (c)What task would the following machine-language program perform?• 1000000100001010• 1010000100000000• 1010000100000000• 1000001000101000• 1111111111111111

• (d) Write a sequence of machine-language instructions that would add the contents of memory locations 12 and 13 then store the result in memory location 14.

Page 22: Introduction to Computers -3rd exam- 授課教授:李錫智. Q1 What will the web page look like if the user type 100 、 20 in the numberbox1 、 numberbox2 respectively?

Solution of Q8-2

• (c)Answer :• 100000010 00 01010 load memory 10 into R0• 1010000100 00 00 00 add R0 and R0, store result in R0• 1010000100 00 00 00 add R0 and R0, store result in R0• 100000100 01010 00 store R0 in memory 10 • 1111111111111111 halt

• (d)Answer :• 100000010 00 01100 load memory 12 into R0• 100000010 01 01101 load memory 13 into R1• 1010000100 10 00 01 add R0 and R1, store result in R2• 100000100 01110 10 store R2 in memory 14 • 1111111111111111 halt

Page 23: Introduction to Computers -3rd exam- 授課教授:李錫智. Q1 What will the web page look like if the user type 100 、 20 in the numberbox1 、 numberbox2 respectively?

Q9

• Please just modify the function that counting the loops when the doubles are obtained and show the times of loops in next line.

<html><head>

<title> Test 4 </title><script type="text/javascript"

src="http://dave-reed.com/book/random.js">

</script><script type="text/javascript">function twodice(){

var d1,d2,count;d1=-1;d2=-2;while(d1 != d2)

{ d1=RandomInt(1,6); d2=RandomInt(1,6);

document.getElementById("OutputBox").value=

document.getElementById("OutputBox").value+"Dice1:"+ d1+"; Dice2:"+d2+"\n";

}} </script> </head><body> <div style="text-align:center"> <input type="button" value="Click for starting the program"

onclick="twodice();"> <br/><hr/><br/> <textarea id="OutputBox" rows="20" clos="20"></textarea></div></body>

</html>

Page 24: Introduction to Computers -3rd exam- 授課教授:李錫智. Q1 What will the web page look like if the user type 100 、 20 in the numberbox1 、 numberbox2 respectively?

Solution of Q9

function twodice()

{

var d1,d2,count;

d1=-1;

d2=-2;

count=0;

while(d1 != d2)

{

count=count+1;

d1=RandomInt(1,6);

d2=RandomInt(1,6);

document.getElementById("OutputBox").value =

document.getElementById("OutputBox").value +

"Dice1:"+d1+"; Dice2:"+d2+"\n";

}

document.getElementById("OutputBox").value =

document.getElementById("OutputBox").value +

"Run "+ count + " rounds";

}

註:題目原意為將內容與結果一起 print在 OutputBox中若在 function的最後一行使用 document.write,會覆蓋function原本要輸出的畫面,只剩下 document.write 的訊息。

Page 25: Introduction to Computers -3rd exam- 授課教授:李錫智. Q1 What will the web page look like if the user type 100 、 20 in the numberbox1 、 numberbox2 respectively?

The end of the 3rd Exam