การเขียนโปรแกรมเพื่อใช้งาน (programming for...

271

Upload: -

Post on 14-Jun-2015

123 views

Category:

Software


0 download

DESCRIPTION

การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

TRANSCRIPT

Page 1: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 2: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 3: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 4: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 6: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 7: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 8: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 9: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 10: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 11: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 12: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 13: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 14: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 15: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 16: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 17: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 18: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 19: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 20: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 21: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 22: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 23: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 24: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 25: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 26: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 27: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 28: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 29: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 30: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 31: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 32: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 33: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 34: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 35: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 36: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 37: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 38: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 39: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 40: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 41: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 42: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

1. Each of the followinmg represents an attempt to write an

algorithm that computer the sum of the first 10

integers. State why each is either poor or invaled.

(a) START

set sum to 1+2+3+4+5+6+7+8+9+10

write sum

END OF THE ALGORIOTHM

(b) START

set n to 10

set sum to 0

add n to the sum

decrement n by 1

set if n > 0

if it is go back and repeat the previous step

write sum

END OF THE ALGORIOTHM

(c) START

set i to 10

set sum to 0

repeat 10 times

add i to sum

end of the repeat loop

write sum

END OF THE ALGORITHM

Page 43: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

4 0

(d) START

s e t i to 1

se t sum to 0

while i c 10 do

add 1 to sum

increment i by 1

end of the while loop

write sum

END OF THE ALGORITHM

2. W r i t e a va l i d a l g o r i t h m t o f ind the sum of the first k

i n t e g e r s l,‘Z,...,k. t h e v a l u e of k will b e e x t e r n a l i n p u t

t o t h e a l g o r i t h m . ( B e sure t o h a n d l e t h e ilegal s i t u a t i o n o f

K <= 0)

3 . Wri te an algorithm t h a t ditermines t h e m o d e o f a l i s t

mode is the value that occurred most frequently. For example, in

1 2 2 3 3 3 4 5 5 6 6 7 7 7 7

the mode is 7 and has a frequency of 4. You may assume that

(a) The values are has already sorted into ascending

sequence.

(b) If two or more values occur an equal number of

times, the mode will be defind as the numerically

1 argest value. The algorithm you develop should

n o t s t o r e a l l t h e d a t a i n a l i s t ; i t s h o u l d r e a d

Page 44: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

in only one value at a time and process that

value. The output of your algorithm should be

the mode and its frequency.

4. Develop an algorithm to solve quadratic equations of the

form ax2 + bx + c = 0 using the quadratic formula

Roots = mb - b - 4ac* P - - - - - -

2a

YOUI- algrithm should handle the regular cases as well as the

special cases of;

(a) A double root (b' - 4ac < 0).

(b) Complex roots (b7 - 4ac < 0).

(c) A nonquadratic equation (a = 0).

(d) An illegal equation (a = 0,b = 0).

5. Assume that you are given a string of input text of

arbitary length and ending with a "$". Furthermore, assume

that you wish to print this text as a set of line of width

no greater than k columns (k will be external input to the

algorithm). The rules of our printing alSO specify that we

must never break up a word between lines (Assume the length

of one word ) will never exceed k ) If a word cannot fit on

the current line , print the current line, print the current

line as is and go to on to the next line For example , if

Page 45: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 46: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 47: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 48: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 49: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 50: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 51: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 52: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 53: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 54: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

5 2

nlvnFlJLioL5luvfl modu 1 e dW~WWl?&li? Ql71tuvlfJ~lvat

Page 55: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 56: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 57: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 58: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 59: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 60: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 61: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 62: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 63: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 64: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 65: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 66: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 67: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 68: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 69: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 70: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 71: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 72: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 73: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 74: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 75: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 76: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

74

begin

writeln('in p2:', x:3,y:3,a:3,b:3,c:3,d:3)

a:=ord(b)

end;{of p2]

begin[pl}

d:='x'

writeln('in PI:', x:3,y:3,a:3,b:3,c:3,d:3);

x:=y;y:=O;b:=chr(a);c:=chr(x);

p2;

writeln('out pl:', x:3,y:3,a:3,b:3,c:3,d:3)

end;{of pl)

begin {program block}

a:=l;b:=Z;c:=3;

pl(a,b):

pl(b,c) i

pl(c,a)

end;{of block]

6. Assume that we had a procedure that merged two sorted

listed a and b procuced a!new master list c containing all the

elements of both a and b. The parameters to the

procedure are:

Page 77: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 78: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 79: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 80: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 81: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 82: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 83: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 84: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 85: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 86: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 87: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 88: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 89: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

87

d e c l a r e v a r i a b l e

startd e c l a r e v a r i a b l e

let x = x+1

Write X

end o f ~roc~ram.

w-i te X

ii:End.

b . s e l e c t i o n

b.1 if then

F

i f ( c o n d i t i o n ) t r u e

then do task A

e n d o f i f

do task B

Page 90: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 91: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 92: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 93: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 94: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 95: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 96: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 97: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 98: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 99: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

program ex3(output);

var

low, term : real;

sign, n : integer;

begin

logn := 0;

sign := 1;

n := 1;

term := 1;

writeln('n logn term’);

while(term >= 0.0001)

do

begin

term := 1.0/n;

logn := logn + term*sign;

writeln(n, logn, term);

n := n+l;

sign := -sign

end {end of while}

end.

Page 100: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 101: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 102: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

1 average = sum/howmany

Page 103: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 104: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 105: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 106: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 107: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 108: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 109: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 110: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

1 0 8

50 FOR I = 1 TO 10 STEP .5

100 NEXT I

Page 111: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

3. nis'l.ii nes t ed f o r l o op finh-rLnn r-h

3.1

10 : for i := 1 to 10 do

begin

f o r j :=l to5do

begin

end;

i f ( c o n d i t i o n ) t h e n g o t o 1 0 :

end

Page 112: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

for i

begin

program exfor...

for i := 1 to 100 do

begin

end

Page 113: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

111

Which Loop Primitive Should I Use?

In this chapter we have introduced three statements for

p e r f o r m i n g i t e r a t i o n i n P a s c a l - - t h e w h i l e , r e p e a t , a n d f o r .

You w i l l need to choose the appropriate one when you begin

w r i t i n g YO”I- programs. In many cases it won’t matter , but

these two guidedlines can assist you in making the r i g h t

choice.

1. T h e f o r l o o p i s t h e l e a s t f l e x i b l e s t a t e m e n t

and s h o u l d never b e u s e d w h e n there a r e t w o o r more

c r i t e r i a for loop emination. Y o u w i l l g e t y o u r s e l f i n t h e

f o l l ow ing b ind .

{loop can terminate after 100

iterations or upon encountering a negative value}

for i := 1 to 100 do

begin

readln(x) ;

if x < 0 Then ????

else

{ p r o c e s s t h i s d a t a v a l u e }

end { f o r l oop }

What do we put in for the question marks? It will have to

be an unconditional branch. If we had selected a while

o r repeat ( the l oop termination conditions could have

been phrased as

Page 114: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

1 1 2

while (count C= 100) and not(x c 0) do

2. When choosing between the while and repeat

take a carefule look at the null case -- the situation in

which there are no data to process The repeat loop will

alWayS execute the 100p today at least once and may not

correctly handle the null case.

Page 115: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 116: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 117: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 118: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 119: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 120: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 121: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 122: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 123: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 124: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 125: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 126: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 127: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 128: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 129: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

1. Required punctuation missing

2. Unmatched parenthesis

3. Missing parenthesis

4. Incorrectly formed statements

5. Incorrect variable names

6. Misspelling of reserved words

syntax e r r o r ,J~rlnn~i~niln~an~~~~ (interaction)Y

~~oJfl~~Jn~o~lnnillul~ ~J$%atilJa=i4u

1. Conflicting instructions

2. Nontermination of loops

3. Duplicate or missing labels

4. Not declaring arrays

5. Illegal t,ranr;fer

Page 130: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 131: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 132: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 133: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 134: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 135: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 136: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 137: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 138: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 139: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

1 3 8

Page 140: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 141: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 142: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 143: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 144: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 145: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 146: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 147: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

146

Loops

1.

2.

3 .

4 .

5 .

Data

1.

2.

3 .

4 .

Variables

1.

2.

3 .

4 .

N o t i n i t i a l i z e t h e l o o p p r o p e r l y .

Not terminate the lopp properly.

Wrong number of loop cycles.

I n c o r r e c t i n d e x i n g o f t h e l o o p .

I n f i n i t e l o o p s ( s o m e t i m e s c a l l e d c l o s e d l o o p s ) .

Failure to consider one or more conditions.

F a i l u r e t o e d i t o u t i n c o r r e c t d a t a .

Trying to read less or more data than there are.

E d i t i n g d a t a i n c o r r e c t l y o r m i s m a t c h i n g o f e d i t i n g f i e l d s

w i t h d a t a f i l e d s .

Using an uninit ial ized variable.

Not resetting a counter or accumulator.

F a i l u r e t o s e t a p r o g r a m awitch c o r r e c t l y .

Using an incorrect variable name (that is , spell ing

error using wrong variable).

Arrays

1. F a i l u r e t o c l e a r t h e a r r a y .

2 . Failure to declare arrays large enough.

3 . Transpose the subscript order.

Page 148: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 149: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 150: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

1 4 9

Semantic Error

These errors are caused by the failure to understand exactly

how a command work::. An example is to assume that arithmetic operations

are rounded. Anot~her example is to assume that a loop will be skipped

if the ending valu~e is smaller than the initial value. In IBM FORTRAN

00, loops are always executed once.

Semaphore Bug

This type of bug becomes evident when process A is waiting on

a process B while process B is waiting upon process A. This type of bug

usually emerger; when running large complicated systems, such as operatin

systems. This is called the deadly embrace.

Timing Bug

A timinS bug can develop when two operations depend on each

other in a time sense. That is, operation A must be completed before

operation B can start. If operation B starts too soon, a timing bug

can appear. Both timing bugs and semaphore bugs are called situation

bugs.

Opration Irregularity Bugs

These bugs are the result of machine operations. Sometimes

unsuspecting programmers do not understand that the machine does arith-

metic in binary; so the innocent expression

Page 151: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 152: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 153: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 154: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 155: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

Task Units

Planning 1

5 . Pr

insert, delete

ulJ%l~flalualul:

alul5fl~Lfla‘Lunl:

“151\

Writing

Debugging

Testing

Page 156: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 157: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 158: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 159: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 160: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 161: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

160

? 100

index fibonacci number

0 1

1 1

2 2

3 4

4 8

5 16

6 32

7 64

8 128

end of the fibonacci sequence

a total of 8 numbers were generated

(a) Insted of printing out 100 numbers as we had requested,

the program only pi-inted out 8. In addition, the numbers

did print are incorrect, Discuss how you would go abo

finding and correcting this logic error. In your answ

discuss the role that hand-simulation, the program outpu

and additional writeln commands would play in helping y

correct the mistake.

(b) After correcting the error(s) from part a, test the

program to see if it works property under all possib

conditions. if not, suggest necessary change to make t

program more secure against pathological conditions.

Page 162: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 163: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 164: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 165: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 166: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 167: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 168: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 169: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 170: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 171: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 172: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 173: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 174: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 175: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 176: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 177: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 178: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

D E F I N T I

D A T A “B”, 4 0

READ NAM$,IVOL

= IVOL **I

ANEXT I

Page 179: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 180: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 181: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 182: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 183: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 184: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

1 8 4

Page 185: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 186: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 187: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 188: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 189: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 190: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 191: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 192: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 193: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 194: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 195: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 196: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 197: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 198: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 199: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

1

2

100 2nruuuufiaz.Pizlnn

67 a2 69

43 65 72

56 76 74

Page 200: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 201: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

sex : 1: %lU

2: MGJ

age : i n t e g e r 2 Gn

per : 1: ‘Sal.

2 : -Iha,

3 : 6QU+-

Page 202: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

I

Page 203: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 204: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

2 0 4

uuuii 1

Btype .__ . . . . . . . .

var . . . . . . . . .

Page 205: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

205

task1 : decisl[l,l ] :=: decisl[l l]+l

t a s k 2 : decisl[l,Z] := decisl[l,Z]+l

t a s k 3 : decisl[l,3] : = decisl[l,3]+1

t a s k 4 : decisl[2,1] := decisl[2,1]+1

t a s k 5 : decisl[2,2] := decisl[Z 2]+1

taskR : decis1[2,3] :== decisl[2 3]+1

end

readln(age,per)

/decisl[sex,per]:= decisl[sex,per]+l

Page 206: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 207: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 208: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 209: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 210: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 211: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 212: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 213: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

2 1 3

2. reduced representation form

1 J 1

2 1 3

A 4x3 5 1 7

5 5 -22

Page 214: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 215: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

14 5 a2

4 3 1 9 7

5 1 5 0 2

8 9 0 7 5

2 7 2 5 14

1

4

5

R

2

3

1

9

7

5

0

2

7

5

14

Page 216: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

- name A 20 element array of character

- year in school : An integer between 1 and 4

- Grade point average :real

- Fees Paid : boolean

type studentrec = record

name : array [l..lO] of char;

year : 1..4;

!3ra : real;

fees : boolean

end.{student)

var

student : array[l..ZO] of studentrec ;

Page 217: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

s.tudent[l]

name[l] ._ name[lO] yearname[l] ._ name[lO] year gra feesgra fees

student[ i] := student [j]

Page 218: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

student[l].name[l],...,student[l].name[l9]

studant[l].year

student[l].gra

student[l].fees

a .

with student[l] do

begin

for i:=l to 20 do

read(name[i]);

year := 1;

9ra := 4.0;

fees := true;

Page 219: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 220: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

2 2 0

1. Show the Pascal representation of the following constants.

*(a) .a

(b) e (the base of the natural logarithms)

*(c) the fractional value l/2

*(d) 6.02 * 10z3

(e) the number 7

*(f) the character 7

(g) the color red

e(h) 18 billion

(i) truth

(j) the blank character

2. Which of the following are valid Pascal const

declarations? Identify the error(s) in each of the invalid

declarations.

(a) const

highvalue = 200;

(b) const

lowvalue = 0 or 1 or 2;

(c) const

firstchar : 'a';

(d) const

greatest = 200.0;

least, = -100.0;

(e) const

range = 200..800;

Page 221: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 222: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

2 2 2

Cc) var

a : integer;

b : integer;

c : integer;

abc :real;

( d ) “ar

resulta, resultb:real;

count : integer;

C l :char;

X,Y :real;

6. Choose ressonable names and write B single var declaration

for the following values.

( a ) t h e t h r e e r e a l c o e f f i c i e n t s o f a q u a d r a t i c

equation

(b) the two real roots of that equation.

(c) a boolean value indicating whether or not there

was a solution

(d) an integer value that indicates the data set number

(e) the primary colors red, yellow, and blue

7. Choose reasonable names and write a single var declaration

f o r t h e following values.

(a) a six-digit student identification number

(b) year in school (freshman, sophomore,...)

(c) year of graduation

(d) grade point average (A = 4.0)

Page 223: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 224: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

224

10. Look up the character code used on your computer and state

what the result of each of the following expressions would

be on your computer.

(a) 0x-d ('9') (b) chr(3)

(c) succ('z') (d) pred(ord('#'))

(e) chr(trunc(sqrt(517)))

11. Using the relational operators and the knowledge that

true ) false implement the following logical operators as defined

by the given truthtables.

(a) p=q(equivalence)

Page 225: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

tb)

