Day 14
For the homework,we had to create a program that finds the peaks and valleys from a data file called elevation. I got everything working, except the count of the peaks. My count was all over the place. Here's a copy of my code.
/* */
/* 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;
double elevation[N][N];
int total=0;
int 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");
int count=0;
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);
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("Valley at row: %d column: %d \n",i,j);
distance=(i*i+j*j)*.5*100;
printf("Distance from [0][0] is %i ft \n",distance);
//count++;
//printf("Number of Peaks is %i \n",count);
}
fclose(grid); /* Close file. */
}
return 0; /* Exit program. */
}
Here's a screencap of my output
For the lab we had to connect a speaker to a temperature sensor that makes a sound when the temperature changes. Here's a screencap of the code.
And here's a link to a video of the arduino in action
And a here's a pic
/* */
/* 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;
double elevation[N][N];
int total=0;
int 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");
int count=0;
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);
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("Valley at row: %d column: %d \n",i,j);
distance=(i*i+j*j)*.5*100;
printf("Distance from [0][0] is %i ft \n",distance);
//count++;
//printf("Number of Peaks is %i \n",count);
}
fclose(grid); /* Close file. */
}
return 0; /* Exit program. */
}
Here's a screencap of my output
For the lab we had to connect a speaker to a temperature sensor that makes a sound when the temperature changes. Here's a screencap of the code.
And here's a link to a video of the arduino in action
And a here's a pic



Comments
Post a Comment