terrain mapping valleys


/* ---------------------------------------------------- */
/* This program determines the locations of peaks in an */
/* grid of elevation data. */
#include <stdio.h>
#define N 25
#define FILENAME "grid1.txt"
int main(void)
{
/* Declare variables. */
int nrows, ncols, i, j, peaks;
double elevation[N][N];
FILE *grid;

/* Read information from a data file. */
grid = fopen(FILENAME,"r");



if (grid == NULL)
{
printf("Error opening input file\n");
}

else
{
fscanf(grid,"%d %d",&nrows,&ncols);
for (i=1; i <= nrows - 1; i++)
{
//printf("1");
for (j=0; j <= ncols - 1; j++)
{
fscanf(grid,"%lf",&elevation[i][j]);
}
}
//printf("1");
/* Determine and print peak locations. */
printf("Top left point defined as row 0, column 0 \n");
for (i=2; i <= nrows - 1; i++)
{
for (j=2; j <= ncols - 1; j++)
{
if ((elevation[i-1][j]>elevation[i][j]) && (elevation[i+1][j]>elevation[i][j]) && (elevation[i][j-1]>elevation[i][j]) && (elevation[i][j+1]>elevation[i][j]))
{
printf("Valley at row: %i column: %i \n",i-1,j);
peaks ++;
}
}
}
printf("There are %i valley(s)", peaks);
}
fclose(grid); /* Close file. */

return 0; /* Exit program. */
}

Comments

Popular posts from this blog

DAY 17

day 19

Day 21