• 沒有找到結果。

Homework 5 Answer 7. Rewrite the following program segment using a

N/A
N/A
Protected

Academic year: 2022

Share "Homework 5 Answer 7. Rewrite the following program segment using a"

Copied!
3
0
0

加載中.... (立即查看全文)

全文

(1)

Introduction to Computer (NTU, Fall 2015)

- 1 -

Homework 5 Answer

7. Rewrite the following program segment using a repeat structure rather than a while structure. Be sure the new version prints the same values as the original. Initialization:

9. What must be done to translate a posttest loop expressed in the form

into an equivalent posttest loop expressed in the form

“Condition” should be opposite to each other.

13. What is the difference between a formal programming language and a pseudocode?

A programming language is a set of characters (words, numbers, symbols) and syntax used to write code that can be understood by a computer and then executed (ran by the computer) as a program.

Pseudo-code is language that mimics true programming language but computer cannot understand and execute.

num = 0

while (num < 50):

if (num is Odd)

print(num is Odd) num = num + 1

num = 0 repeat:

if (num is Odd)

print(num is Odd) num = num + 1

until (num>=50)

repeat:

(. . . )

until (. . . )

do:

(. . . )

while (. . . )

repeat:

(. . . )

until (condition )

do:

(. . . )

while (not condition)

(2)

Introduction to Computer (NTU, Fall 2015)

- 2 -

14. What is the difference between syntax and semantics?

Syntax refers to the primitive’s symbolic representation.

Semantics refer to the actual meaning of the primitives.

16. The following is a multiplication problem in traditional base 10 notation. Each letter represents a different digit. What digit does each letter represent? How did you get your

foot in the door?

V = 0, W = 4, X = 1, Y = 3, Z = 9.

18. Four prospectors with only one lantern must walk through a mine shaft. At most, two prospectors can travel together and any prospector in the shaft must be with the lantern.

The prospectors, named Andrews, Blake, Johnson, and Kelly, can walk through the shaft in one minute, two minutes, four minutes, and eight minutes, respectively. When two walk together they travel at the speed of the slower prospector. How can all four prospectors get through the mine shaft in only 15 minutes? After you have solved this problem, explain how you got your foot in the door.

Andrews and Blake go through the shaft first (2 minutes), and Andrews returns with the lantern (1 minute). Then, Johnson and Kelly go through (8 minutes), and Blake returns with the lantern (2 minutes). Finally, Andrews and Blake go through again (2 minutes). The total travel time is 2 + 1 + 8 + 2 + 2 = 15 minutes.

27. Identify the termination condition in each of the following iterative statements.

a.

Count>=5 b.

Count = 1 c.

Count >=5 “or” Total>=56 while (Count < 5):

. . .

repeat:

. . .

until (Count == 1)

while ((Count < 5) and (Total < 56)):

. . .

(3)

Introduction to Computer (NTU, Fall 2015)

- 3 -

31. Suppose we apply both Check1 and Check2 (defined next) to the input value 1. What is the difference in the printed output of the two routines?

Check1(1) no output Check2(1) print 1

34. Call the function CodeWrite (defined below) with the value 100 and record the values that are printed.

100 50 25 12 6 3 1 1 2 4 7 13 26 51 101

52. Explain what will be the values of a and b if we call the function CodeWrite (defined below) with inputs 78 and 89.

def Check1 (num):

if (num % 2 == 0):

print(num) Check1(num + 1) def Check2(num):

if (num % 2 == 1):

print(num) Check2(num + 1)

def CodeWrite (N):

if (N > 0):

print(N)

CodeWrite(N / 2) print(N + 1)

def CodeWrite (a, b):

if (a > 0 and b > 0):

a = a + b;

a = a + b;

a = a + b;

print(a and b)

CodeWrite (78, 89):

if (a > 0 and b > 0):

167 = 78 + 89;

256 = 167 + 89;

345 = 256 + 89;

print(345 and 89)

參考文獻

相關文件

With the help of the pictures and the words below, write a journal entry about what happened.. Write at least

Before the recording is played, you will have five minutes to study the Question-Answer Book and the Data File to familiarise yourself with the situation and the tasks.. Complete

Before the recording is played, you will have five minutes to study the Question-Answer Book and the Data File to familiarise yourself with the situation and the tasks. Complete

Use crystal field theory to deduce what geometry is consistent with

A) 5.0 minutes B) 2.5 minutes C) 7.5 minutes D) 100 minutes E) 0.6 minutes Answer: A.. Two mechanisms proposed for the following reaction of nitrogen dioxide with carbon monoxide

* Attained: Accumulated at least 420 minutes of moderate- to vigorous-intensity physical activities across the week (60 x 7 = 420 minutes). 本星期體能活動總時間:

team dumped the ball to the opposite court but the libero of the China team saved the ball just in time.. Then, the attacker scored successfully in the last two minutes of

identify different types of tourist attractions and examine the factors affecting the development of tourism in these places;.4. recognize factors affecting tourist flows and the