# Homework 5

## Water Flow in a Channel

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.49A R2/3 S1/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 a channel flow cross-section (square feet);
• S is the slope of the channel (feet/foot);
• R is the hydraulic radius (feet). The hydraulic radius is the cross-sectional area divided by the wetted perimeter.

For rectangular channels, the hydraulic radius is:

• R = (D*W) /(2D + W)

where

• D is the depth of the channel flow
• W is the width of the channel

## Your  Problem

You are given a rectangular channel that is 16 feet wide and has walls that are 8 feet high. It has a slope of 0.0014 feet/foot and its roughness coefficient is 0.015. How deep will the water be when the water flow is 1,000 cubic feet per second?

To answer that question, you are to 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.

To make your task easier we have done some of the algorithmic design for you.

### High-Level Solution 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 8 feet (channel full).
3. Extract the user’s guess for the depth. If the user enters a number less than zero or greater than 8, print out an appropriate message and prompt again for a legitimate guess.
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.

Using this high-level design, we can produce the following more detailed design of the individual steps

### Detailed Design

1. Print appropriate text that describes the problem. An example output would be:
```This program computes the flow rate in units of cfs (cubic feet per
second) of water in a rectangular channel with a width of 16 feet, a
slope of 0.0014feet/foot, and a roughness coefficient of 0.015 for a
given depth of water (in feet) input by the user (0 <= depth <= 8). The
user should input an estimate of the water depth that will result in a
desired 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.```
2. Prompt the user to input a guess for the depth. An example prompt might be
```Input a guess of the water depth (0 <= depth <= 8) that will result in
a flow of 1,000 cubic feet per second:```
3. Input the user’s guess for the depth.
• If the user inputs a number outside the valid range, issue an appropriate message and repeat step two. If the user does not provide a guess, exit the program; otherwise continue at step four.
4. Compute the flow Q, the absolute difference between Q and 1,000, and the percentage error.
• Computing Q and the absolute difference are straightforward
• The percentage error is given by:  100.0 | Desired Flow - Q | / Desired Flow
5. 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. An example output in which you need to go back to step 2 because the error is greater than 0.1%
```Depth: 4.5000 feet
Flow: 541.6929 cfs
Target: 1000.0000 cfs
Difference: 458.3071 cfs
Error: 45.8307%```

An example final output could be

`At a water depth of 6.9900 feet the desired flow is obtained. `

## Requirements

• Print each of the values using four decimal places of precision. To get such output requires the use of the iomanip library. You may find manipulators setprecision() and fixed helpful.
• Compute R2/3 using the pow() function. Computer S1/2 using the sqrt() function. Compute the absolute value using the fabs() function. These three functions can be found in the cmath library.
• For those values that are numerical constants, use appropriately named constants. We might test your program on a different collection of values. In this regard, make sure your problem description displayed in step 1 uses the named constants. Output statements are ones in which programmers often forget to use their named constants.
• All variables and constants should be of type double.
• You must provide both an electronic and a printed copy of your program. Submit an electronic copy using the name flow.cpp. The electronic copy must be submitted prior to the beginning of lab 7. A submission under the wrong name will loose 10% of the grade. Attach a copy of the grading criteria to your solution when you hand it in.
• The printed copy must be an exact duplicate of the electronic copy. It is to be submitted at the start of lab 7.
• The assignment is pledged. You may receive help only from the CS101 teaching assistants, the ITC consultants, and the CS101 professors.
• Late homework assignments are not accepted.