CC)

225

pbqtexclusive -or)

paq(NEGATIVE IMPLICATION1

Page 226: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

226

12. Assign the following given value of three variables

a := 4;

b := -15;

c := 7;

d := red ;

What is the value of each of the following Pascal expressions?

( a ) ( ab s ( b -10 ) + a mod (c - l ) )

(b) a + 103 div sqr(a-c)

(c) pred(a * 6 + b div 5)

( d ) succ(d)

(e) (2 + a *b mod c + 1) < 2

13. Using the declarations from Exercise 12 as well as the

f o l l o w i n g n e w v a r i a b l e s :

x,y,r : r e a l ;

X: = 1.52El;

Y: = 0.3;

2: = -5.lE3;

What is the value of each of the following Pascal expressions?

Page 227: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

227

(a) trunc(x *y + 1.0) a

(b) x/y ~3.4 +- z

(c) abs(sqr(sin(y) + CC%(Y)) - 0.5)

(d) round(x) div round(y + 1.6) + b

(e) exp(c - 4)

14. Using the declarations from Exercises 12 and 13 and the

following additional declarations:

"a=

m,n : boolean;

P : ch:Br;

m := true;

n :=false;

P := 'a'

What are the values of the following expressions?

(a) m and not n

(b) (a>b, and (b>c) or not(c = 7)

(c) not odd(c) m

( d ) (x,0.0) or (y>O.O) and(z>O.O)

(e) chr(succ(ord(p)))

15. Determine whether the following Pascal expressions are

a correct translation of the corresponding mathematical

notation. If not, add the IlC5CeSSaI-y parentheses to

correct it.

Page 228: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

228

(a), I sin .5 - 1

cos t + 1

k := sqrt(sin(theta)-l.O/cos(thata)+l.O)

(b) ax*+ bx + c, k:= a * sqr(x) + b * x + c

Cc) a +c e I sqrt(a / b + c/d - e/f)

b d f

16. Translate each of the following English or mathematical

specifications into a correct Pascal assignment stagement.

Show the var declaration for all variables used in the

expressions.

(a) Taxable pay is gross pay less a fixed deduction

of $14 and less $11 for each dependent.

(b) The interest charge is 5% on the part of the

balance that exceeds $100 (you may assume that

balance >= $100).

(c) The average is sum of all scars

number of scores less illegal ones

Page 229: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

229

(f) Valid is true if and only i f the examination

score is between 200. and 800.0, inclusive.

(17) Done is false unless x is negative, or y is

n e g a t i v e , orboth x and y a r e e x a c t l y 0 , i n

which case done is true.

17. Assume that we have the following data.

input pointer

II5 3 8 1 102

~601 0 8 0

1 5 Q 102

What values will be assigned to the integer variables a, b,

c by each o f t h e f o l l o w i n g i n p u t o p e r a t i o n s ? I n a d d i t i o n ,

show where the input pointer would be at the conclusion of

each s e t o f input o p e r a t i o n s . ( A s s u m e t h a t e a c h s e t o f

input commands i s independent and begins with the input

p o i n t e r i n the p o s i t i o n s h o w n . )

( a ) readln(a,b,c)

I(b) read(a,b,c)

I(c) readln(a);readln(b);readln(c)

i(d) read(a);readln;read(b);readln;read(c)

(e) readln;read(a,b,c)

Page 230: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

230

18. Write the declarations and the input commands needed to

read in data prepared for the following format.

(a) A master payroll card containing a social

security number(integer) and a department number

(integer),followed by a separate timecard

containing hours worked this week (real).

(b) A single student grade card containing a 6 digit

student identifier (integer) followed by three

letter grades (a,b, c, d, e) all separated by

exactly one blank character.

19. Show how the data cards should be prepared if the input

commands in our program are written like this.

(a) read(x,chl);

read(y,chZ)

(b) readln(x,y);

readln(chl,chZ)

(c) readln(x,y,chl,chZ)

(d) readln(x);

readln(y);

readln(ch1);

readln(ch2)

Page 231: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

23,

20. Assume that the variables w, x, y, L have been declared to

be of type integer and that the symbol ' ' represents the

end-of-file condition. If our input data is as follows:

input pointer

I13 80

21 Q

for each of the following input sequences, give the value

assigned to each variable and the value of the boolean

function eo'f on completion of the entire sequence Also

state whether or not the sequence would lead to an errc~r

condition caused by attempting to perform a read (or

readln) while eof is true.

*(a) read(w); (b) readln(x,y,r)

read(x);

read(y); (c) read(w,x)

read(r) read(y,z)

(d) readIn(

readln(y);

readln(r)

Page 232: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

232

21. Assume our input was currently:

input pointer

I

Il ine 1 eoln next line eoln times up ,...

po in te r on comp le t i on of the input sequence. (Assume chl

and ch2 have been declared to be of type char.)

( a ) read(chl);readln

(b) readln;read(chl)

( c ) read(chl,chZ)

(d) readln(chl,chZ)

(e) readln(chl);readln(chZ)

22. Show the exact output produced by each of the following

output sequences. Assume t h a t w e a r e presently at the

beginning of a new line and that the referenced variables

h a v e t h e f o l l~owing v a l u e s .

x := 1.23456

y := 567 .89

z := 10

c := ‘$’

( a ) writeln(x,y,z);writeln(c)

( b ) writeln(x:15:l,y:8:3,z)

(c) write(x:15); write(y:15); write(c:3)

(d) writeln(x:16:4);writeln(y);writeln(z:l)

Page 233: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 234: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

2 3 4

24. Write a complete Pascal pIWgI?Sl to input a data card

containing three values

(a) A real value corresponding to the amount of a

loan m dollars and principal).

(b) A real value corresponding to the interest rate

of the loan.

Cc) An integer quantity, N, giving the number of

