created 05/24/03; edited 11/09/2012

Chapter 41 Programming Exercises

Many of your programs from previous chapters can modified by changing while loops into the equivalent for loops. Some of these exercises are repeats of previous ones.

Exercise 1 — Sheep Herd Size

A breeding group of 20 bighorn sheep is released in a protected area in Colorado. It is expected that with careful management the number of sheep, N, after t years will be given by the formula:

N = 220/(1 + 10(0.83)t )

and that the sheep population will be able to maintain itself without further supervision once the population reaches a size of 80.

Write a program (using a for loop) that writes out the value of N for t starting at zero and going up to 25. How many years must the sheep heard be supervised?

Hint: don't calculate (0.83)t   "from scratch" each time the formula is used. Use a variable power that is multiplied by 0.83 in each iteration of the loop. What value should it be initialized to?

(Problem from Howard Anton, Calculus, 6th ed., p. 105. )

Click here to go back to the main menu.

Exercise 2 — Maximum of Integers in a File

Write a program that reads 5 integers from a file, computes their and their maximum and prints these values to the screen. Do this by modifying the summing program from the chapter. Insert a new int variable called max which you should initialize to the first value in the file. This will call for an extra set of input statements before the loop starts. To compute the maximum you will need an if statement nested inside the loop.

Click here to go back to the main menu.