Upload
kathlyn-wood
View
260
Download
0
Embed Size (px)
Citation preview
42510011 0010 1010 1101 0001 0100 1011
Lecture 13
Midterm overview
When you know a thing, to hold that you know it, and when you do not know a thing, to allow that you do not
know it: this is knowledge
Conficus, Analects
4251
0011 0010 1010 1101 0001 0100 1011
Problem 1 (10 points)Describe in one sentence the difference between each of the following:
1)write and writelnwrite prints enclosed expression and leaves the cursor on the same
line, while writeln places cursor to the beginning of the next line (via CL/CR).
2)integer and realAn integer variables in Pascal represent whole numbers from
mathematics with system dependent bounded range, while real variables in Pascal represent decimal numbers from mathematics with system depended bounded range.
3)chr and ord
chr function returns a character given an ASCII code, while ord function returns an ASCII code of a given character.
4)writeln( ‘Hello’ ) and writeln( ‘’’Hello’’’ )First one prints Hello, second prints ‘Hello’.
e) Mr. Pascal and Mr. WirthPascal was a French mathematician after whom Wirth named the
language we are studding
4251
0011 0010 1010 1101 0001 0100 1011
Problem 2 (10 points)What is the purpose of each of the following menu items in TP
a)RunExecutes the program
b) CompileChecks the program for errors and converts to
machine codes
3)Step overExecute current line of code
4)Add watchStart observing a variable
5)Add breakpointMark a line on which the execution of program
should pause.
4251
0011 0010 1010 1101 0001 0100 1011
4251
0011 0010 1010 1101 0001 0100 1011
Problem 4 (5 points)
Find as many mistakes as you can in the following program
Progrm My Big MistakeBegin;
Readln( iMyInt );iAnswer := iAnswer / iMyInt;writeline( ‘Answer is ‘, ‘iAnswer’ );
enD
Mistakes:
1) Progrm should be spelled Program
2) No spaces in the program name are allowed
3) Missing semicolon after program name
4) Shouldn't have semicolon after begin
5) Need to declare variables
6) writeline should be spelled writeln
7) in the writeln, the iAnswer variable shouldn't be enclosed in quotes
8) There should be a period after enD
4251
0011 0010 1010 1101 0001 0100 1011
Problem 5 part a)
What would be printed by each of the following programs?
PROGRAM TRICKY1;VAR one, two, count : integer;BEGINone := 10;two := 0;for count := 1 to 2 doBEGIN two := two + one; for one := 1 to 2 do;ENDwriteln( two );END
Result(s)
12
One Two Count
init 10 0
for count := 1 to 2 do (I) 1
two := two + one 10
for one := 1 to 2 do (I)1
for one := 1 to 2 do (II) 2
for count := 1 to 2 do (II) 2
two := two + one 12
for one := 1 to 2 do (I)1
for one := 1 to 2 do (II) 2
writeln(two) produces 12
4251
0011 0010 1010 1101 0001 0100 1011
Problem 5 part b)
PROGRAM Tricky2;VAR count1, count2 : integer;BEGIN FOR count1 := 1 TO 10 DO FOR count2 := 1 TO count1 DO BEGIN writeln( count1 ); count1 := count2 * 10; END;END.
Result(s)
1
count1 count2
for count1 := 1 TO 10 do (I) 1for count2 := 1 TO count1 do (I)
1(note really count2 :=1 to 1)writeln( count1 ); produces 1count1 := count2 * 10; 10(inner loop exits)count1 is 10 so outer loop exits as well
4251
0011 0010 1010 1101 0001 0100 1011
Problem 6 (5 points)
How many times will each of the following loops be executed?
Loop Number of times
a) FOR I:=1 TO 101 DO 101
b) FOR I:= ‘a’ TO ‘c’ DO 3
c) FOR I:= ‘a’ TO ‘z’ DO 26
d) FOR I:= 1 DOWNTO 100 DO 0
e) FOR I:= ‘z’ DOWNTO ‘z’ DO 1
4251
0011 0010 1010 1101 0001 0100 1011
Problem 7 (10 Points)
Factorial of a number N (written as N!) is defined as a product of all numbers starting from 1 up to N. That is, N! = 1*2*3*…*(N-2)*(N-1)*N.
Write a Pascal program, which reads in a positive integer and outputs the value of N!
Program Factorial;var iNumber, iFactorial, iLoopCount : integer;begin
writeln( ‘Please enter a positive integer: ‘ );readln( iNumber );
iFactorial := 1;for iLoopCount := 1 to iNumber dobegin
iFactorial := iFactorial * iLoopCount;end;writeln( iNumber,’! = ‘, iFactorial );
end.
4251
0011 0010 1010 1101 0001 0100 1011
Problem 8 (10 points)
Write a Pascal program which reads in two positive integers a and b and then determines the remainder and the whole part when a is divided by b.
The program should output a = b*c + d, where c is the whole part and d is the remainder.
Program AgainDivAndMod;var a, b, c, d : integer;begin
writeln( ‘Enter a, b );readln( a, b );c := a div b;d := a mod b;writeln( a, ‘ = ‘, b, ‘*’, c, ‘+’, d );
end.
4251
0011 0010 1010 1101 0001 0100 1011
Problem 9 (15 points)
Write a Pascal program (utilizing for-loop), that prints every letter of the alphabet and its ‘opposite’. That is, the first few output lines should look like this:‘a’ – ‘z’‘b’ – ‘y’‘c’ – ‘x’etc.
PROGRAM OppositeLetter;
VAR c1 : char;
BEGIN
FOR c1 := 'a' TO 'z' DO
BEGIN
writeln( c1, ' - ', chr( ord( 'z' ) - ord( c1 ) + ord ( 'a' ) ) );
END;
END.
4251
0011 0010 1010 1101 0001 0100 1011
Problem 10 (15 points)Write a Pascal program, which prints out formatted multiplication table.The table’s rows and columns should run from 1 to 9. Make sure that your
table is formatted well. Use loops to do this problem.PROGRAM MultiplicationTable;
VAR iLoopCount1, iLoopCount2 : integer;
BEGIN
{ Column headings }
write( ' ' );
for iLoopCount1 :=1 to 9 do
begin
write( iLoopCount1:4 );
end;
writeln;
for iLoopCount1 := 1 to 9 do
begin
{ Row heading }
write( iLoopCount1 );
for iLoopCount2 := 1 to 9 do
begin
{ The value at iLoopCount1 column and iLoopCount2 row }
write( (iLoopCount1*iLoopCount2):4 );
end;
writeln;
end;
end.
4251
0011 0010 1010 1101 0001 0100 1011
Home work
• Start reading chapter 6 ( Sections 1,2 )