Download presentation
Presentation is loading. Please wait.
Published byKevin Dean Modified over 9 years ago
1
Introduction to Computers - 3 rd exam- 授課教授:李錫智
2
1.Consider the following program: Magic 8-ball http://dave-reed.com/book/random.js function GetResponse() { var response; if (document.getElementById('inputArea').value == "") { alert(“You have to ask a question first!”); } else { response = RandomOneOf(["yes", "no", "definitely", "highly unlikely“, "maybe", "reply hazy, try again"]); document.getElementById('outputBox').value = response; } }
3
Have a Fun Enter your question in the text area below: The page says:
4
1-(a) What the page looks like without any user’s input? Ans: 1-(b) What happens if the user hits the button without any other input? Ans:
5
1-(c) What happens if the user hits the button after entering “Am I pretty?” in the text area? Ans: The outputBox will show one result of the RandomOneOf function ["yes", "no", "definitely", "highly unlikely","maybe", "reply hazy, try again"]
6
2.Consider the following program: Dot Race <script type= "text/javascript" src=http://dave-reed.com/book/random.js >http://dave-reed.com/book/random.js
7
function DoStep() { var goal, redPos, bluePos; goal = parseFloat(document.getElementById('len').value); redPos = parseFloat(document.getElementById('redDot').value); bluePos = parseFloat(document.getElementById('blueDot').value); if (redPos < goal && bluePos < goal) { redPos = redPos + RandomInt(1,3); bluePos = bluePos + RandomInt(1,3); document.getElementById('redDot').value = redPos; document.getElementById('blueDot').value = bluePos; } if (redPos >= goal && bluePos >= goal) { alert("It's a tie!"); } else if (redPos >= goal) { alert("Red Dot wins!"); } else if (bluePos >= goal) { alert("Blue Dot wins!"); } }
8
Dave's Online Dot Race Race length: Red Dot : : Blue Dot
9
2-(a) What the page looks like without any user’s input? Ans: 2-(b) What happens if the user continues to hit the button? Ans: goal 的預設值為 20 ,以 redPos 為例, redPos 會從 redDot 中 取值,經過運算後再存回 redDot ,因為使用者不斷的 hit the button ,所以可以視為 redPos 一直累加 RandomInt(1,3) 的值,同理 bluePos 也是如此,一直到 redPos 或是 bluePos 大 於等於 goal 為止,如果兩者都大於等於 goal ,則輸出 “It’s a tie!” ,如果只有 redPos ≧ goal ,則輸出 “Red Dot wins!” , 若只有 bluePos ≧ goal ,則輸出 “Blue Dot wins!”
10
3.A grayscale image is one in which each pixel can be white, or black, or various shades of gray in between. On the other hand, a color image is one in which each pixel contains three components, red, green, and blue. Assuming that there are 256 discernable shades of gray. Also, assume that there are 256 different values for red, green, and blue, respectively. Suppose you are given an image of 1024×1024 pixels. (a) How many Mega bits are required for the image if it is a grayscale one? Ans: 1024*1024*8 bits 8 Mega bits We use 8 bits to represent 256 discernable shades of gray 1Mega bits=1024*1024 bits The grayscale image has 1024*1024 pixels, Each pixel has 256 discernable shades of gray
11
3-(b) How many Mega bits are required for the image if it is a color one? Ans: 1024*1024* 3*8 bits = 24Mega bits 24 Mega bits The Colorful image has 1024*1024 pixels each pixel has 256 different values for red, green, and blue, respectively 3-(c) How many Mega bits are required for a video of 2 hours? Suppose a video requires 30 images per second? Ans: Assume the video is a colorful video. 2*60*60*30*24 = 5184000 Mega bits. 3-(d) How many Mega bits are required for the video if you compress the video using Mpeg which has a compression ratio 100:1? Ans: 5184000/100 = 51840 Mega bits.
12
4.Please write a program to record the number of rolls that occur before doubles are obtained. Please show the number in a text box. Also, the user has to hit the button in order to start the counting. Note that a double means two dice having the same number. PS. 你必須顯示在第幾次出現 Double 的情況 Ans: Test 4 <script type="text/javascript" src="http://dave-reed.com/book/random.js">
13
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("OutputCount").value=count; } 參考 — 初始值設定 d1=RandomInt(1,6); d2=RandomInt(1,6); count=1;
14
The round(s) occur double!
15
5.Predict the output sequences that would be produced by each of the following while loops. (a) num = 1; while (num >= 5 && num <= 30) { document.write(num + " "); num = 3*num + 1; } document.write("DONE WITH FIRST!"); Ans: DONE WITH FIRST!
16
5-(b) x = 1; y = 30; while (x < y) { document.write(x + " " + y + " "); x =2* x + 1; y = y – 2; } document.write("DONE WITH SECOND!"); Ans: 1 30 3 28 7 26 15 24 DONE WITH SECOND!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.