Thursday, October 11, 2018

day 13 10/9/18

Elevation problem
Modify the program to determine the following information for a grid of elevation data:
1. Print a count of the number of peaks in the grid.
2. Print the location of valleys instead of peaks. Assume that a valley is a point with an elevation lower than the four surrounding elevations.
3. Find and print the location and elevation of the highest point and the lowest point in the elevation data.
4. Assuming that the distance between points in a vertical and horizontal direction is 100 feet, give the location of the peaks in feet from the lower left corner of the grid.
5. Use all eight neighboring points, instead of only four neighboring points, to determine a peak.

/* This program determines the locations of peaks in an */
/* grid of elevation data. */
#include <stdio.h>
#include <math.h>
#define N 25
#define FILENAME "grid1.txt"
int main(void)
{
/* Declare variables. */
int nrows, ncols, i, j, sum = 0, sum2 = 0, Max = 0, Min = 0, x = 0, y = 0;
double elevation[N][N], distance = 0.;
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]) && (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("Peak at row: %d column: %d \n",i,j);
distance = sqrt((((j+1)*100)*((j+1)*100))+(((nrows-i)*100)*((nrows-i)*100)));
printf("The distance from the peak to the origin is %5.2f feet\n", distance);
sum += 1;
}
}
}
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])){
printf("Valley at row: %d column: %d \n",i,j);
sum2 += 1;
}
}
}
for (i=1; i<=nrows-1; i++){
for (j=0; j<=ncols-1; j++){
if (elevation[i][j] > Max){
Max = elevation[i][j];
x = i;
y = j;
}
}
}
printf("The max elevation is %d at row: %d column: %d\n", Max, x, y);
Min = elevation[1][0];
for (i=1; i<=nrows-1; i++){
for (j=0; j<=ncols-1; j++){
if (Min > elevation[i][j]){
Min = elevation[i][j];
x = i;
y = j;
}
}
}
printf("The min elevation is %d at row: %d column: %d\n", Min, x, y);
printf("The total amount of Peaks is %d\n", sum);
printf("The total amount of Valleys is %d\n", sum2);
fclose(grid); /* Close file. */
}
return 0; /* Exit program. */

}

Arduino isquaredc temp sensor hw


No comments:

Post a Comment