Tutorial 2

ÇŃµ¶Ř»ĂĐĘ§¤ě

1. ŕľ×čÍăËéąŃˇČÖˇÉŇä´é·´ĹÍ§»®ÔşŃµÔ áĹĐ·´ĘÍş¤ÇŇÁŕ˘éŇă¨ăąËŃÇ˘éÍ

1.1 Data Types

1.2 Control Structure ·Ńé§ Repetitive Control Structure áĹĐ Selective Control Structure

1.3 Function áĹĐ Procedure

˘ŃéąµÍąˇŇĂ·´ĹÍ§

1. (Data Type and Operator) ¨§ŕ˘ŐÂąâ»ĂáˇĂÁ·Őč·Ó§ŇąµŇÁąŐé

1.1 ĂŃş¤čŇ a,b ·ŐčÁŐŞąÔ´˘éÍÁŮĹŕ»çą Integer â´ÂˇÓËą´ăËé a = 7, b = 2

1.2 ĂŃş¤čŇ c,d  ·ŐčÁŐŞąÔ´˘éÍÁŮĹŕ»çą Real â´ÂˇÓËą´ăËé c = 5.0, d = 3.0

1.3 ĂŃş¤čŇ e,f  ·ŐčÁŐŞąÔ´˘éÍÁŮĹŕ»çą Boolean â´ÂˇÓËą´ăËé e = True, f = false

1.3 ĘŃč§ăËéľÔÁľě Expression ·ŐčŕˇÔ´¨ŇˇˇŇĂ Operate ˇŃş Operand ·ŐčÁŐŞąÔ´˘éÍÁŮĹµčŇ§ć ´Ń§ąŐé

1.3.1 a mod b

1.3.2 c mod a

1.3.3 not((a <= b) and (c >= d)) (¤Ô´ÇčŇÇ§ŕĹçşăą·Őč¤ĹŘÁąÔľ¨ąě a<= b áĹĐ c>= d ÁŐ¤ÇŇÁ¨Óŕ»çąäËÁ)

1.3.4 Assign ¤čŇ b + c Ĺ§ä»ăą a ĘŃ§ŕˇµĽĹ·ŐčŕˇÔ´˘Öéą ËŇˇŕˇÔ´¤ÇŇÁĽÔ´ľĹŇ´˘Öéą ¨§şÍˇÇčŇŕˇÔ´˘Öéąŕą×čÍ§¨ŇˇĘŇŕËµŘÍĐäĂ ăËé·ÓˇŇĂáˇéä˘áĹĐľÔÁľě¤čŇÍÍˇÁŇ´Ůşą¨ÍŔŇľ

1.3.5 a div b

1.3.6 a + c / d - 4

1.3.7 (e xor f) or pred(true)

ĘŃ§ŕˇµĽĹ·ŐčŕˇÔ´˘Öéą ËŇˇŕˇÔ´¤ÇŇÁĽÔ´ľĹŇ´˘Öéą ¨§şÍˇÇčŇŕˇÔ´˘Öéąŕą×čÍ§¨ŇˇĘŇŕËµŘÍĐäĂ

ĘÔč§·Őč¤ÇĂĂŮé

 µŃÇ´ÓŕąÔąˇŇĂ ¤ÇŇÁËÁŇÂ ŞąÔ´˘éÍÁŮĹ˘Í§ Operand ŞąÔ´˘éÍÁŮĹ˘Í§   ĽĹĹŃľ¸ě + ˇŇĂşÇˇ Integer,Real Integer,Real - ˇŇĂĹş Integer,Real Integer,Real * ˇŇĂ¤Ůł Integer,Real Integer,Real / ˇŇĂËŇĂ¨ÓąÇą¨ĂÔ§ Integer,Real Real div ˇŇĂËŇĂ¨ÓąÇąŕµçÁ Integer Integer mod ˇŇĂËŇĂŕÍŇŕČÉ Integer Integer

 Precedence Numerical Operator Boolean and Relational Operator Highest (......) Second Highest not Third Highest *, /, div, mod and Fourth Highest +, - or, xor Lowest <, <=, >, >=, <>, =, in

