PASCAL-SCHEME

PASCAL-SCHEME
PASCAL-SCHEME

F.3 CIT, 2010-11 Programming with Pascal

1) Structure of a program (1 P )

Program name, Declaration (Variables and constants), Main program (use of begin and end) Reserved Words. {...} = remark 備註, it will be ignored. '....' = prompt 提示. := means

an assignment 賦值 with calculation.

program chapter0;

var A, B, C :integer;

begin

write('A = '); readln(A); {input a value to A with prompt 提示}

write('B = '); readln(B); {input a value to A with prompt 提示}

C := A*A + B*B ; {assign A 2 + B 2 to C}

writeln('A*A + B*B = ',C); {output the value of C with prompt 提示}

readln; {hold the output window}

end.

Modify the above program to do other calculations.

Composite Statement : begin ........... end ; {as ONE complex 複式組合}

program chapter1;

var A,B :integer; c :real ; {C has decimal places 小數}

begin

write('A = '); readln(A);

write('B = '); readln(B);

if (A = 0) or (B = 0) then writeln(‘Invalid Input ’) else

begin C := 1/A + 1/B; writeln('1/A + 1/B = ',C :10 :4) end;

{output in 4 decimal places}

readln;

end.

Observe what happens if either A or B is inputted to be ZERO !!

2) Looping (1P)

program loop1;

var sum, i :integer;

begin sum := 0; {initialize 開始化 sum to be 0} for i := 1 to 4 do sum := sum + i; writeln(sum); readln; end.

Note the use of a and b. Guess the output of the following program.

program loop2;

var sum, i, a, b :integer;

begin

sum := 0; a := 1; b := 6; for i := a to b do {i := 1 to 6}

begin sum := sum + i ; writeln(i :10,sum :10) end ;

writeln(a,'+..+',b,' = ',sum );

readln;

end.

{use it to find 1+2+3+...100, 1000+1001+...+2000, 1/2 + 1/3 + 1/4 +...+

1/10000}

i sum

0 1 1 2 3 3 6 4 10 SUM i = 1 2 3 4

3) More Looping (3P )

program moreloop;

var i, first, final :integer; sum :real;

begin

write('First number ?'); readln(first ); {input a value to first }

write('Final number ?'); readln(final ); {input a value to final }

sum := 0;

writeln(' sum i new sum');

writeln(' --- - -------');

for i := first to final do {starting and ending is controlled}

begin

write(sum :8 :0,i :8); {sum = 0 and i = first for the first time}

sum := sum + i; {add sum by i, first ≤ i ≤ final}

writeln(sum :8 :0);

end;

writeln; {output a blank line}

writeln('sum = ',sum :10 :0); {10 digits and 0 dps}

readln;

end.

The for-loop can be changed to for i := 1 to 4 do and bypass the input statements , 1

and 4 are now treated as constants. Using 1 and 4 instead of first and final (variables) will be less flexible. In most situations, using variables can achieve more convenience than using constants.

Exercises (2P )

a) Run the program to calculate

i) 1 + 2 + 3 + 4 + .... + 10.

ii) 1 + 2 + 3 + 4 + ...................... + 100

b) Change the program to calculate

i) 1 + 2 + 3 + 4 + .... + 10. ii) 12 + 22 + 32 + 42 + .... + 10

2

c ) 1*2 + 2*3 + 3*4 + 4*5 + .... + 29*30

d) 1001.....4131211+++++

4) if then-else and logical operators (1P )

if odd(i) then sum := sum + i; {1+3+5+7...}

if odd(i) then sum := sum + i else sum := sum - i; {1-2+3-4+5-6+...}

if i mod 4 = 0 then sum := sum + i; {4+8+12+16...}

if not (i mod 4 = 0) then sum := sum + i; {1+2+3+5+6+7+9...}

if (i mod 3 = 0) and (i mod 4) = 0 then sum := sum + i; {12+24+36...}

if (i mod 3 = 0) or (i mod 4) = 0 then sum := sum + i; {3+4+6+8+9+12+...}

Difference between = and :=

= is used for comparing the variables and constants, usually after if

:= calculate result in RHS and assign the result to the variable in the LHS

Exercise : Insert the above if-then-else statements into the for-loop of moreloop.pas

Input 1 for First and any number(from small to large ) for final .

5) More examples (1P)

if i mod 7 = 0 then sum := sum + i else sum := sum + i*i;

if i > 10 then sum := sum + i else sum := sum + i*i;

if i < 10 then sum := sum + i else sum := sum + i*i;

if i >= 10 then sum := sum + i else sum := sum + i*i;

if i <=10 then sum := sum + i else sum := sum + i*i;

6) Explore the combinations of A 2

+ B

2

= C

2

, where A, B and C are positive integers.

