CS121: Homework Project 2
Assigned February 7
Due before midnight February 24


In this assignment you may receive help only from the class teaching assistants, and the class professor. You may work in groups of up to two for this assignment. Members of a group must come from the same lab section. No sharing of code between groups.

Objective: The objective of this assignment is to gain experiece with library and user-defined functions.

When engineers build a storm drain, they have to know how deep the water will be when a known volume of water is running through it. Manning's equation describes the flow of water through a channel:

Q = (1.49 A R^2/3 S^1/2) / N

where Q is the flow of water in units of cubic feet per second, N is the roughness coefficient (unitless), A is the area of the channel (square feet), S is the slope of the channel (feet/foot), and R is the hydraulic radius (feet). The hydraulic radius is the cross-sectional area divided by the wetted perimeter. For square channels, the hydraulic radius is: R = (depth)(width) / (2 (depth) + width)

The Problem: You are given a square channel that is 15 feet wide and has walls that are 10 feet high. It has a slope of 0.0015 feet/foot and its roughness coefficient is 0.014. How deep will the water be when the water flow is 1,000 cubic feet per second?

To answer that question, design and implement a program that reacts to a user iteratively guessing a depth. For each depth guessed, the program should display the corresponding flow. The process of guessing a depth should continue until the depth guessed results in a computed flow that is within 0.1% of the target flow of 1,000 cubic feet per second. The calculation of a flow must be accomplished by writing a user-defined function SquareChannelFlow( ). Function SquareChannelFlow( ) should return a value of type double. Its parameters are: the channel area, the channel slope, the roughness coefficient, the water depth, and the width of the channel.

Preliminary Design:

1. Print out a text message that explains what the program computes and what the user is required to do.

2. Prompt the user for a guess of water depth. The range of legitimate guesses is bounded by 0 feet (channel empty) and by 10 feet (channel full). If the user enters a number less than zero or greater than 10, print out an appropriate message and prompt again for a legitimate guess.

3. Input the user's guess for the depth.

4. Compute, as a function of the depth, the flow Q, the difference between Q and 1,000, and the error percentage.

5. If the computed flow is within 0.1% of the target flow (1,000 cubic feet per second), print a message indicating that the user's guess was correct; if the error exceeds 0.1%, return to step two.

Detailed Design: Each step 1-4 should be implemented as a separate function.

Step one. Print appropriate text that describes the problem. An example output would be: This program computes the flow rate (in units of cubic feet per second) of water in a square channel with a width of 15 feet, a slope of 0.0015 feet/foot, and a roughness coefficient of 0.014 for a given depth of water (in feet) input by the user (0 <= depth <= 10). The user should input an estimate of the water depth that will result in a flow of 1,000 cubic feet per second. If the computed flow rate is too low, guess a higher depth; if too large, guess a lower depth. The program terminates when the depth guessed results in a calculated flow rate that is within 0.1% of the target flow rate.

Step two: Prompt the user to input a guess for the depth. Example:
Input a guess of the water depth (0 <= depth <= 10) that will result in a flow of 1,000 cubic feet per second:
If the user inputs a number outside the valid range, issue an appropriate message and repeat step two. After the user inputs a number in valid range, continue at step three.

Step three: Compute the flow Q, the absolute difference between Q and 1,000, and the percentage error. The percentage error is given by: ((TargetFlow - ComputedFlow) / TargetFlow ) x 100

Step four: Print the depth, the computed flow, the target flow, the absolute difference between the computed and target flows, and the percentage error between the computed and target flows. Example:
Depth: 6.0000 feet
Flow: 827.8129 cfs
Target: 1000.0000 cfs
Difference: 172.1871 cfs
Error: 17.2187 %

Print each of the values using four decimal places of precision. This may require the use of the IOMANIP library (see the text book for details.)

Step five: If the error is greater than 0.1% (in absolute value), return to step two. Otherwise, print a final message and exit. Example:
At a water depth of 6.9 feet the flow is 1000 cfs.

Hints:

1. Compute R^2/3 using the pow function. Computer S^1/2 using the sqrt function. Compute the absolute value using the abs or fabs function. You must include cmath library to access these three functions.

2. Include the iomanip library to gain access formatting commands.

3. For those values that are constants, use the appropriate C++ construct.

4. The variables used for the calculation of the flow should be of type double.