Question: Write a C++ program to simulate a stream path. In this problem you can make the following assumptions 1. The elevation grid will be a

Write a C++ program to simulate a stream path.

In this problem you can make the following assumptions

1. The elevation grid will be a n row, m column grid of integers, where n and m are constant.

2. Once the stream origin location is specified, then the stream flows to whichever of the four adjacent locations in the grid represents the largest descent.

3. If more than one of the adjacent locations have the largest descent, then the water flows to whichever adjacent location appears first in the last item's list.

4. The process continues, with the stream flowing from location to location until it either reachers a location where none of the four adjacent locations has its elevation strictly less than that location, or until it reachesr the edge of the grid. (any row with index 0 or n-1 or any column with index 0 or n-1).

Write a C++ program that sets up a grid, and asks the user to input an initial location. If the input location is an invalid location, the program should print "Invalid location" to the screen. Otherwise the program should call a function:

bool moveToLowerElev(int elev[][m], int n, int m, int& r, int& c)

Given the row and column of the input location, this function should first check to see if that location is on the end of the grid. If it is, the function should return false. If it is not, the function should check the four adjacent locations. If none has an elevation strictly less than the current location, then the function should return false. Otherwise the function should return true, and return the row and column of the adjacent location with the leaset elevation through the reference parameters int& r and int& c.

If moveToLowerElev returns false, the main program should output "Path ends." Otherwise the program should print the new row and colum, as well as the elevation of the new location.

Have the user input a file name and read the array of data from that file (located at the end of this problem description). The program should also print out an error message if the file cannot be opened. The input file is layed out as follows: the irst line contains two integers, which are the number of rows and columns in the grid. The next rows are the elevation data.

Remember to close the file when finished.

Compute the entire stream path rather than just a single step. While the program computes the path, it should print out the locations and elevations along the path. The program must make several calls the moveToLoweElev.

Have a continuation loop starting after the file data has been read. Let the user deside if they want to continue.

Here is an example run of the program:

Please input the data file name: hw04b.dat

Input the origin location (row and column): 0 10

Invalid row or column

Enter another origin ocation (y/n)? y

Input the origin location (row and column): 0 2

Path ends

Enter another origin location (y/n)?: y

Input the origin location (row and column): 4 3

Row 3, column 3, elevation 2

Row 3, column 2, elevation 1

Row 2, column 2, elevation 0

Path ends

Enter another origin location (y/n)?: y

Input the origin location (row and column): 1 3

Row 0, column 3, elevaiton 0

Path ends

Enter another origin location (y/n)?: n

Here is the data file named hw04b.dat

7 5

0 0 0 0 0

0 1 2 3 4

0 2 0 2 0

2 3 1 2 4

3 4 3 4 5

4 5 5 2 6

3 3 3 2 1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!