Examples are 3, 4, 5; 5, 12, 13; 7, 24, 25; ....... (1P)

Use nested for-loops :

for A := 1 to 20 do

for B := 1 to 20 do

for C := 1 to 20 do

if A * A + B * B = C * C then

writeln(A :5, B :5, C :5);

How can you make sure that each combination appears only ONCE ?

Explore A 2

+ B

2

+ C

2

= D

2

(one example is 3,4,12,13)

7) Random Number and Simulations on throwing dice and playingcards (3P)

a) generate 3 numbers in the range 1,2,3,4,5,6 up to 20 times. (throwing 3 dice)

program noname; {dice.pas}

uses crt;

var x,y,z,i :integer;

begin

clrscr;

randomize;{to activate random number generator}

for i := 1 to 20 do

begin

x := random(6) + 1; y := random(6) + 1; z := random(6) + 1;

{random (6) returns a random integer from 0 to 5 inclusively}

textcolor(90); write('Round ',i:3,' : ');

textcolor(2); write(x :2,' + ',y :2,' + ',z :2);

textcolor(13); writeln(' = ':5,x+y+z:3);

if i mod 5 = 0 then readln;

end;

readln;

end.

b) Drawing 13 playing cards.

program noname; (card.pas)

uses crt;

var x,y,i :integer; stg :string;

begin

clrscr;

randomize;

stg := 'A234567890JQK'; {a string of text}

for i := 1 to 13 do

begin

x := random(13) + 1; {x = 1,2,3,4,..13}

{x represents a kind in a suite}

y := random(4) + 1; {y = 1,2,3,4}

{y represents a suite.}

if y < 3 then textcolor(4) else textcolor(7); {color}

writeln(i:5,chr(y+2):5,stg[x]:2); {output suit, kind}

end;

readln;

end.

c) Generate 6 random numbers in the range 1,2,3,4,5,...49, with the use of procedure.

(mark-six)

program mark6; {mark6.pas}

uses crt;

var i,k :integer;

ww

procedure draw;

begin

textcolor(4); write(k :3,' : ');

textcolor(7); for i := 1 to 6 do write(random(49)+1 :5);

{try to get 6 random integers from 1 to 49}

textcolor(2); write(' extra = ',random(49)+1 :3);

{try to get another random integer}

writeln;

if k mod 20 = 0 then readln;

end;

begin

clrscr;

randomize;

for k := 1 to 6 do draw;

readln;

end.

Advantage of using procedures : simply the main program so that a big task can be split into smaller tasks.

8) Mathematical task : , typical summation and series (2P)

Find the values of the following summation using a program;

4 {1 - 1/3 + 1/

5 - 1/7 + 1/9 - ......... + 1/2001}

1 + 1/

2 + 1/4 - 1/8 + 1/16 + ......... + 1/2

20

1 + 2*0.5 + 3*0.52

+ 4*0.5

3

+5*0.5

4

+ ..... + 20*0.5

19

Algorithm :

sum := 0; term := _____;

for i := _____ to ____ do

begin

term := term + _________

{or term : = term * ____________;}

{or if ______________ term := _______________________;}

sum := sum + term;

writeln(i:5,sum:10:6);

end;

readln;

9) Arrays of integer, characters and strings (3P)

The following program uses ARRAYS ENGLISH, SEX and NAME to store the English mark, sex and name of a class of 5 students.

program demoarray; {demoarray.pas}

Var SCORE :array[1..5] of integer;

SEX :array[1..5] of char;

NAME :array[1..5] of string; i :integer;

begin

for i := 1 to 5 do

begin

write('no. ',i:5,' Name ? '); readln(NAME[i]);

write('no. ',i:5,' SEX ? '); readln(SEX[i]);

write('no. ',i:5,' MARK ? '); readln(SCORE[i]);

end;

writeln('No. Name Sex Mark');

for i := 1 to 5 do

writeln(i:5,NAME[i]:12,SEX[i]:6,SCORE[i]:6);

readln;

end;

Advantages of using arrays :

i) use ONE variable ????[] to store many (hundreds or thousands) of values

ii) with the use looping, it can achieve maximum efficiency

iii)i is insignificant in the looping. Other variables j, a, b ... can be used.

10) Iteration using arrays (2P )

Fill in the table assuming x[0] = 1 according to the formula : x[i+1] := x[i] - (x[i]2 - 49)/(2*x[i]) i = 0 : x[1] := x[0] - (x[0]2 - 49)/(2*x[0]) i = 1 : x[2] := x[1] - (x[1]2 - 49)/(2*x[1]) i = 2 : x[3] := x[2] - (x[2]2 - 49)/(2*x[2]) i = 4 : x[4] := x[3] - (x[3]2 - 49)/(2*x[3])

program iteration; {iteration.pas}

var i :integer; x :array[0..100] of real;