payment periods per The program should complete

the loall payment required for each pay period

repay the outstanding principal in one year at

the indicated interest rate in N payments. The

for doing this is:

Payment = p(l + i) + 25N

where p = principal

i = interest rate

N = number of pay periods per year

$25 = annual service charge

The program should print out the input data and the answer in some

nice, format. NOW attempt to run this program on the computer

facilities available at your installation. If appropriate, run

the program in both a batch access and sharing mode. Familiarize

yourself with all aspects of the operations policy your computer

center. This would include:

Page 235: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

2 3 5

Location of keypunches and computer terminals.

Operating procedure for keypunches and terminals.

Location of program submission stations.

Commands required for program preparation,executio

n, and stage.

Availability of consulting help and documentation.

25.Write the complete IJascal for solving this solutions to the

equation

3x + 2y 72 = 5

for values of x,y,and L inthe range Cl to 100.

26. Let vl and v2 represent boolean variilbles and let cl,sZ?,and

s-3 represent PaSCal statements Suppose that. ant: of the

statements is to be executed baszed on the values of the

boolean variables. as follows.

v2

t f

,vl t 51 s2fqs 3 s3

Set up a conditionals statement to achieve this effect.

Page 236: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

236

27. Write a complete Pascal program to computer the average of

all legal examination scores. Alegal score is one in the

