Question: C++ Help Program Flow Your program starts by executing the first 3 steps from Part 1: Read the data into a 2D vector Find min

C++ Help

Program Flow

Your program starts by executing the first 3 steps from Part 1:

  1. Read the data into a 2D vector

  2. Find min and max elevation to correspond to darkest and brightest color, respectively

  3. Compute the shade of gray for each cell in the map using shade of gray = 255* (elevation point - min) / (max - min )

  4. Create three parallel vectors and insert the shade of gray into all three vectors

  5. Step 4: Compute a greedy path

Write a function that calculates distance of the path created for a starting row and colors that path a particular color.

For a row, you color the cell you are at using the RGB color value passed in as three int parameters to the function, and compute your next position (following the specified movement rules) and color it, until you get to the last column. To choose the direction to move at each step, you look at the elevation data (that you read from a file) and compute the difference. Remember that you can be going downhill or uphill, therefore when you compute your vertical movement/distance, notice that it may end up negative, and you will need to compare the absolute value of vertical differences. As you color the path, you also have to compute the total distance. This is used to identify the best among all the greedy paths. You will need to return this distance at the end of the function.

The function will compute the greedy path starting at the given row and color it with the provided RGB color value, returning the total vertical movement. Every step you take should follow the greedy choice strategy we described (including what to do when there are ties.) As you move through the path, keep a running total of the total elevation change that would be 'experienced' by a person walking this path. Since we consider an elevation change the absolute value (i.e. going 'uphill' 10 meters is the same amount of change as going 'downhill' 10 meters), this running total will be non-decreasing and may end up being a pretty large positive number. The function should return this running total, so that you can compare it with other totals.

Greedy Function Requirements

  • The function declaration and definition should be placed in separate header and source files named functions.h and functions.cpp. You may place other functions there as well.

  • The function signature/prototype is:

    1. You do not need to add any parameters. Note you can use a vectors size() function to determine how many elements it has. So no need to add any extra parameters.

int colorPath(const vector>& elevations, vector>& r, vector>& g, vector>& b, int color_r, int color_g, int color_b, int start_row);

  • 1st parameter is the 2d vector of elevation data

  • 2nd parameter is 2d vector of red values for an RGB color for the corresponding row,col location in the elevation data

  • 3rd parameter is 2d vector of green values for an RGB color for the corresponding row,col location in the elevation data

  • 4th parameter is 2d vector of blue values for an RGB color for the corresponding row,col location in the elevation data

  • 5th parameter is the red value for the RGB path color. This color will replace the grey color of corresponding row,col locations of the pathn

  • 6th parameter is the green value for the RGB path color. This color will replace the grey color of corresponding row,col locations of the path

  • 7th parameter is the blue value for the RGB path color. This color will replace the grey color of corresponding row,col locations of the path

  • 8th parameter is index of the starting row to begin procedure on.

  • Returns the distance of the path. This is the sum of changes in elevation from one column to the next.

  1. Step 5: Call Function

Call the greedy algorithm function ( colorPath()) for each row (a loop works well). The greedy path should start on the west edge of the row (i.e. index 0) and color in red [RGB(252,25,63)] the corresponding cells in your output RGB data. While executing the loop, keep track of which row has the shortest path. If more than one path is shortest, then use the one with the lowest index.

  1. Step 6: Paint shortest path

Call the function again for the row with the shortest path using this green [RGB(31,253,13)] for the color.

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!