Homework 13

#include <stdio.h>
#include <math.h>
#define N 25
#define FILENAME "grid1.txt"
int main(void)
{
/* Declare variables. */
int nrows, ncols, i, j, count = 0;
double elevation[N][N];
FILE *grid;
int hi, hj, li, lj, highest = 0, lowest = 10000;
/* 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]);

if (elevation[i][j] > highest)
{
highest = elevation[i][j];
hi = i;
hj = j;
}
else if (elevation[i][j] < lowest)
{
lowest = elevation[i][j];
li = i;
lj = 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]))
{
if ((elevation[i-1][j-1]>elevation[i][j]) &&
(elevation[i+1][j+1]>elevation[i][j]) &&
(elevation[i+1][j-1]>elevation[i][j]) &&
(elevation[i-1][j+1]>elevation[i][j]))
{
printf("Valley at row: %d column: %d \n",i,j);
printf("Distence from the lower left corner is %.2f ft\n",
(double)100*sqrt((nrows-i-1)*(nrows-i-1)+j*j));
count++;
}
}
else 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]))
{
if ((elevation[i-1][j-1]<elevation[i][j]) &&
(elevation[i+1][j+1]<elevation[i][j]) &&
(elevation[i+1][j-1]<elevation[i][j]) &&
(elevation[i-1][j+1]<elevation[i][j]))
{
printf("Peek at row: %d column: %d \n",i,j);
printf("Distence from the lower left corner is %.2f ft\n",
(double)100*sqrt((nrows-i-1)*(nrows-i-1)+j*j));
count++;
}
}
}

printf("Highest point at row: %d column: %d \n", hi, hj);
printf("Lowest point at row: %d column: %d \n", li, lj);
fclose(grid); /* Close file. */
}


return 0; /* Exit program. */

}



Comments

Popular posts from this blog

Homework 6

Another Day 23

Homework4