this is from a text book and i dont know why it isnt compiling? #include <iostre
ID: 3555348 • Letter: T
Question
this is from a text book and i dont know why it isnt compiling?
#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 filel;
cout << "Enter the name of the input file. ";
cin >> filename;
filel.open(filename.c_str());
if(filel.fail())
{
cerr << "Error opening input file ";
exit(1);
}
filel >> 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)
{
filel >> elevation[i][j];
}
}
cout << "Top left point defined as row 0, column 0 ";
for (int i=1; i<nrow-1, ++i)
{
for (int j=1; j<ncols-1; ++j)
{
if(isPeak(elevation, i, j))
{
cout << "Peak at row: " << i
<< " column: " << j << endl;
}
}
}
filel.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
Hi,
There were a few mistakes which I have corrected. Please find the correct code below:
CODE:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
bool isPeak(const double grid [][25], int r, int c);
int main()
{
int const N = 25;
int nrows, ncols;
double elevation [N][N];
string filename;
ifstream filel;
cout << "Enter the name of the input file. ";
cin >> filename;
filel.open(filename.c_str());
if(filel.fail())
{
cerr << "Error opening input file ";
exit(1);
}
filel >> 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)
{
filel >> 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;
}
}
}
filel.close();
return 0;
}
bool isPeak(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;
}
Errors :
1.You have to declare the array with a size. and
2.nrows variable was declared but it was used as nrow. the alphabet 's' was missing.
Hope it helped. Do rate:)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.