ENGR 200 SPRING, 2017 A6: (using input/output files, if structures, one-dimensio
ID: 3863169 • Letter: E
Question
ENGR 200 SPRING, 2017 A6: (using input/output files, if structures, one-dimensional character arrays, one-dimensional and two-dimensional numeric arrays) DUE: March 20, 2017 at 11:59 PM, CDT (Mon Wed section) March 21, 2017 at 11:59 PM, CDT Tues/Thur sections and DEDP) POINTS: 65 INTRODUCTION: You are a Mechanical Engineer working for a company that designs autonomous air and ground vehicles. To follow and or avoid terrain, the vehicles need to have the capability to detect elevation differences. The systems that guide these vehicles need to be tested over a variety of terrain anomalies. You have determined that elevation information for large grids of the Earth's surface are often available in large computer databases. And one way to detect terrain anomalies of a grid sector is to the peak elevations in the grid. determine An input file called terrain contains elevation data for adesignated land grid region. The first record line contains the grid size, the second record line contains the grid name, the third record line contains the grid row letters, the fourth record line contains the grid column numbers, and the remaining record lines are the grid elevations ASSIGNMENT: Write a C read the grid name; 3) a one-dimensional character array Using a one-dimensional character a read the grid row letters: 4 Using a one- dimensional numeric array read the grid column numbers; 5) Using a two-dimension numeric array read the elevation data. Your program will: 1)Compute the average elevation; 2) Determine the minimum and maximum elevations; 3) Determine the grid row and grid column of the elevation: Determine the grid row and grid column of the maximum elevation: the number elevations. Note: peaks elevation when the four surrounding elevations are lower. Also, the elevations that are on the edges of be the grid cannot considered as peak elevations since they are not surround by elevations on all four sides. Your program will print to the computer screen and to an output file OUTPUT FORMAT: AREA-49-LAND XX ite Report elevation is xxx x Number of peaks in this grid Minimum elevation is xxx Maximum elevation is xxxExplanation / Answer
/* Preprocessor directives */
#include <stdio.h>
#include <math.h>
#define inputfile "u:\engr 200\terrain.txt"
#define outputfile "u:\engr 200\grid_report.txt"
/* Main function */
int main(void)
{
/* Declare and initialize variables */
int nrow,ncolumn,npeaks=0,i,j,xmax,ymax,xmin,ymin;
char name[25];
double average,maxelev,minelev,total,grid[10][10];
FILE *terrain, *report;
/* Open files */
terrain = fopen(inputfile,"r");
report = fopen(outputfile,"w");
/* Verify input file and read control number */
if (terrain == NULL)
{
printf(" Error opening input file.");
printf(" Press Enter to quit.");
getchar(); getchar();
return 0;
}
else
{
/* Read control numbers and table name */
fscanf(terrain,"%s",&name);
fscanf(terrain,"%i,%i",&nrow,&ncolumn);
/* Print main heading */
printf(" %25s "
" (Elevations in feet) "
" 0 1 2 3 4 5 6"
" 7 8 9 ",name);
fprintf(report," %25s "
" (Elevations in feet) "
" 0 1 2 3 4 5 6"
" 7 8 9 ",name);
/* Read values from the input file, print table using user defined func */
for(i=0;i <= nrow-1;i++)
{
printf(" %1i ",i);
fprintf(report," %1i ",i);
for(j=0;j <= ncolumn-1;j++)
{
fscanf(terrain,"%lf",&grid[i][j]);
printf("%5.1f ",grid[i][j]);
fprintf(report,"%5.1f ",grid[i][j]);
total = total + grid[i][j];
}
}
/* Find total number of peaks */
for(i=1;i <= nrow-2;i++)
for(j=1;j <= ncolumn-2;j++)
{
if(grid[i][j] > grid[i-1][j] && grid[i][j] > grid[i+1][j] &&
grid[i][j] > grid[i][j-1] && grid[i][j] > grid[i][j+1])
npeaks++;
}
/* Find minimum and maximum values and coordinates */
minelev = grid[i][j];
maxelev = grid[i][j];
for(i=0;i <= nrow-1;i++)
for(j=0;j <= ncolumn-1;j++)
{
if(grid[i][j] > maxelev)
{
maxelev = grid[i][j];
xmax = i;
ymax = j;
}
if(grid[i][j] < minelev)
{
minelev = grid[i][j];
xmin = i;
ymin = j;
}
}
}
/* Calculating average and printing the composite report */
average = total/(nrow*ncolumn);
printf(" COMPOSITE REPORT "
"Average elevation is %5.1f feet "
"Number of peaks in this grid = %i "
"Minimum elevation is %5.1f feet at grid %i,%i "
"Maximum elevation is %5.1f feet at grid %i,%i ",
average,npeaks,minelev,xmin,ymin,maxelev,xmax,ymax);
fprintf(report," COMPOSITE REPORT "
"Average elevation is %5.1f feet "
"Number of peaks in this grid = %i "
"Minimum elevation is %5.1f feet at grid %i,%i "
"Maximum elevation is %5.1f feet at grid %i,%i ",
average,npeaks,minelev,xmin,ymin,maxelev,xmax,ymax);
/* Close the files */
fclose(terrain);
fclose(report);
/* Exit program */
printf(" Press Enter to quit. ");
getchar();
getchar();
return 0;
}
/******************************************************************************/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.