C++ include bonus please Please make the code easy to understand, I am trying to
ID: 3750686 • Letter: C
Question
C++ include bonus please
Please make the code easy to understand, I am trying to learn.
Objective In this assignment, students will load a topographic (elevation) data into a 2D array and write functions to compute the path that requires the least overall change in elevation, from a given starting point. Then, they will display this path to the user Main Topics Covered o Functions o File I/O Problem There are many contexts in which you want to know the most efficient way to travel over land. When traveling through mountains (let's say you're walking) perhaps you want to take the route that requires the least total change in elevation with each step you take call it the path of least resistance Given some topographic data, your program must calculate elevation change walk from one side of a map to the other. Subscripts 4 3011 2900 2852 2808 2791 2818 2 2972 2937 2886 2860 2830 2748 2937 2959 2913 2864 2791 2742 4 2999 2888 2986 2910 2821 2754 2909 2816 2893 2997 2962 2798Explanation / Answer
/ C++ program to compute the path that requires the least overall change in elevation, from a given starting point.
#include <iostream>
#include <fstream>
#include <cfloat>
#include <math.h> / fabs /
using namespace std;
// function to return the difference between two points in the array
double difference(double array[5][6], int r, int c, int r1, int c1){
return fabs(array[r][c]-array[r1][c1]);
}
int main() {
string filename;
int start_point;
double data[5][6]; // data array
int row,col;
// input of filename
cout<<" Enter the filename :";
cin>>filename;
ifstream f;
f.open(filename.c_str());
// if file can be opened
if(f.is_open())
{
ofstream out("out.txt"); // output file
f>>start_point; // read the starting point
row=0,col=0;
// read till the end of file
while(!f.eof())
{
f>>data[row][col];
col++;
if(col == 6)
{
row++;
col=0;
}
}
//start from leftmost point
int row = start_point;
int col = 1;
double diff = DBL_MAX;
int next_row,next_col;
// loop to find the least overall change path
while(true)
{
if(col == 6) // if end point reached then break from loop
break;
// print the current point in the file
out<<row<<","<<col<<":"<<data[row-1][col-1]<<endl;
// first compute the difference with the top right point
if(row > 1)
{
diff = difference(data,row-1,col-1,row-2,col);
next_row = row-1;
next_col = col+1;
}
// calculate the difference with bottom right point
if(row < 5)
{
// if difference is less or same as top right point then select bottom right
if(difference(data,row-1,col-1,row,col) <= diff)
{
diff = difference(data,row-1,col-1,row,col);
next_row = row+1;
next_col = col+1;
}
}
// calculate the difference with the right point
// if difference is less or same as bottom right then select right point
if(difference(data,row-1,col-1,row-1,col) <= diff)
{
diff = difference(data,row-1,col-1, row-1,col);
next_row = row;
next_col = col+1;
}
// make it as current point
row = next_row;
col = next_col;
}
// print the last point in the file
out<<row<<","<<col<<":"<<data[row-1][col-1];
out.close();
f.close();
}else
cout<<" Unable to open file "<<filename.substr(0,filename.length()-1)<<endl;
return 0;
}
//end of program
Output:
input1 :
3,1:2937 2,2:2937 3,3:2913 4,4:2910 5,5:2962 5,6:2798
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.