1 / 23

Homework 2 (simple loops in TL) : Write and test the following function:

Homework 2 (simple loops in TL) : Write and test the following function: % Return the number of occurrences of character c in string s . func count_chars ( c : char, const ref s : string ) : int ... your code here …

trudy
Télécharger la présentation

Homework 2 (simple loops in TL) : Write and test the following function:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Homework 2 (simple loops in TL) : Write and test the following function: % Return the number of occurrences of character c in string s . funccount_chars ( c : char, const ref s : string ) : int ... your code here … NOTE: Proper comments, including the loop invariant and the termination argument, are an essential part of a complete solution! HINT: Read and understand the second example in the lecture “Arrays and quantifiers”. Also: recall the counting quantifier N . EXAMPLE: The statement TRACE count_chars( ‘a’, “Able was I ere I saw Elba” ) should print out the value 3 . NOTE: 1. Please do it just by yourself, don’t show your solution to, or discuss it with others. 2. Run your program and check that it does what you expect. 3. Please hand in your submission on paper, not via e-mail.

  2. Homework 2 (simple loops in TL) : Write and test the following function: % Return the number of occurrences of character c in string s . funccount_chars ( c : char, const ref s : string ) : int ... your code here … NOTE: Proper comments, including the loop invariant and the termination argument, are an essential part of a complete solution! HINT: Read and understand the second example in the lecture “Arrays and quantifiers”. Also: recall the counting quantifier N . EXAMPLE: The statement TRACE count_chars( ‘a’, “Able was I ere I saw Elba” ) should print out the value 3 . NOTE: 1. Please do it just by yourself, don’t show your solution to, or discuss it with others. 2. Run your program and check that it does what you expect. 3. Please hand in your submission on paper, not via e-mail.

  3. Homework 2 (simple loops in TL) : Write and test the following function: % Return the number of occurrences of character c in string s . funccount_chars ( c : char, const ref s : string ) : int ... your code here … NOTE: Proper comments, including the loop invariant and the termination argument, are an essential part of a complete solution! HINT: Read and understand the second example in the lecture “Arrays and quantifiers”. Also: recall the counting quantifier N . EXAMPLE: The statement TRACE count_chars( ‘a’, “Able was I ere I saw Elba” ) should print out the value 3 . NOTE: 1. Please do it just by yourself, don’t show your solution to, or discuss it with others. 2. Run your program and check that it does what you expect. 3. Please hand in your submission on paper, not via e-mail.

  4. Homework 2 (simple loops in TL) : Write and test the following function: % Return the number of occurrences of character c in string s . funccount_chars ( c : char, const ref s : string ) : int ... your code here … NOTE: Proper comments, including the loop invariant and the termination argument, are an essential part of a complete solution! HINT: Read and understand the second example in the lecture “Arrays and quantifiers”. Also: recall the counting quantifier N . EXAMPLE: The statement TRACE count_chars( ‘a’, “Able was I ere I saw Elba” ) should print out the value 3 . NOTE: 1. Please do it just by yourself, don’t show your solution to, or discuss it with others. 2. Run your program and check that it does what you expect. 3. Please hand in your submission on paper, not via e-mail.

  5. Homework 2 (simple loops in TL) : Write and test the following function: % Return the number of occurrences of character c in string s . funccount_chars ( c : char, const ref s : string ) : int ... your code here … NOTE: Proper comments, including the loop invariant and the termination argument, are an essential part of a complete solution! HINT: Read and understand the second example in the lecture “Arrays and quantifiers”. Also: recall the counting quantifier N . EXAMPLE: The statement TRACE count_chars( ‘a’, “Able was I ere I saw Elba” ) should print out the value 3 . NOTE: 1. Please do it just by yourself, don’t show your solution to, or discuss it with others. 2. Run your program and check that it does what you expect. 3. Please hand in your submission on paper, not via e-mail.

  6. Let a be an array with N elements. We want to establish: R : sumsq = ( S i : 0 =< i < N : a[ i ] * a[ i ] ) Our invariant will be P( k ) : (sumsq = ( S i : 0 =< i < k : a[ i ] * a[ i ] ) and k =< N. sumsq, k := 0, 0 ;% P( k ) while k != N do % P( k ) and k != N sumsq := sumsq + a[ k ] * a[ k ] ; % P( k ) violated k := k + 1 % progress, P( k ) restored od % P( k ) and k = N, hence R State spaces

  7. Homework 2 (simple loops in TL) : Write and test the following function: % Return the number of occurrences of character c in string s . funccount_chars ( c : char, const ref s : string ) : int ... your code here … NOTE: Proper comments, including the loop invariant and the termination argument, are an essential part of a complete solution! HINT: Read and understand the second example in the lecture “Arrays and quantifiers”. Also: recall the counting quantifier N . EXAMPLE: The statement TRACE count_chars( ‘a’, “Able was I ere I saw Elba” ) should print out the value 3 . NOTE: 1. Please do it just by yourself, don’t show your solution to, or discuss it with others. 2. Run your program and check that it does what you expect. 3. Please hand in your submission on paper, not via e-mail.

  8. Other useful quantifiers: sum, product, count. (S i : 0 =< i < N : a[ i ]) (P i : 0 =< i < N : a[ i ]) (N i : 0 =< i < N : a[ i ] = 0) The number of elements of a in the range [ 0, N ) whose value is 0. (Here, a[ i ] = 0 is just an example of a predicate and a[ i ] is just an example of an expression). NOTE: These are not predicates: their values are numerical. State spaces

  9. Let a be an array with N elements. We want to establish: R : sumsq = ( S i : 0 =< i < N : a[ i ] * a[ i ] ) Our invariant will be P( k ) : (sumsq = ( S i : 0 =< i < k : a[ i ] * a[ i ] ) and k =< N. sumsq, k := 0, 0 ;% P( k ) while k != N do % P( k ) and k != N sumsq := sumsq + a[ k ] * a[ k ] ; % P( k ) violated k := k + 1 % progress, P( k ) restored od % P( k ) and k = N, hence R We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) State spaces

  10. Let a be an array with N elements. We want to establish: R : sumsq = ( S i : 0 =< i < N : a[ i ] * a[ i ] ) Our invariant will be P( k ) : (sumsq = ( S i : 0 =< i < k : a[ i ] * a[ i ] ) and k =< N. sumsq, k := 0, 0 ;% P( k ) while k != N do % P( k ) and k != N sumsq := sumsq + a[ k ] * a[ k ] ; % P( k ) violated k := k + 1 % progress, P( k ) restored od % P( k ) and k = N, hence R We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) Our invariant will be WHAT? State spaces

  11. Let a be an array with N elements. We want to establish: R : sumsq = ( S i : 0 =< i < N : a[ i ] * a[ i ] ) Our invariant will be P( k ) : (sumsq = ( S i : 0 =< i < k : a[ i ] * a[ i ] ) and k =< N. sumsq, k := 0, 0 ;% P( k ) while k != N do % P( k ) and k != N sumsq := sumsq + a[ k ] * a[ k ] ; % P( k ) violated k := k + 1 % progress, P( k ) restored od % P( k ) and k = N, hence R We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) Our invariant will be P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) State spaces

  12. We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) Our invariant will be P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) In other words: count is equal to the number of occurrences of c in s at positions strictly lower than the current value of k. State spaces

  13. We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) Our invariant will be P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) Note that P( k ) and k = size( s ) implies R and this is what makes it a good candidate for an invariant! State spaces

  14. We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) Our invariant will be P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) Note that P( k ) and k = size( s ) implies R and this is what makes it a good candidate for an invariant! Also, our loop condition must be not k = size( s ) , i.e., k != size( s ) . State spaces

  15. We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) Our invariant will be P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) Note that P( k ) and k = size( s ) implies R and this is what makes it a good candidate for an invariant! Also, our loop condition must be not k = size( s ) , i.e., k != size( s ) . % establish P( k ) while k != size( s ) do ... od Why is k != size( s ) a better loop condition than k < size( s ) ? State spaces

  16. We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) Our invariant will be P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) Note that P( k ) and k = size( s ) implies R and this is what makes it a good candidate for an invariant! Also, our loop condition must be not k = size( s ) , i.e., k != size( s ) . % establish P( k ) while k != size( s ) do ... od Why is k != size( s ) a better loop condition than k < size( s ) ? We want to make it as weak as possible, to increase the likelihood of nontermination in case we mess things up (e.g., forget proper initialisation). WHY? State spaces

  17. We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) Our invariant will be P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) Note that P( k ) and k = size( s ) implies R and this is what makes it a good candidate for an invariant! Also, our loop condition must be not k = size( s ) , i.e., k != size( s ) . % establish P( k ) while k != size( s ) do ... od Why is k != size( s ) a better loop condition than k < size( s ) ? We want to make it as weak as possible, to increase the likelihood of nontermination in case we mess things up (e.g., forget proper initialisation). Remember: no result is much better than the wrong result!

  18. We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) Our invariant will be P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) Note that P( k ) and k = size( s ) implies R and this is what makes it a good candidate for an invariant! Also, our loop condition must be not k = size( s ) , i.e., k != size( s ) . % establish P( k ) while k != size( s ) do ... od How do we establish P( k ) ?

  19. We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) Our invariant will be P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) Note that P( k ) and k = size( s ) implies R and this is what makes it a good candidate for an invariant! Also, our loop condition must be not k = size( s ) , i.e., k != size( s ) . k , count := 0, 0 ; % P( k ) established while k != size( s ) do ... od How do we establish P( k ) ?

  20. We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) Our invariant will be P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) Note that P( k ) and k = size( s ) implies R and this is what makes it a good candidate for an invariant! Also, our loop condition must be not k = size( s ) , i.e., k != size( s ) . k , count := 0, 0 ; % P( k ) established while k != size( s ) do if s[ k ] = c then count := count + 1 % P( k ) violated fi; ... od

  21. We want to establish R : count = ( N i : 0 =< i < size( s ) : s[ i ] = c ) Our invariant will be P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) Note that P( k ) and k = size( s ) implies R and this is what makes it a good candidate for an invariant! Also, our loop condition must be not k = size( s ) , i.e., k != size( s ) . k , count := 0, 0 ; % P( k ) established while k != size( s ) do if s[ k ] = c then count := count + 1 % P( k ) violated fi; k : = k + 1 % progress, P( k ) re-established od

  22. % Invariant: P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) % Termination: size( s ) – k >= 0, decreases with each iteration. k , count := 0, 0 ; % P( k ) established while k != size( s ) do if s[ k ] = c then count := count + 1 % P( k ) violated fi; k : = k + 1 % progress, P( k ) re-established od

  23. % Return the number of occurrences of character c in string s . funccount_chars ( c : char, constref s : string ) : int begin varcount : int; var k : int; % Invariant: P( k ) : count = ( N i : 0 =< i < k : s[ i ] = c ) and k =< size( s ) % Termination: size( s ) – k >= 0, decreases with each iteration. k , count := 0, 0 ; % P( k ) established while k != size( s ) do if s[ k ] = c then count := count + 1 % P( k ) violated fi; k : = k + 1 % progress, P( k ) re-established od; return count end

More Related