Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Lecture 7
0011 0010 1010 1101 0001 0100 1011
1
2
The man who regards his own life and that of his fellow
creatures as meaningless is not merely unhappy but hardly
fit for life
Albert Einstein
4
Lecture 6 review
0011 0010 1010 1101 0001 0100 1011
• Input buffering
• for-loop
1
2
4
Quiz #2 (15 minutes)
1) What is the output of the following
PROGRAM P1;
VAR sMyString : String[ 10 ];
0011
0010 1010
1101 0001 0100 1011
iMyInteger
: integer;
BEGIN
writeln( 'Enter a string and an
integer: ' );
read( sMyString );
readln( iMyInteger );
writeln( 'String = [', sMyString, ']' );
writeln( 'Integer = [', iMyInteger, ']' );
END.
If input was:
abc 10
10
2) What is the output of the following
PROGRAM P2;
VAR i, f, s : integer;
BEGIN
f := 300;
s := 300;
for i:= 1 to 3 do
begin
f := f - 50;
s := s - f;
writeln( i:4, f:6, s:6 );
end
end.
3) What are the ASCII codes for?
1
2
4
Fibonacci numbers
First = 1
0011 0010 1010 1101 0001 0100 1011
Second = 1
Fib[ N ] = Fib[ N - 1 ] + Fib[ N - 2 ]
Recursive definition, recurrence relation
1
2
4
Fibonacci numbers
Pseudo-code ideas
0011 0010 1010 1101 0001 0100 1011
- read the number of Fibonacci numbers to compute
- need to have a for-loop
- in the loop, figure out the next number
based on two previous numbers, so we need 3
variables:
FibN, FibNMinusOne and FibNMinusTwo
1
2
4
Fibonnaci numbers
More details on the loop
0011 0010 1010 1101 0001 0100 1011
FOR <something>
FibN := FibNMinusOne + FibNMinusTwo;
{ Here is the trick! }
FibNMinusTwo := FibNMinusOne;
FibNMinusOne := FibN;
{ end of for loop }
! Note the order of the update !
1
2
4
Fibonacci numbers
PROGRAM FirstNFibonacciNumbers;
{ Fib( N ) = Fib( N - 1 ) + Fib( N - 2 ) }
VAR iNumber, iFibN, iFibNMinusOne, iFibNMinusTwo, iSum, iLoopCount : integer;
BEGIN
write( 'Please
the 0001
number0100
of Fibonacci
0011 0010
1010enter
1101
1011numbers: ');
readln( iNumber );
iFibNMinusOne := 1;
iFibNMinusTwo := 1;
iSum := iFibNMinusOne + iFibNMinusTwo;
writeln( 'Fibonacci number 1 is 1' );
writeln( 'Fibonacci number 2 is 1' );
FOR iLoopCount := 3 TO iNumber DO
BEGIN
iFibN := iFibNMinusOne + iFibNMinusTwo;
writeln( 'Fibonacci number ', iLoopCount, ' is ',
iFibNMinusOne, ' + ', iFibNMinusTwo, ' = ', iFibN );
iSum := iSum + iFibN;
iFibNMinusTwo := iFibNMinusOne;
iFibNMinusOne := iFibN;
END;
writeln( 'The sum of first ', iNumber, ' Fibonacci numbers is ', iSum );
END.
1
2
4
Average revisited
PROGRAM Average;
VAR iSum, iCount, iGrade, iNumberOfGrades : integer;
BEGIN
0011 0010 1010 1101 0001 0100 1011
write( 'Please enter the number of grades: ' );
readln( iNumberOfGrades );
iSum := 0;
FOR iCount := 1 TO iNumberOfGrades DO
BEGIN
write( 'Enter grade #', iCount, ': ' );
readln( iGrade );
iSum := iSum + iGrade;
END;
1
2
4
writeln( 'Your average is ', (iSum / iNumberOfGrades):2:2 );
END.
Nested loops
• Why do we need this???
0011 0010 1010 1101 0001 0100 1011
• For example, compute class average!
1
2
4
Class average
PROGRAM ClassAverage;
VAR iGradeCount, iStudentCount, iGrade, iSum : integer;
iNumberOfGrades, iNumberOfStudents : integer;
BEGIN
0011 0010
1010 1101 0001 0100 1011
write( 'Please enter the number of students: ' );
readln( iNumberOfStudents );
write( 'Please enter the number of grades: ' );
readln( iNumberOfGrades );
iSum := 0;
FOR iStudentCount := 1 TO iNumberOfStudents DO
FOR iGradeCount := 1 TO iNumberOfGrades DO
BEGIN
write( 'Enter grade #', iGradeCount, ' of student #', iStudentCount, ': ' );
readln( iGrade );
iSum := iSum + iGrade;
END;
writeln( 'Class average is ',
(iSum / (iNumberOfGrades * iNumberOfStudents) ):2:2 );
END.
1
2
4
Triangle program
PROGRAM Triangle;
USES Crt;
VAR iNumberOfLines, iNumberOfColumns : integer;
0011 0010
1010 1101 iLoopCount2,
0001 0100 1011
iLoopCount1,
iNumberOfStars : integer;
BEGIN
ClrScr;
iNumberOfLines := 10;
iNumberOfColumns := 20;
FOR iLoopCount1 := 1 TO iNumberOfLines DO
BEGIN
iNumberOfStars := 2*iLoopCount1 - 1;
write( ' ': iNumberOfColumns - ( iNumberOfStars DIV 2 ) );
FOR iLoopCount2 := 1 TO iNumberOfStars DO
write( '*' );
writeln;
END
END.
1
2
4
Homework
0011 0010 1010 1101 0001 0100 1011
• Read Chapter 5 sections 4 and 10
• Program 3 Page 204 #15 a,b
due Wed. October 14
1
2
4
Programs from this lecture
0011 0010 1010 1101 0001 0100 1011
•
•
•
•
•
•
Formatting (final investigation)
Mixed input
Fibonacci numbers
Personal average
Class average
Triangle
1
2
4