Loop Invariance Worksheet #2
EXERCISES
For each exercise, write the code and state the loop's pre-condition, post-condition, exit condition, and loop invariant.
- Given a vector of n elements, write a program fragment to reverse the elements in the vector without using another array.
- Write a program fragment to raise a real base to a positive integer power.
- Write a program fragment to read a list of integers stored one per line from a file and count the number of integers read.
- Write a program fragment to determine the number of consecutive letters that are the same in a sequence of characters to be read from a file.
- Write a program fragment that prompts for a print symbol and a height. It should then print a "Floyd's" triangle of the specified height using the specified print symbol. The output below illustrates a
height of 3 and a "*" symbol
*
**
***
Continue to: Unit 4
/ Prev
/ Next