2. (IF...THEN...ELSE, ; before ELSE)

program lab4_1;

{This program calculates the total payment on a loan based on the interest rate.

Try compiling the program. Correct the error and then re-run the program}

var loan, rate, total: real;

begin

write ('Enter Loan Amount ');

write ('Enter Interest Rate(%) ');

total:=loan+(loan * (rate/100));

if rate > 10 then

writeln ('Loan Shark');

else

writeln ('Go For It');

writeln ('Loan = ',loan:8:2,' Interest Rate = ',rate:2:2,'%');

writeln ('Total Repayment = ',total:8:2)

end.

3. (Compound Statement)

program lab4_2;

{This program prompts the user for the temperature and then determines whether it is above or below freezing. Try compiling the program.  Fix the program so that it does what it was intended to do.}

var degrees: integer;

begin

write ('Enter Temperature In Farenheit ');

if degrees > 32 then

writeln ('The Temperature Is ',degrees,' Degrees Farenheit');

writeln ('It Is Above Freezing')

else

writeln ('It Is Below Freezing')

end.

4. (CASE, Same Label)

{Every employee at Patriotic Paraphenalia has a base pay of \$200 per week. The week before July 4 each employee randomly selects an envelope containing his BonusNumber and gets a bonus as follows:

Someone with BonusNumber 1 through 8 gets a bonus of \$50.

Someone with Bonusnumber 4 is very lucky; she gets a \$100 bonus in addition to the \$50

bonus, for a total pay of \$350.

Someone with a Bonusnumber of 9 through 12 get a bonus of \$30

Someone with a BonusNumber of 13 gets a bonus of -\$30

Run this program three times. The first time input 6 for the BonusNumber, the second time input 4; the third time input 13. Then fix the program}

program Lab12_1;

var pay, BonusNumber, bonus: integer;

begin

pay := 200;

write ('enter Bonus Number ');

case BonusNumber of

1..8   :  bonus :=  50;

4      :  bonus :=  100;

10..12 :  bonus :=  30;

13     :  bonus := - 30;

end; {case}

pay := pay + bonus;

writeln ('Your July 4 week pay is ', pay)

end.

5. (FOR...TO...DO, Null Statement)

{GUIDED DEBUGGING -- Inserting Extra Writeln Statements. This program is supposed to find the sum of the first 12 cubes. Run it. Does it do what was intended?}

program Lab6_1;

{supposed to find the sum of the first 12 cubes}

var i, cube, sum: integer;

begin

sum := 0;

for i := 1 to 12 do;

begin

cube := i * i * i;

sum := sum + cube;

end;

writeln ('Sum of first 12 cubes is  ', sum)

end.

{HINTS

1.   1728 seems too small since 10 cubed by itself is 1,000 and thus surely the sum must exceed 2,000, since 11 cubed will exceed 1,000. Try the following debugging technique:

1.1 Insert the following statement after the statement  sum := sum + cube;

writeln ('i= ',i, 'cube= ',cube, 'sum= ',sum);

2. Run the program again and review the output. Any clues?

ANSWER:   Obviously, the writeln statement that you thought you were inserting into the loop body was executed only once. Why wasn't it executed 12 times? Answer: The semicolon after the word "do" produced an empty loop body and the block marked with

begin end was AFTER the for loop.

3. Fix the program and rerun it. The correct answer should be  6084}

6. (FOR...TO...DO, Out of range of variable)

{Before reading the HINT, try running and debugging this program on you own}

program Lab6_2;

{supposed to find the sum of the first 30 cubes}

var i, cube, sum: integer;

begin

sum := 0;

for i := 1 to 30 do

begin

cube := i * i * i;

sum := sum + cube

end;

writeln (sum)

end.

