Loop Invariance Worksheet #2

For each exercise, write the code and state the loop's pre-condition, post-condition, exit condition, and loop invariant.

  1. Given a vector of n elements, write a program fragment to reverse the elements in the vector without using another array.

  2. Write a program fragment to raise a real base to a positive integer power.

  3. Write a program fragment to read a list of integers stored one per line from a file and count the number of integers read.

  4. 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.

  5. 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