Thursday, October 11, 2018

day 13 assignment

/*?????????????????????????????????????????????????????????????*/
/* 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;
int numpeaks=0;
int numvalleys=0;
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=0; i<=nrows-1; i++)
for (j=0; j<=ncols-1; j++)
fscanf(grid,"%lf",&elevation[i][j]);
/* Determine and print peak locations. */
printf("Top left point defined as row 0, column 0 \n");
for (i=1; i<=nrows-2; i++)
for (j=1; j<=ncols-2; 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])){
numpeaks = numpeaks +1;
numvalleys =
printf("Peak at row: %d column: %d \n",i,j);
}
printf("peak count is %i \n", numpeaks);
fclose(grid); /* Close file. */
}
return 0; /* Exit program. */

}

No comments:

Post a Comment