Lecture 26 Final review
Problem 1 (10 points) How many times would the FOR-DO loops beginning with the following statements be executed? If the loop is invalid, write invalid and give the reason. _____ a. FOR J:= 0 TO 0 DO _____ b. FOR J:= 1 DOWNTO 5 DO _____ c. FOR CH:= '#' TO 7 DO _____ d. FOR J:= '1' TO '5' DO _____ e. FOR CH:= 5 DOWNTO 3 DO
Problem 1 (10 points) How many times would the FOR-DO loops beginning with the following statements be executed? If the loop is invalid, write invalid and give the reason. __1___ a. FOR J:= 0 TO 0 DO __0___ b. FOR J:= 1 DOWNTO 5 DO _invalid_ c. FOR CH:= '#' TO 7 DO (types mismatch) __5___ d. FOR J:= '1' TO '5' DO __3___ e. FOR CH:= 5 DOWNTO 3 DO
Problem 2 (10 points) How many times would the write beginning in the following statements be executed? If the loop is invalid, write invalid. and give the reason. A) FOR J:= 3 DOWNTO 1 DO BEGIN FOR K:= 1 TO 3 DO BEGIN write('*') END; writeln END
Problem 2 (10 points) How many times would the write beginning in the following statements be executed? If the loop is invalid, write invalid. and give the reason. A) FOR J:= 3 DOWNTO 1 DO BEGIN The outer loop will be FOR K:= 1 TO 3 DO BEGINexecuted 3 times. write('*')The inner loop will be END; executed 3 times as well. Writeln Therefore, write will be END called 9 times (3*3) and writeln 3 times.
Problem 2 (10 points) How many times would the write beginning in the following statements be executed? If the loop is invalid, write invalid. and give the reason. B) FOR J:= 1 TO 3 DO BEGIN FOR K:= J TO 1 DO BEGIN write('*') END; writeln END
Problem 2 (10 points) How many times would the write beginning in the following statements be executed? If the loop is invalid, write invalid. and give the reason. B) FOR J:= 1 TO 3 DO BEGIN Again, the outer loop FOR K:= J TO 1 DO BEGIN will be executed 3 times. write('*') The inner one will be END;entered 1 time for J = 1, writeln0 times for J = 2, END0 times for J = 3. Therefore write will be called 1 time( ). Writeln will be called 3 times
Problem 2 (10 points) How many times would the write beginning in the following statements be executed? If the loop is invalid, write invalid. and give the reason. C) FOR J:= 4 TO 6 DO BEGIN FOR J:= 1 TO 5 DO BEGIN write('*') END; writeln END
Problem 2 (10 points) How many times would the write beginning in the following statements be executed? If the loop is invalid, write invalid. and give the reason. C) FOR J:= 4 TO 6 DO BEGINThe trick here is that both FOR J:= 1 TO 5 DO BEGIN loops have J as a counter. write('*')Be super-careful! END;Outer loop starts with J=4, writeln we go into inner one. J is reset to 1 and END inner loop runs from 1 to 5. Thus after we leave the inner loop for the first time, J = 5! For- loop autoincrementing will increment J to 6, then we enter the inner loop again, where J gets reset to 1 and the inner loop runs from 1 to 5. The J is 5 again, again it is incremented to 6 and the entire things repeats! Infinite loop!
Problem 3 (10 points) Indicate whether each of the following is valid or not. By valid we mean, whether it is executable code that can occur anywhere in a program. If it is not valid, explain why not. Note that a variable can be of any type a) True AND True
Problem 3 (10 points) Indicate whether each of the following is valid or not. By valid we mean, whether it is executable code that can occur anywhere in a program. If it is not valid, explain why not. Note that a variable can be of any type a) True AND True Yes, although it is quite silly :)
Problem 3 (10 points) Indicate whether each of the following is valid or not. By valid we mean, whether it is executable code that can occur anywhere in a program. If it is not valid, explain why not. Note that a variable can be of any type b) CASE Character OF 'a': writeln('one'); '2': writeln('Two'); '3': writeln('Three') END{CASE};
Problem 3 (10 points) Indicate whether each of the following is valid or not. By valid we mean, whether it is executable code that can occur anywhere in a program. If it is not valid, explain why not. Note that a variable can be of any type b) CASE Character OF 'a': writeln('one'); '2': writeln('Two'); '3': writeln('Three') END{CASE}; Sure. One good looking CASE statement.
Problem 3 (10 points) Indicate whether each of the following is valid or not. By valid we mean, whether it is executable code that can occur anywhere in a program. If it is not valid, explain why not. Note that a variable can be of any type C) One OR Two AND Three
Problem 3 (10 points) Indicate whether each of the following is valid or not. By valid we mean, whether it is executable code that can occur anywhere in a program. If it is not valid, explain why not. Note that a variable can be of any type C) One OR Two AND Three Yes. The variables One, Two and Three have to be of boolean type.
Problem 3 (10 points) Indicate whether each of the following is valid or not. By valid we mean, whether it is executable code that can occur anywhere in a program. If it is not valid, explain why not. Note that a variable can be of any type D) 5 > ‘4’
Problem 3 (10 points) Indicate whether each of the following is valid or not. By valid we mean, whether it is executable code that can occur anywhere in a program. If it is not valid, explain why not. Note that a variable can be of any type D) 5 > ‘4’ Can’t be. Types mismatch.
Problem 3 (10 points) Indicate whether each of the following is valid or not. By valid we mean, whether it is executable code that can occur anywhere in a program. If it is not valid, explain why not. Note that a variable can be of any type E) if x := 4 then writeln(‘ow’);
Problem 3 (10 points) Indicate whether each of the following is valid or not. By valid we mean, whether it is executable code that can occur anywhere in a program. If it is not valid, explain why not. Note that a variable can be of any type E) if x := 4 then writeln(‘ow’); Nope. If expects a boolean expression.
Problem 4 (5 points) (5 points) Complete the program segment that adds the odd numbers from 1 to N, where N is read into the program. So if N is 8, then the program would add 1, 3, 5, 7. writeln('enter your number'); Sum := _____ FOR J:= 1 TO ______ DO IF _______________________then Sum:=_________________
Problem 4 (5 points) (5 points) Complete the program segment that adds the odd numbers from 1 to N, where N is read into the program. So if N is 8, then the program would add 1, 3, 5, 7. writeln('enter your number'); Sum := 0; FOR J:= 1 TO _N_ DO IF ( J MOD 2 = 1 ) then Sum:= Sum + J;
Problem 5 (5 points) 5. (5 points) Complete the program segment that prints the value of the variable letter, if letter is a capital vowel. The vowels are (A, E, I, O, or U). IF letter_____________________then _________________
Problem 5 (5 points) 5. (5 points) Complete the program segment that prints the value of the variable letter, if letter is a capital vowel. The vowels are (A, E, I, O, or U). IF letter in [‘A’, ‘E’, ‘I’, ‘O’, ‘U’ ] then writeln( letter ); Note: we didn’t cover sets, so we could answer this with either CASE statement or IF-STATEMENT
Problem 6 (10 points) Complete the program segment that reads a group of numbers ending with sentinel of -9 (a sentinel is the number or character that indicates the end of the data) and places the even numbers in an array called A. For instance, if the input were: , the program would place 12 in A[1], 6 in A[2] and 16 in A[3]. VAR j, number:integer; A: ARRAY[1..100] OF integer; BEGIN writeln('type your numbers'); read(number); j:=________ WHILE number <> -9 DO BEGIN IF __________________________THEN BEGIN j:=________ A[____]:= Number; ________________ END
Problem 6 (10 points) VAR j, number:integer; A: ARRAY[1..100] OF integer; BEGIN writeln('type your numbers'); read(number); j:=0; WHILE number <> -9 DO BEGIN IF ( number mod 2 = 0 ) THEN { Test if even } BEGIN j:=j+1; { Point to next array position } A[ j ]:= Number; read( number ); { Read next number } END
Problem 7 (10 points) What does the following program produce? program pattern; const star = '*'; VAR j:integer; BEGIN for j:= 4 downto 1 do writeln(star:j, star:10 - (2 * j) ); for j:= 1 to 4 do writeln(star:j, star:10 - (2 * j) ); END.
Problem 7 (10 points) program pattern; const star = '*'; VAR j:integer; BEGIN * * for j:= 4 downto 1 do * * writeln(star:j, star:10 - (2 * j) ); * * for j:= 1 to 4 do * * writeln(star:j, star:10 - (2 * j) ); * * END. * * * * *
Problem 8 (10 points) **** ++++ *** +++ ** ++ Complete the program that produces the pattern: for j:=__________________DO begin ________ end
Problem 8 (10 points) Given the following procedures: procedure star(number:integer); var j:integer; begin for j:= 1 to number Do write('*'); writeln end; procedure plus(number:integer); var j:integer; begin for j:= 1 to number Do write('+'); writeln end; your goal is to produce the following pattern: **** ++++ *** +++ ** ++ Complete the program that produces the pattern: for j:=__________________DO begin ________ end
Problem 8 (10 points) Given the following procedures: procedure star(number:integer); var j:integer; begin for j:= 1 to number Do write('*'); writeln end; procedure plus(number:integer); var j:integer; begin for j:= 1 to number Do write('+'); writeln end; your goal is to produce the following pattern: **** ++++ *** +++ ** ++ Complete the program that produces the pattern: for j:= 4 DOWNTO 2 DO begin star( j ); plus( j ); end
Problem 9 (10 points) Given the following heading: procedure pattern(number: integer; symbol:char); complete the procedure that when activated by, for instance, pattern(5, '*'), would produce ***** and when activated by, for instance, pattern(3, '-') would produce --- procedure pattern(number:integer; symbol:char); VAR j:integer; BEGIN __________________ BEGIN ________________ END END;
Problem 9 (10 points) Given the following heading: procedure pattern(number: integer; symbol:char); complete the procedure that when activated by, for instance, pattern(5, '*'), would produce ***** and when activated by, for instance, pattern(3, '-') would produce --- procedure pattern(number:integer; symbol:char); VAR j:integer; BEGIN for J:= 1 to number do BEGIN write( symbol ); END END;
Problem 10 (20 points, do either A or B) A. Write a program that reads a sentence at least four characters long into an array of characters and prints every thing but the last three characters. The sentence must end with an asterisk. Thus if the sentence was hello there*, the program would print: hello the as output. In other words, the re* would be stored in the array but would not be printed.
Problem 10 (20 points, do either A or B) B. Write a program that reads a sentence that ends with a period, for instance, hello there. and prints the sentence with each letter e replaced with an asterisk. Thus in our example the program would print h*llo th*r*.
Optional problems on arrays Optional problems: page 473 #1, 2, 6, 13, 15
Problem 1 program t; var i : integer; x : array[1..5] of integer; begin for i:= 1 to 5 do { Init every element of array to be 2*index } x[i] := 2*i; for i:=1 to 5 do if ( i < 3 ) then { If index less than three modify in one way x[i] := 2*x[i] - x[i+2] else in another } else x[i] := -x[i]; for i:=1 to 5 do { Just print elements } writeln( x[ i ] ); -2, 0, -6, -8, -10 all on separate lines. end.
Problem 2 program prob2; type Vector = array[ ] of integer; var Ray, L : Vector; j : integer; procedure box( M : Vector; VAR N : Vector ); var k : integer; begin for k:=1 to 10 do N[ k ] := M[ k ] + k*k; { M[k] has squares of indexes, and we are adding squares of end; { of procedure Box } indexes, so we will have 2*index^2 sitting in the N (or Ray) } begin { Main } for j := 1 to 10 do L[ j ] := j*j;{ Init L to be index squared: 1, 4, 9, etc. } Box( L, Ray );{ Note that Ray is passed by reference } for j := 1 to 10 do writeln( 'j = ',j, ' Ray = ', Ray[ j ] ); 2, 8, 18, 32, 50, 72, …, 200 end.
Problem 6 Write a program that reads ages of up to 100 people and prints the number of people who are older than average and the number of people who are younger than average.
Problem 13 Write program that determines the largest element in an array X, and the value of its subscript, given VAR X : array[1..10] of integer;
Problem 15 Write a program that determines whether two (integer) arrays of the same size have the same elements.
GOOD LUCK ON ALL OF YOUR FINALS HAVE FUN (IN GENERAL)
Programs from this lecture Prob 10a from last year final Prob Prob 10b from last year finalfrom last year final Prob1 after chapter 10 Prob2 after chapter 10 Prob6 after chapter 10 Prob13 after chapter 10 Prob15 after chapter 10