Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem Description: Stream Paths Suppose you have a 2D array elev of integers t

ID: 3916189 • 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 following 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, (iii) 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 flows 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:

Given the row and a column for a current interior location, this function should first check if that location is on the edge 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 least elevation1 (using the rules above in case of a tie) through the reference parameters int& rand int& c.

If moveToLowerElev returns false, your main program should print “Path ends”. Other- wise, the program should print the new row and column, as well as the elevation at that new location.

Here is an example based on the following grid that has n = 7 rows and m = 5 columns.2

Here is an example run of the program with the origin location row 4 column 3. This location is surrounded by the values (reading clockwise starting at the location with row 3 and column 3) 2, 5, 2, and 3, so the function prints should out the following:

As usual, make sure your input and output follow the formatting, etc. in the example. Also, remember that the function definition of moveToLowerElev should be after main in your file. (You should follow the convention, in all your files that contain main plus other functions, that the other functions’ definitions should be after main.)

Explanation / Answer

// here is the full code

#include <iostream>

#include <fstream>
#include <cstdlib>
using namespace std;

int* read(string fileName) {
    ifstream in(fileName.c_str());
   
    if(!in) { // if file is not opened
        cout<<"FILE NOT FOUND!!!"<<endl;
        exit(0);
    }
    int r, c;
    in>>r>>c;
    int** grid = new int*[r];
   
    while(!in.eof()) { // while there is atleast one element
        cin>>i; // read it
        n++; // update number of elements in the file
    }
    in.seekg(0, in.beg); // seek the pointer to the beginning
   
    int *array = new int[n]; // create an array of size n  
    while(!in.eof()) {
        in>>array[i]; // read all the elements of file to array
    }
    in.close(); // close the file
   
    return array; // return the array
}

// function to find the sum of all the elements of an array
int sumof(int* a, int n) {
    int sum = 0;
    for(int i = 0; i < n; i++) {
        sum += a[i];
    }
    return sum;
}

// function to print the elements of an array backwords
void print_back(int* a, int n) {
    cout<<"Elements of Array in backword are : "<<endl;
    for(int i = n-1; i >= 0; i--) {
        cout<<a[i]<<" ";
    }
    cout<<endl;
}

int main() {
    int *elements = read("file.txt"); // read the elements from file
    int n = sizeof(elements)/sizeof(int); // find the size of the array
    print(elements, n); // print the elements of the array
   
    int sum = sumof(elements, n); // find the sum of the array
    cout<<"Sum of all the elements of Array is "<<sum<<endl;
   
    print_back(elements, n); // print the array backword
    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote