Problem Description: Stream Paths Suppose you have a 2D array elev of integers t
ID: 3912209 • Letter: P
Question
Problem Description: Stream Paths Suppose you have a 2D array elev of integers that represent elevations on a grid. If a stream of water originates at the location with row index r and column index c, what would it do what path would it take and where would it end up? This problem is similar to problems in many areas, for example, in certain optimization techniques ("steepest descent") In this problem we'll make the tollowing assumptions 1. The elevation grid will be a n row, m column grid of integers, where n and m are constant ints. (Note: the example below uses a 7-row 5-column array; however, do not "hard code" 7 and 5 in your program. Instead use n and m so it will be easy to change the sizes if needed.) 2. Once the stream origin's location is specified, then the stream flows to whichever of the four adjacent locations in the grid represents the largest descent. The adjacent locations are the locations (i) in the same column, but with row index decreased by 1, (ii) in the same row, but with column index increased by 1, (ii) in the same column, but with row index increased by 1, and (iv) in the same row, but with column index decreased by 1 3. If more than one of the adjacent locations have the largest decrease, then the water Hows to whichever adjacent location appears first in the last item's list. 4. This process continues, with the stream flowing from location to location until it either reaches a location where none of the four adjacent locations has its elevation strictly less than that location, or until it reaches the edge of the grid (any row with index 0 or n-1, or any column with index 0 or m-1 This homework asks you to solve this problem in two parts Problem A: A Single Step (20 points) Write a C++ program that sets up a grid, and then asks a user to input an initial location If the input location is an invalid location (row less than 0 or greater than or equal to n, or column less than 0 or greater than or equal to m), the program should print "Invalid location". Otherwise the program should call a function you should write:Explanation / Answer
// code in c++ is
#include<bits/stdc++.h>
using namespace std;
const int m1=1000,n1=1000;
int elev[n1][m1];
bool moveToLowerElev(int n, int m, int *r,int *c)
{
int low_elev = INT_MAX, r1 = -1,c1 = -1;
if(*r-1>=0)
{
if(elev[*r-1][*c]<elev[*r][*c] && elev[*r-1][*c]<low_elev)
{
low_elev = elev[*r-1][*c];
r1=*r-1;
c1=*c;
}
}
if(*c+1<m)
{
if(elev[*r][*c+1]<elev[*r][*c] && elev[*r][*c+1]<low_elev)
{
low_elev = elev[*r][*c+1];
r1=*r;
c1=*c+1;
}
}
if(*r+1<n)
{
if(elev[*r+1][*c]<elev[*r][*c] && elev[*r+1][*c]<low_elev)
{
low_elev = elev[*r+1][*c];
r1=*r+1;
c1=*c;
}
}
if(*c-1>=0)
{
if(elev[*r][*c-1]<elev[*r][*c] && elev[*r][*c-1]<low_elev)
{
low_elev = elev[*r][*c-1];
r1=*r;
c1=*c-1;
}
}
if(r1 == -1 && c1 == -1)
return true;
else
{
*r = r1;
*c = c1;
cout<<"Row "<<*r<<" column "<<*c<<" elevation "<< elev[*r][*c]<<endl;
return false;
}
return false;
}
int main()
{
int n,m; // grid rows and columns
cout<<"Enter the dimensions of your matrix"<<endl;
cin>>n>>m;
//int elev[n1][m1];
cout<<"Input values to your grid [i,j], i is row and j is column"<<endl;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>elev[i][j];
}
}
bool x =true;
int r,c;
cout<<"Enter your initial location on the grid"<<endl;
cin>>r>>c;
if(r>=0 && c>=0 && r<n && c<m)
{
cout<<"Origin location(Row and column) "<< r<<" "<<c<<endl;
while(x==true)
{
x = moveToLowerElev(n,m,&r,&c);
}
}
else
{
cout<<"Invalid location"<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.