A two-dimensional array of size mxn, X[m][n], can be used to represent a terrain
ID: 3676052 • Letter: A
Question
A two-dimensional array of size mxn, X[m][n], can be used to represent a terrain of land where each element is elevation information (i.e. height in feet). For this problem, we want to determine number and location of peaks in the terrain, where a peak is a point that has lower elevations all around. We also want to determine the highest peak and its location. How to decide whether a location (i, j) is a peak: For 2-D array below, each element, except those on the boundaries (edges), has exactly 8 neighboring cells (e.g. Above, Below, Left, Right, and four Diagonals).
Diagonal Above Diagonal
Left (i, j) Right
Diagonal Below Diagonal
1) We decide that the value at (i, j) is a peak if the value at (i, j) is higher than the values at the “Above”, “Below”, “Left” and “Right” locations of (i, j). In other words, if the values of all 4 neighboring points are less than the value at (i,j), then location (i,j) is considered a peak.
2) The points on the edge cannot be a peak since we do not have elevation information on all 4 sides. In other words, you can ignore the elements at the boundary (edge) of the array. That is, start your element procession from row 1: elements (1,1) up to (1, n-2), continue until row m-2: elements (m-2, 1) to cell (m-2, n-2). Hint: in your program, “Above”, “Below”, “Left” and “Right” locations of (i, j) can be written in terms of i and j. For example, “Above” location of (i, j) is location (i-1, j)
Write a C program that:
a) Read the terrain data from Prob2.txt (you will create this text file). See sample code executions on the next page.
b) Determine and print all the peak values and the location of those peaks. Your program should examine every location (i, j) starting from i=1 and j=1 and decide whether that location is a peak using the criteria above.
c) Determine and print the highest peak and its location. Note: (nested) loops MUST be used to read from the text file in order to create 2-D array used in your program and the loops should be used to access elements of 2-D array for data processing. Otherwise, -20 pts. 4 Sample code execution #1: If the terrain data in Prob2.txt (again, you are to create this text file for your program) is 5039 5127 5238 5259 5248 5310 5299 5150 5392 5410 5501 5320 5820 5321 5290 5560 5490 5421 5530 5831 5210 5110 5429 5430 5411 5459 5630 5319 4920 5129 4921 5821 4722 4921 5129 5023 5129 4822 4872 4794 4862 4245 Display the following on the output screen: Terrain data 5039 5127 5238 5259 5248 5310 5299 5150 5392 5410 5501 5320 5820 5321 5290 5560 5490 5421 5530 5831 5210 5110 5429 5430 5411 5459 5630 5319 4920 5129 4921 5821 4722 4921 5129 5023 5129 4822 4872 4794 4862 4245 Peak 5501 at row 1 col 3 Peak 5560 at row 2 col 1 Peak 5831 at row 2 col 5 Peak 5821 at row 4 col 3 The maximum peak is 5831 at row 2 col 5
Sample code execution #2: If the terrain data in Prob2.txt (you are to create this text file for your program) is 5039 5127 5238 5259 5248 5310 5299 5150 5392 5410 5401 5320 5750 5321 5290 5560 5990 5421 5530 5731 5210 5110 5429 5430 5611 5459 5830 5319 4920 5829 4921 5321 4722 5921 5129 5023 5129 4822 4872 4794 4862 4245 Display the following on the output screen: Terrain data 5039 5127 5238 5259 5248 5310 5299 5150 5392 5410 5401 5320 5750 5321 5290 5560 5990 5421 5530 5731 5210 5110 5429 5430 5611 5459 5830 5319 4920 5829 4921 5321 4722 5921 5129 5023 5129 4822 4872 4794 4862 4245 Peak 5750 at row 1 col 5 Peak 5990 at row 2 col 2 Peak 5611 at row 3 col 3 Peak 5829 at row 4 col 1 Peak 5921 at row 4 col 5 The maximum peak is 5990 at row 2 col 2 Explanation, 1) Consider element[1][1] (=5392). Its 4 neighbors are 5127, 5150, 5560, 5410. Since 5392 is not the maximum (among the 4 neighbors), this point is not a peak. 2) Consider element[1][3] (=5501). Its 4 neighbors are 5259, 5410, 5421, 5320. Since 5501 is the maximum (among the 4 neighbors), this point is a peak
Explanation / Answer
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{ // since it is not mentioned in the question , what is the format of input file,
int row = 6, column = 7; // enter values of row and colums after observing the Prob2.txt file
int arr[row][column];
ifstream file_("Prob2.txt"); // opening the file
int number;
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < column; ++j)
{
file_ >> number;
arr[i][j] = number;
}
}
cout<< " ";
file_.close(); // closing the file
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < column; ++j)
{
cout << arr[i][j];
cout << " ";
}
cout << " ";
}
cout<< " ";
int max = arr[0][0];
int max_row, max_column;
for (int i = 1; i < row-1; ++i)
{
for (int j = 1; j < column-1; ++j)
{
if(arr[i][j] > arr[i-1][j] && arr[i][j] > arr[i+1][j] && arr[i][j] > arr[i][j-1] && arr[i][j] > arr[i][j+1])
{
cout << "Peak " << arr[i][j] << " at row " << i << " col "<< j <<endl;
if(arr[i][j] > max)
{ max = arr[i][j];
max_row = i;
max_column = j;
}
}
}
}
cout << "Maximum Peak is " << max << " at row " << max_row << " col "<< max_column <<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.