1.Print a count of number of peaks on grid 2.Print the location of valleys inste
ID: 3548434 • Letter: 1
Question
1.Print a count of number of peaks on grid
2.Print the location of valleys instead of peaks. Assume that a valley is a point with an elevation lower than the foursurrounding elevations. Write boolean function names isvalley to be called by your program.
3. Modify the function isPeak() to use all eight neighboring points in determining a peak instead of only four neighboring points
/* Program 8_4*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
bool isPeak(const double grid[][N],int r, int c);
int main()
{
int const N = 25;
int nrows,ncols;
double elevation[N][N];
string filename;
ifstream file1;
cout<<"Enter the name of the input file. ";
cin>>filename;
file1.open(filename.c_str());
if(file1.fail())
{
cerr<<"Error opening input file ";
exit(1);
}
file1>>nrows>>ncols;
if(nrows>N||ncols>N)
{
cerr<<"Grid is too large, adjust program.";
exit(1);
}
for (int i=0; i<nrows-1;i++)
{
for(int j=0; j<ncols;++j)
{
file1>>elevation[i][j];
}
}
cout<<"Top left point defined as row 0,column 0 ";
for (int i=1;i<nrows-1;++i)
{
for (int j=1;j<ncols-1;++j)
{
if(isPeak(elevation,i,j))
{
cout<<"Peak at row:"<<i<<"column:"<<j<<endl;
}
}
}
file1.close();
return 0;
}
bool isPeak(const double grid[][N],int i,int j)
{
if((grid[i-1][j]<grid[i][j]))&&
(grid[i+1][j]<grid[i][j])&&
(grid[i][j-1]<grid[i][j])&&
(grid[i][j+1]<grid[i][j]))
return true;
else
return false;
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
bool isPeak(const double grid[][25],int r,int c);
bool isValley(const double grid[][25],int r,int c);
int main()
{
int const N = 25;
int nrows,ncols;
double elevation[N][N];
string filename;
ifstream file1;
cout<<"Enter the name of the input file. ";
cin>>filename;
file1.open(filename.c_str());
if(file1.fail())
{
cerr<<"Error opening input file ";
exit(1);
}
file1>>nrows>>ncols;
if(nrows>N||ncols>N)
{
cerr<<"Grid is too large, adjust program.";
exit(1);
}
for (int i=0; i<nrows-1;i++)
{
for(int j=0; j<ncols;++j)
{
file1>>elevation[i][j];
}
}
cout<<"Top left point defined as row 0,column 0 ";
int peaksCount=0;
for (int i=1;i<nrows-1;++i)
{
for (int j=1;j<ncols-1;++j)
{
if(isPeak(elevation,i,j))
{
peakCount++;
}
}
}
cout<<"There are "<<peakCount<<" peaks;"<<endl<<endl;
for (int i=1;i<nrows-1;++i)
{
for (int j=1;j<ncols-1;++j)
{
if(isValley(elevation,i,j))
{
cout<<"Valley at row:"<<i<<"column:"<<j<<endl;
}
}
}
file1.close();
return 0;
}
bool isPeak(const double grid[25][25],int i,int j)
{
if(((grid[i-1][j]<grid[i][j])) && (grid[i+1][j]<grid[i][j]) && (grid[i][j-1]<grid[i][j]) && (grid[i][j+1]<grid[i][j]) && (grid[i-1][j-1]<grid[i][j]) && (grid[i-1][j+1]<grid[i][j]) && (grid[i+1][j-1]<grid[i][j]) && (grid[i+1][j+1]<grid[i][j]))
{
return true;
}
else
{
return false;
}
}
bool isValley(const double grid[][25],int i,int j)
{
if(((grid[i-1][j]>grid[i][j])) && (grid[i+1][j]>grid[i][j]) && (grid[i][j-1]>grid[i][j]) && (grid[i][j+1]>grid[i][j]))
{
return true;
}
else
{
return false;
}
}
The above should be the modified code accomodating the asked changes.
Comment for any doubts.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.