begin

x[0] := 1; {try other values later}

writeln(' i x[i]');

for i := 0 to 20 do

begin

x[i+1] := x[i] - (x[i]*x[i]-49)/2/x[i];

write(i :5,x[i] :22 :8); readln; {a pause until is pressed}

end;

end.

Note that the iteration can compute the numerical value of

49. You can adjust 49 and

x[0] to other values for further observations.

11) Cubic root : Amend the above program to do the following iterations :

x[i+1] := 2*x[i]/3 + 64/3/x[i]/x[i], x[0] = 1

12) Generate the following sequence in which the next number is determined by the previous

number(s) :

2N - 1

1,3,7,15,31,63,.............. x[1] = 1, x[i+1] := 2*x[i] + 1

Square numbers

1,4,9,16,25,36,.............. x[1] = 1, x[i+1] := x[i] + 2i + 1

Triangle numbers

1,3,6,10,15,21,.............. x[1] = 1, x[i+1] := x[i ] + i

Cubic numbers

1,8,27,64,125,216............. x[1] = 1, x[i+1] := x[i] + 3i(i+1) + 1

Fibonacci no.s

1,1,2,3,5,8,13,21,34,55,...... x[1]=x[2]=1, x[i+1] := x[i] + x[i-1]

program sequence; {sequence.pas}

var i :integer; x :array[1..100] of integer;

begin

x[1] := _____ ;

writeln(' i x[i]');

for i := 2 to 20 do

begin

x[i+1] := ________________________________________________________ ;

write(i :5,x[i] :15); readln;

end;

end.

i x[i] 0 1 1 2 3 4

12) Further use of arrays : Recall the program of mark-six drawing with the newly added

underlined items. Can you observe the improvements ? (1P) program mark6; {mark66.pas}

uses crt;

var i, k, x :integer; drawn :array[1..6] of integer;

procedure draw;

begin

for i := 1 to 49 do drawn[i] := 0;

textcolor(4); write(k :3,' : ');

textcolor(7);

for i := 1 to 6 do

begin

repeat x := random(49)+1 until drawn[x] = 0;

write(x :5); drawn[x] := 1

end;

textcolor(2); write(' extra = ',random(49)+1 :3);

writeln;

if k mod 20 = 0 then readln;

end;

begin

clrscr;

randomize;

for k := 1 to 1 do draw; {change to 1 to 10 later}

readln;

end.

You should follow that the first six numbers in each draw will never be drawn twice.

Try to do small changes to ensure the extra number in each draw must be different from the first six numbers.

13) A Project of 5-player Mah-jong game(麻雀) using Looping (iterations) and arrays. (2-3P)

Initially players 'A', 'B', 'C' and 'D' have $100, E has $0. In each round there will be a winner or a draw (no winner). If there is a draw, all of them pay $2 to 'E' otherwise the winner will receive an $N from the loser(s), number of loser(s) may be 1 or 3. 'E' behaves as the host providing the facilities for 'A', 'B', 'C' and 'D'.

Open-ended decisions:

If winner is 'A' and loser is 'D' {出沖}, what happens to their money ?

___________________________________________________________________________________ ___________________________________________________________________________________

If winner is 'A' and losers are 'B','C','D'{自摸}, what happens to their money ?

___________________________________________________________________________________ ___________________________________________________________________________________ ___________________________________________________________________________________

How can you determine a DRAW or otherwise ?

__________________________________________________________________________________ How can you determine the number of losers and N ?

___________________________________________________________________________________ ___________________________________________________________________________________

program mah_jong; {mah_hong.pas}

uses crt;

var money :array[1..5] of integer; {the money of the players A,B,C,D,E} : : : :

begin

randomize;

money[5] := 0; for i := 1 to 4 do money[i] := _______________;

writeln('Round A B C D E');

for i := 1 to 16 do {4 rounds as 1 cycle}

begin

write(i :4);

if ______________________________________ then draw else win;

for j := 1 to 5 do write(money[j] :6);

writeln; delay(1000) {slow down the execution}

end;

end.

Guideline for procedures:

procedure draw;

begin

______________________________________________________________________________;

______________________________________________________________________________;

______________________________________________________________________________;

end;

procedure win;

begin

x := random(4) + 1; {x denotes winner}

N := __________________________________________________________________________;

if ___________________________________________ then {自摸}

begin {update money[]}

__________________________________________________________________________;

__________________________________________________________________________;

__________________________________________________________________________;

__________________________________________________________________________;

end else {出沖}

begin {update money[]}

repeat y : = _________________________ until y <> x; {y denotes loser}

______________________________________________________________________;

______________________________________________________________________;

______________________________________________________________________;

end;

end;

Practical Work : Work out whole program and run it with different settings/rules.

相关主题
相关文档
最新文档