'How could I fix the undefined identifier error in the bool functions of my program? [closed]

I was doing an assignment for my class but I cannot see how to fix the undefined variable error for the N under grid[][N]. I was wondering if anyone here would mind showing me in the right direction? This program determines the locations of peaks in an elevation grid of data

#include <iostream> //Required for  cin, cout
#include <fstream> //Required for ifstream
#include <string> //Required for string
#include <cmath> //Required for calculations

using namespace std;

//Function prototypes
bool isPeak(const double grid[][N], int i, int j);

int main()
{
    //Object declaration
    int const N{ 25 };
    int nrows, ncols;
    double elevation[N][N];
    string filename;
    ifstream file1;

    //Prompt user for file name and open the named file
    cout << "Enter the name of the input file: \n";
    cin >> filename;
    file1.open(filename);
    if (file1.fail()) //Triggers if the file name is wrong or can't open the file
{
    cerr << "Error opening input file \n";
    exit(1);
}

file1 >> nrows >> ncols;
if (nrows > N || ncols >> N) //Triggers if the grid is too large
{
    cerr << "Grid is too large, adjust the input.";
    exit(1);
}
//Read the information from data file into array
for (int i = 0; i < nrows; ++i)
{
    for (int j = 0; j < ncols; ++j)
    {
        file1 >> elevation[i][j];
    }
}


//Determine and print peak locations
cout << "Top left point defined as row 0, column 0 \n";
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;
        }
    }
}

//Close file
file1.close();

//Exit program 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;
}

The grid data is a text file with the following points:

5039 5127 5238 5259 5248 5310 5299
5150 5392 5410 5401 5320 5820 5321
5290 5560 5490 5421 5530 5831 5210
5110 5429 5430 5411 5459 5630 5319
4920 5129 4921 5821 4722 4921 5129
5023 5129 4822 4872 4794 4862 4245
c++


Solution 1:[1]

As other users pointed out, your N is defined in the main() scope, not in global. But the definition of a isPeak() function is in global scope. Thus the compiler can not see the N you are requiring him to see.

In order to solve the problem you can just define N in global scope (outside the main)

#include <iostream> //Required for  cin, cout
#include <fstream> //Required for ifstream
#include <string> //Required for string
#include <cmath> //Required for calculations

using namespace std;

// here we define N
const int N = 25;

//Function prototypes
bool isPeak(const double grid[][N], int i, int j);
...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Levon Minasian