terrian mapping1


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

return 0; /* Exit program. */
}




//the program gets stuck in the first for loop

/* ---------------------------------------------------- */
/* 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];
FILE *grid;

/* Read information from a data file. */
grid = fopen(FILENAME,"r");

printf("1");

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]));
printf("Peak at row: %d column: %d \n",i,j);
}
}
}
fclose(grid); /* Close file. */

return 0; /* Exit program. */
}

Comments

Popular posts from this blog

DAY 17

day 19

Day 21