{HINT:  Run this program? Doesn't 19,617 seem a little small for the sum of the first 30 cubes, considering the size of 30 cubed alone. What went wrong?  Insert a writeln statement at the bottom of the loop and then consider what might cause the value of sum to jump to a negative value when i = 19}

7. (FOR...TO...DO, IF...THEN...ELSE and Boolean Test)

{If the user inputs the scores 45 87 91 44 and 98, the program is supposed to output  Lowest score was 44}

program Lab6_3;

{supposed to output the lowest score}

var i, score, low: integer;

begin

low := 0;

for i := 1 to 5 do

begin

write ('enter score ');

if score < low then low := score;

end;

writeln ('Lowest score was ', low)

end.

{What do you think the output will be if this program is run with the user entering the scores 45, 87, 91, 44, and 98? Run it.  Then insert an appropriate writeln statement and rerun it. Then fix the error and rerun it .  }

8. (REPEAT...UNTIL)

{This automatic teller program gives you a starting balance of \$500 and then uses a loop to allow you to enter as many transactions as you wish. Run it with the following three transactions:

a deposit of \$200,  a withdrawal of \$50, and a withdrawal of \$100. Your closing balance

should be \$550. What is wrong?}

program Lab7_2;

var balance, code, amount: integer;

ans: char;

begin

balance := 500;

writeln ('Your starting balance is \$500');

repeat

write ('enter amount of deposit or withdrawal ');

write ('enter 1 for deposit or 2 for withdrawal ');

if code = 1 then

balance := balance + amount

else

balance := balance - amount;

write ('enter y to continue or n to stop ');

until upcase (ans) = 'N';

writeln ('Closing balance \$', balance)

end.

9. (Update Loop Control Variable in REPEAT...UNTIL Control Structure)

{Find the mistake and correct it.}

program Lab7_3;

{Finds which value of n, which first puts the sum of even squares 2*2 + 4*4 + 6*6 + 8*8 +... + n*n over 500}

var n, sum: integer;

begin

sum := 0;

n := 2;

repeat

sum := sum + n*n;

n := n + 2;

until sum > 500;

writeln ('Sum first went over 500');

writeln ('when ', n, ' squared was added on');

writeln ('Sum was ', sum)

end.

10. (WHILE...DO)

{CAUTION:  Before running this program, you should write down on a piece of paper the following sentence:

ONE WAY TO TERMINATE AN INFINITE LOOP IS TO PRESS THE Break KEY

WHILE HOLDING DOWN THE Ctrl KEY; It MAY ALSO BE NECESSARY TO ENTER A     VALUE

IF THE COMPILER IS PAUSING AT A READLN STATEMENT.

This program is intended to find the sum of the units digits for an input list of 2-digit numbers. Thus, if you input

45,   58,   34,  38,  82,  and 1000 to exit

the output should be:  Sum is 27   (from 5 + 8 + 4 + 8 + 2)    }

program Lab7_4;

var k, numb, UnitsDig, sum: shortint;

begin

sum := 0;

write ('Enter a positive integer or 1000 to quit ');

while numb <> 1000 do

begin

UnitsDiG := numb mod 10;

sum := sum + UnitsDig;

write ('enter a positive integer or 1000 to quit ');

end;

writeln (sum);

end.

11. (Function)

{Fix the syntax errors in this program and then run it }

program Lab13_1;

var x, y : integer

sum  : integer;

begin

sum := 0;

begin {main}

write ('enter two numbers to add  ');

writeln (x,' + ',y,' = ', SUM);

end.

1.  Needs a semicolon after the declaration of x and y

2.  Needs a semicolon between the arguments in function header

3.  Needs semicolons after function header and function end

4.  Function type should be integer

5.  In the writeln SUM should be replaced by ADD_NUMBERS(x,y)

Also the body of the function could simply be

12. (Procedure)

{Run the following program using as input 34, 19, 20, 13, 15, 22. The intended output is the average of the even input numbers.}

program Lab11_1;

{Calls a procedure to find the sum of the even numbers. On return from the procedure it finds the average of the even numbers }

var sum, numb, ct : integer;

ans : char;

avg : real;

procedure SUMUP ( numb : integer; var  ct, sum : integer );

{counts the even numbers and sums them }

begin

if numb mod 2 = 0 then

begin

writeln ( numb, ' IS EVEN');

sum := sum+numb;

ct := ct+1

end

end;

begin {main}

sum := 0;

ct := 0;

repeat

write ('enter a number  ');

SUMUP ( numb, sum, ct );

write ('press y to continue, n to stop  ');