lab 13 temp sensor and grid code

#include <stdio.h>
#include <math.h>
#define N 25
#define FILENAME "grid.txt"
int main(void)
{
/* Declare variables. */
int nrows, ncols, i, j, count = 0;
double elevation[N][N];
FILE *grid;
int highi, highj, lowi, lowj, 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];
highi = i;
highj = j;
}
else if (elevation[i][j] < lowest)
{
lowest = elevation[i][j];
lowi = i;
lowj = 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", highi, highj);
printf("Lowest point at row: %d column: %d \n", lowi, lowj);
fclose(grid); /* Close file. */
}


return 0; /* Exit program. */


}

}


Comments

Popular posts from this blog

pointers code no lb

real time & EEPROM lab

interrup and sound lab