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 ');
readln (loan);
write ('Enter Interest Rate(%) ');
readln (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 ');
readln (degrees);
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 ');
readln (BonusNumber);
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 ');
readln (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 ');
readln (amount);
write ('enter 1 for deposit or 2 for withdrawal ');
if code = 1 then
balance := balance + amount
else
balance := balance - amount;
readln (code);
write ('enter y to continue or n to stop ');
readln (ans)
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 ');
readln (numb);
while numb <> 1000 do
begin
UnitsDiG := numb mod 10;
sum := sum + UnitsDig;
write ('enter a positive integer or 1000 to quit ');
readln (numb) ;
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;
function ADD_NUMBERS (add1, add2 integer) : real,
begin
sum := 0;
sum := add1 + add2;
ADD_NUMBERS := sum;
end { function ADD_NUMBERS }
begin {main}
write ('enter two numbers to add ');
readln (x, y);
writeln (x,' + ',y,' = ', SUM);
end.
{ANSWERS:
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
ADD_NUMBERS := add1 + add2}
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 ');
readln (numb);
SUMUP ( numb, sum, ct );
write ('press y to continue, n to stop ');
readln (ans);
until ans = 'n';
avg := sum/ct;
writeln ('the average of the above EVEN numbers is ', avg )
end.
{HINT: Insert a writeln statement at the bottom of SUMUP to see what values numb, sum and ct have there. Also insert a writeln statement just before the calculation of avg}
{HINT: To get rid of the blue highlighting press CTRL-F2. Next insert a writeln statement at the bottom of the loop body. Can you see any reason why the exit condition is not satisfied?}