tange 0 to 150. You I‘ program should read scores until an

end-of-file condition occurs and then product as output:

(a) The average of all legal scores.

(b) The number of legal scores.

(c) The number of legal scores.

(Be sure your programworksproperlyevenwhen there ar

le$J7al Xor-es.)

28. The Fibonacci series IS defind as

II<, = I , n = 1

llj+:, = n, + nI., i = O,l,Z,

Thus, the fir-t few Fibonacci numbers are

1,1,2,3,5,8,13,...

Write a complete Pascal program to compute and print the

first k Fibonacci numbers, where k is, input to the program.

(Your program should work property even if k (= 0.)

29. Temperatures on the Celsius (or centigrade) scale are

relate on the Fahrenheit scale by the formula

Page 237: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

237

Write a complete program that prints the Celsius equivalent

of Fahrenheit temperatures in the range low to high where

low and high are input to the program. The increment sire of

the table should also be input to the program.

30. Write a program that reads text and produces encoded text

by replacing each character with the character that occurs

five positions "later" in the character set.(Thus,

cnnsidering the typical ordering for alphabetic character,

'a' wouldbe replaced by 'f,' 'b' by 'g,' etc.)This

replacement should "wrap around" the end of the character

set so that there is a well-defined replacement for each

character. (That is, the character that occurs at the end

of the character set should be replaced by the character

i n the fifth position.You will have to know how large the

character set is on your computer.)Read text and print the

encoded form until an end-of-file condition occures.

31. Write a complete program that compute the minimum number

of coins and bills needed to make change for a particular

purchase. The cost of the item and the amount tendered

should be read as data values.Your program should indicate

how NlY3ny co 1 ins and bills of each denomination are needed

for change. Make use of these denominations.

Coins: $0.01,!$0.05,$0.10,$0.25 , Bills: $1,$5,$10

Page 238: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

238

32. Write a program to determine the frequency of each vowel in

some English language text.The input will consist of sentences

running over a number of line. The end of the text is indicated

by the special symbol '*' that will not appear any where else in

the text. The output of the program should be the input text and

the percentage of characters that were equal to 'a,' 'e,' 'i,' 'o

'u, ' Blanks and punctuation marks should not be treated as

characters and should not be include in the total.

33. Write a program to count the total number of words in some

English language text. The input will consist of text

running ober a number of cards or lines. The end of the

text will be indicated by the end-of-file condition

becoming true. YOUI- program should count the number of

words n the entire input, whrer a word is defined as any se

quence of nonblank characters bounded on either side by one

o r more blank characters. (Therefore things like + 1.2,

real-life, don't,and l,OOO,OOO are all counted as one word.)

The output from your program should simply be the number of

words in the text.

34. The area of a circle with a radius of 1 is a, and the area

of a square thatjust contain the circle is 4. Therefore, if

a large number of points is chosen randomly in the square,

the fraction of those points thatfall within the circle

Page 239: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

2 3 9

will be apprc8ximately s/4. Assuming the existence of a

standard real-valued function called random that returns

a r a n d o m number, r, between 0 and 1, write a program to

compute an approximation to d

35.Assuming randomnumber is a standard real-valued function that

return a random number, I-, between 0 and 1, write a program

that approximates the probabilities for rolling the values 2 to

12 with two dice.Use a case statement in the program.

36. Write n Pascal program to process the weekly payroll of the

Brooks Leather Company (BLC). For each employee of BLC your

program will compute the gross pay , deductions , and net

pay. This information is to be clearly printed in the output

along with certain summary information for the entire payroll.

Each week BLC punches a data card for each employee that

includes the following information. Social security number

(9 digests)

Hourly pay rate (xx.xx)

Number of exemptions (0 to 19)

Healt insurance code (1,2,3,or 4)

Hours worked (xx.x)

Using this information, your program should carry out the

following computations.

Page 240: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 241: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 242: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

242

If S is greater than But No More Than Letter Grada

M +

M + 2 s.d. 100

1 s.d. M + 2 s.d.

M - 1 s.d. M + 1 s.d.

B

C

M - 2 s.d.

0

M - 1 s.d.

M - 2 s.d.

A

D

Page 243: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 244: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 245: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

2 4 5

A f t e r r e a d i n g i n a n a m e , p r i n t i t o u t i n t h e m o r e s t a n d a r d

“ r e p o r t - o r i e n t e d ” f o r m a t .

ICCCCC ( cccc c.

f i r:;t 1 ast m i d d l e

name name i n i t a l

whe1.e ’ liist~ name and ’ f i r:;t-name’ i n c l u d e o n l y t h e f i r s t .

Rebei:ca Al 1 i son Schnc i der (. M e a n s b l a n k )

Page 246: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

2 4 6

43. Palendromes. A palendrome is something that reads the same

way backward andforwaed. The unit in a palindrome can vary

Sometimes it is a letter (WOW or MOM) or a number (4884 or 121)

or words: STRAP ON NO PARTS.

(a) Find some integers N which when squared are palindromes.

26 = 676

(b) FIND some palindrome integers N which when

squared are palindromes.

22 = 484

(c) Generalize the above for powers larger then 2

(d) Write a program that is also a palidrome.

here is the example of a parlindrome

78

+87

165

+ 561

726

+627

1353

+ 3531

4884

Page 247: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 248: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 249: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 250: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 251: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 252: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 253: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 254: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 255: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 256: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 257: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 258: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 259: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 260: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 261: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 262: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 263: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 264: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 265: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 266: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 267: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 268: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications
Page 269: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

Kingston H. Jeffey ALGORITHMS AND DATA STRUCTURES

Addison Wesley Publishing Company , 1991

Knuth E. Donald THE ART OF COMPUTER PROGRAMMING,

Addison Wesley Publishing Company , 1991

Schneider G. Michale , AN INTRODUCTION TO PROGRAMMtNG AND

PROBLEM SOLVING WITH PASCAL

John Wiley & Sons , 1982

Page 270: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications

98wulhiil... ~l~~~u~~~l~nula5l~~l~~~Ramlthamhaeng University Press.

Page 271: การเขียนโปรแกรมเพื่อใช้งาน (Programming for applications