INTRODUCTION TO COMPUTERS 2nd exam solution. Question 1 Suppose that you are given a group of non-negative numbers and you are asked to arrange them in

Embed Size (px)

DESCRIPTION

Solution of Q1 比較型的 Sorting Buble Sort Selection Sort Merge Sort Insertion Sort……

Citation preview

INTRODUCTION TO COMPUTERS 2nd exam solution Question 1 Suppose that you are given a group of non-negative numbers and you are asked to arrange them in sequence from largest to smallest. You may organize a line that begins with the largest person and continues in descending order according to magnitude. Describe an algorithm for completing this task. Solution of Q1 Sorting Buble Sort Selection Sort Merge Sort Insertion Sort Question 2 Suppose you need to look up a specific number from a sequence of N numbers using sequential search. A.How many checks would be required, in the worst case, to find the number? B.How many checks would be required, in the best case, to find the number? Solution of Q2 A.N B.1 Question 3 Consider the list of numbers shown below: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85] A.Which number in the list is the "easiest" to find using binary search? That is, which number could be located in the fewest number of checks? Please explain. Question 3 B.Which number in the list is the "hardest" to find using binary search? That is, which number would require the largest number of checks? Please explain. Solution of Q3 A.45 B.35 40 80 85 4 Question 4 Recall that Newtons algorithm uses the following expression to refine the square root of a positive number N: newApproximation = (oldApproximation + N/oldApproximation)/2 Question 4 Imagine that, while you are using Newton's algorithm, the approximations converge on the actual square root of N. Would the value change if you tried to further refine the square root? Why or why not? Solution of Q4 newApproximation=(oldApproximation+N/oldAp proximation)/2=oldApproximation Question 5 Please answer the following questions: A.Show the page before the user pushes the button. B.Show the page after the user types 45 in the tempBox text box, types 12 in the windBox text box, and pushes the button. Question 5 Solution of Q5-A Solution of Q5-B Question 6 Please answer the following questions: A.Show the page if the user pushes the button without typing anything in the inputBox text box. B.Show the page after the user types in I am sad. and pushes the button. Question 6 Solution of Q6-A Solution of Q6-B Question 7 Please answer the following questions: A.Show the page after the user types 25 in the redDot text box, types 30 in the blueDot text box, and pushes the button. B.Show the page after the user types 5 in the len text box, types 4 in the redDot text box, types 4 in the blueDot text box, and pushes the button. Question 7 Solution of Q7-A Solution of Q7-B Question 8 Create a Web page that reads in an intgeger number entered by the user and then determines whether that number is even positive, odd positive, negative, or zero. The page should contain a text box in which the user can enter the number and a button for initiating the analysis. Question 8 When the user inputs the desired number and clicks the button, a function should be called to identify the numbers type and display the result in an alert box. Solution of Q8 Question 9 Create a Web page that prompts the user to enter a specific number of years and then displays the corresponding time in seconds. Assume that all years consist of exactly 365 days. Question 9 Your page should include the definition of a function named YearsToSeconds that requires one input a value corresponding to a number of years--and returns the equivalent time in seconds, and another definition of a function named DoConversion that reads the number of years from a text box, calls the function YearsToSeconds, and displays the number of seconds in another text box after the user enters the number and pushes the button. Solution of Q9 Question 10 A.Consider the following code segment Given x=23 and y=7, what the output would the above code produce? Question 10 if (x >= y) { if (x*10 ");} else{document.write("TWO ");} } else{document.write("THREE ");} Solution of Q10-A TWO Question 10 B.Consider the following code segment: What ranges in which temperature and windSpeed are located in order to show a window with message Good 5!? Question 10 if (temperature = 15) {alert(Good 2!);} else{alert(Good 3!);} }} . else{ if (windSpeed > 10) {alert(Good 4!);} else{ if (windSpeed < 5) {alert(Good 5!);} else{alert(Good 6!);} }} Solution of Q10-B Temperature > 50 windSpeed < 5