ECE 1021

HOMEWORK #2

(Last Modified: 27 Nov 2010 )

PROGRAM A - Beyond Numeracy

Programming Problem #3.9 (p112).

To be turned in with pseudocode:

1) The problem states that the formula guarantees that the population for the next year will always be between 0 and one million (under the assumption that the current year's population is within that range). Analyze this claim - either prove that it is true or show the conditions under which it is false.

2) The problem states that, for at least some growth rates, this system settles into a stable value. Given the value of R (the rate), what is the equation the yields that stable value? Given the constraints specified on the rate in the problem, what is the maximum stable population (and what growth rate yields that maximum value). For what rate is the stable population zero? For what values of the rate are there no stable values?

To be turned in with source code:

3) As the problem states, for certain values of R this system oscillates between two values each year (after some number of iterations). One such value, 3.14, was given. Find another value and determine what the two population values are that the system oscillates between. Discuss these results in view of the answer to Question #2 above.

Hint: Don't try to find this other value by guessing. Ask yourself what must be true if the system is oscillating between two values. Then ask what is required for those conditions to exist.

PROGRAM B - Printing a Calendar

Programming Problem #4.6 (p151).

Some people might ask if the pseudocode for the second program (the calendar program) has to be "full blown" or if it is only necessary to state what changes are being made to the existing program (the one provided by the authors in their example). The answer is somewhere in between the two extremes. This is a good case where it is appropriate to have different levels of detail for different parts of the program. Start generating pseudocode for the program based on the existing code but only expand those branches that contain parts that need to be modified.

PROGRAM C - Extra Credit - Rounding a Floating Point Value

You do not need to turn in pseudocode for this problem.

General problem:

Given a floating point variable x (of type double) that is initialized to a value supplied by the user, round that value to a specified number of decimal places (an integer supplied by the user) and store the result in variable y (of type double). Your program should produce reasonable results for any possible value of x (e.g., positive, negative, enormous, miniscule).

For this program, you can assume that it only has to work when compiled using TurboC/C++ v4.5 (i.e., you can assume that the sizes and ranges of the various data types will not change).

Remember to divide and conquer. Get it to work for the types of situations that would come immediately to mind - rounding 145.928375 to 4 decimal places. That's the bulk of the points. Once you figure out a way to do that, then start asking questions like:

In the absence of specific guidance (as will frequently be the case in your engineering career) you will need to answer questions like these guided only by your own understanding of what is "reasonable" - and of course what you feel is reasonable may not be what your customer considers reasonable. In many instances your customer will have little to no idea of what is reasonable and you will need to sell them on why your answer is a reasonable one. The same holds here. You get to choose what you feel is reasonable behavior and then describe (in the comments in your source code file) what that behavior is and why it is reasonable - which usually means what its advantages are and what the disadvantages of other potential options is.

HINTS (especially for the more generic situations):

  1. You know that a floating point variable has a certain number of significant digits. Use that knowledge.

  2. Consider the relationship between the base ten logarithm of a number, the number of significant digits, and how many decimal places you want to round the result to.

  3. Think of a way to simplify the problem by reducing the generic problem to a "special case" and then figuring out how to solve the special case. For instance, if you figure out a way to round a number to the nearest integer, can you figure out a way to change a more generic case, like round a value to five decimal places, into a problem where the actual rounding involved is rounding a number to the nearest integer.