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.

Slides:



Advertisements
Similar presentations
Topic Reviews For Unit ET156 – Introduction to C Programming Topic Reviews For Unit
Advertisements

Designing Algorithms Csci 107 Lecture 4. Outline Last time Computing 1+2+…+n Adding 2 n-digit numbers Today: More algorithms Sequential search Variations.
Objectives In this chapter, you will learn about:
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Repeating Actions While and For Loops
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
Designing Algorithms Csci 107 Lecture 4.
Chapter 6 - Visual Basic Schneider
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
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.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
read and learn from example loop programs develop modular program
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 10.
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Chapter 9 Control Structures.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Chapter 9 Repetition.
Categories of loops definite loop: Executes a known number of times.
REPETITION CONTROL STRUCTURE
Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code. Repetition in Pascal:
Introduction To Repetition The for loop
Lecture 6 Repetition Richard Gesick.
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Repetition-Counter control Loop
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Chapter 5 Repetition.
Building Java Programs
Chapter 9 Control Structures.
3.1 Iteration Loops For … To … Next 18/01/2019.
CHAPTER 6: Control Flow Tools (for and while loops)
Objectives You should be able to describe: The while Statement
Computer Science 1 For..do loop Dry Run Take notes on the For loop
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Building Java Programs
ICS103: Programming in C 5: Repetition and Loop Statements
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

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