CMP 131 Introduction to Computer Programming - PowerPoint PPT Presentation

About This Presentation
Title:

CMP 131 Introduction to Computer Programming

Description:

writeln('Fibonacci 1:', N2:5); FOR J := 2 TO Number 1 DO. BEGIN. N3 := N1 N2; ... '1'..'9' : writeln('Performing action', Option:2); 'Q' : ; ELSE ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 22
Provided by: violettaca
Learn more at: http://www.cs.cmu.edu
Category:

less

Transcript and Presenter's Notes

Title: CMP 131 Introduction to Computer Programming


1
CMP 131Introduction to Computer Programming
  • Violetta Cavalli-Sforza
  • Week 11

2
THIS WEEK
  • Today
  • Finish loops
  • Quiz results going over ..
  • Tomorrow (Tuesday) LAB 01
  • Work on homework 5
  • Wednesday Lecture
  • Start on File Processing
  • Thursday LAB 01
  • More work on homework 5

3
NEXT WEEK
  • Quiz 4
  • Wednesday May 30th
  • No make-up session but possibly start classes 15
    minutes early and end 15 minutes late (since you
    have ½ hour breaks)

4
Nested Selection and Repetition
  • Selection (IF-THEN-ELSE, CASE) and repetition
    (FOR-DO, WHILE-DO, REPEAT-UNTIL) are commonly
    occurring programming structures
  • They are frequently nested within each other
  • Selection inside repetition
  • Repetition inside selection

5
Selection Inside Repetition
  • We could have done Fibonaccis problem
    differently (not necessarily better) by
    incorporating the function in the loop

6
Previous Program Fibonacci
  • PROGRAM Fibonacci
  • CONST Number 20
  • VAR J , counter variable
  • N1, N2, N3 counter variable
  • integer
  • BEGIN
  • N1 0
  • writeln('Fibonacci 0', N15)
  • N2 1
  • writeln('Fibonacci 1', N25)
  • FOR J 2 TO Number 1 DO
  • BEGIN
  • N3 N1 N2
  • writeln('Fibonacci',J3,'',N35)
  • N1 N2
  • N2 N3
  • END
  • readln
  • END.

7
New Program Fibonacci
  • PROGRAM Fibonacci
  • CONST Number 20
  • VAR J , counter variable
  • N1, N2, N3 counter variable
  • integer
  • BEGIN
  • FOR J 0 TO Number-1 DO
  • IF J 0 THEN BEGIN
  • N1 0
  • writeln('Fibonacci 0 0')
  • END ELSE IF J 1 THEN BEGIN N2 1
  • writeln('Fibonacci 1 1')
  • END ELSE BEGIN
  • N3 N1 N2
  • writeln('Fibonacci',J3,'',N35)
  • N1 N2
  • N2 N3
  • END
  • readln

Repeated at each iteration of the loop!
8
Selection Inside Repetition
  • If unnecessary, avoid it. It makes for
    inefficient programs
  • May be necessary and part of the design, e.g.
    Selecting a menu option inside a loop.

9
  • PROGRAM MenuSelect
  • VAR Option char
  • BEGIN
  • REPEAT
  • print menu of options
  • writeln writeln('You have the following
    options')
  • writeln(' 1 to perform action 1')
  • writeln(' 2 to perform action 2')
  • writeln(' ....')
  • writeln(' 9 to perform action 9')
  • writeln(' Q to QUIT')
  • read user choice
  • write('Your choice ') readln(Option)
  • handle user choice
  • CASE Option OF
  • '1'..'9' writeln('Performing action',
    Option2)
  • 'Q'
  • ELSE
  • writeln('Unknown option', Option2,'. Try
    again.')

10
  • PROGRAM MenuSelect
  • VAR Option char
  • BEGIN
  • REPEAT
  • print menu of options
  • writeln writeln('You have the following
    options')
  • writeln(' 1 to perform action 1')
  • writeln(' 2 to perform action 2')
  • writeln(' ....')
  • writeln(' 9 to perform action 9')
  • writeln(' Q to QUIT')
  • read user choice
  • write('Your choice ') readln(Option)
  • handle user choice
  • IF ((Option gt 1) AND (Option lt 9)) OR
    (Option Q) THEN
  • CASE Option OF
  • '1'..'9' writeln('Performing action',
    Option2)
  • 'Q'
  • END CASE

11
Repetition Inside Selection
  • Example
  • Menu for printing out different types of pictures
  • ExampleMenu for accessing other menus(selection
    inside repetition repetition inside selection)

12
Selection Inside Repetition Repetition Inside
Selection
  • Example Menu for accessing other menus
  • Each menu is embedded inside a loop selection
    inside repetition
  • Each selection contains a menu within a loop
    repetition inside selection

13
  • REPEAT top-level menu
  • display options
  • read user choice
  • handle user choice CASE or IF-THEN-ELSE
    stmt
  • CASE Option OF
  • choice1 REPEAT
  • display options
  • read user choice
  • handle user choice
  • UNTIL exit from menu1
  • ...
  • choiceN REPEAT display options
  • read user choice
  • handle user choice
  • UNTIL exit from menuN
  • END CASE
  • UNTIL exit top level menu
  • END.

14
Self-Check 1
  • What is displayed by the following program
    fragment, assuming N is 5?
  • FOR I 1 TO N DO BEGIN FOR J 1
    TO I DO write() writeln
    END

15
Self-Check 2
  • What is displayed by the following program
    fragment, assuming M is 3 and N is 5?
  • FOR I N DOWNTO 1 DO BEGIN FOR J
    M DOWNTO 1 DO write()
    writeLn END

16
Self-Check 3
  • Show the output printed by the following nested
    loops
  • FOR I 1 TO 2 DO BEGIN
    writeln(Outer 5, I 5) FOR J 1 TO
    3 DO writeln(Inner 7, I 3, J 3)
    FOR K 2 DOWNTO 1 DO writeln(Inner 7, I
    3, K 3) END

17
Self-Check 4
  • Write a program fragment that, given an input
    value N, displays N rows in the form 1 2 ... N, 2
    3 ... N 1, and so forth.

18
  • PROGRAM Check
  • VAR I,J,N integer
  • BEGIN
  • write('What is N? ') read(N)
  • FOR I 1 TO N DO
  • BEGIN
  • FOR J I TO I N - 1 DO
  • write(J2)
  • writeln
  • END
  • readln
  • END.

19
Self-Check 5
  • Write a program that prints a nicely labeled
    multiplication table for the digits 1 through 9.

20
  • PROGRAM MultiplicationTable
  • VAR I,J integer
  • BEGIN
  • FOR I 1 TO 9 DO
  • BEGIN
  • FOR J 1 TO 9 DO
  • write(IJ3)
  • writeln
  • END
  • readln
  • END.

21
Quiz Results
  • Above Median 60.5 49.5 46.5 43 40 40 37.5
  • Median 35
  • Below Median 26.5 22 21.5 21 16.5 11.5 11
  • Mean 32.13333333
  • Total Points 80
  • N 15
Write a Comment
User Comments (0)
About PowerShow.com