Elevation in 2D arrays (C++) DATA FILE: http://pastebin.com/DTQ9kenP Goals: Usin
ID: 3568653 • Letter: E
Question
Elevation in 2D arrays (C++)
DATA FILE: http://pastebin.com/DTQ9kenP
Goals: Using 2-dimensional arrays.
Problem: Navigation over a terrain can be an important concept. Usually you want to avoid high areas (peaks) and low areas (valleys). For this problem, you may assume the terrain that you want to navigate over may be represented as a 2-dimensional grid of cells. You are to determine the number of peaks and valleys in this terrain as well as give the location of these peaks and valley. A peak is defined as a cell for which all eight neighbors are lower than the cell being considered. A valley is defined as a cell for which all eight neighbors are higher than the cell being considered. An example grid is given below with some of the peaks in bold and some of the valleys underlined. Note that no cell on the perimeter may be considered a peak or valley.
Your main function should
Example Grid (6 rows with 8 columns). Peaks are given in bold and valleys are underlined. You could use this array as test data.
25 58 63 23 21 34 21 50
32 45 43 40 41 32 30 27
34 40 38 39 36 28 30 35
40 45 42 48 32 34 29 32
39 39 40 42 47 49 27 30
31 31 31 32 32 33 44 35
Carefully consider what each function is supposed to do, what it needs to receive from the function call and what it will return to the function call. Do not anything to the function that it does not need to complete its task.
PLEASE USE C++!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Explanation / Answer
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
using namespace std;
int rows = 0, cols = 0;
// INTRO:
// we don't want to use row 0 and column 0
// As they may confuse us. So we are creating 101,101
// array for 100, 100 element to use from 1 - 100
// Array to store elevation data
int elevMap[101][101];
// Array to store peak or valley data
// pvMap[x][y] == -1 if valley, 1 if peak else 0
int pvMap[101][101];
int avgElev = 0;
int maxGrad = 0;
int peaks = 0, valleys = 0;
void importData(string);
void locate_StorePV();
void calAvg_MaxGrad();
void createDataFile(string);
void printInfo(string);
int main()
{
importData("terrain.dat");
locate_StorePV();
calAvg_MaxGrad();
createDataFile("resultData.dat");
printInfo("resultData.dat");
// to wait for user input before closing
system("pause");
return 0;
}
void importData(string fileName)
{
ifstream file;
// type casting string to a file name
file.open(fileName.c_str());
int input;
int rowNo = 0, colNo = 1;
// In outer while loop, every line from the input file is fetched
// sequentially into variable "line"
string line;
// Fetching
while(getline(file, line))
{
stringstream lineStream(line);
// Read an integer at a time from the line
while(lineStream >> input)
{
// get rows and columns from line 0
if (rowNo == 0)
{
if(colNo == 1)
rows = input;
else cols = input;
}
else
{
elevMap[rowNo][colNo] = input;
}
colNo++;
}
colNo = 1;
rowNo++;
}
file.close();
}
void locate_StorePV()
{
int i, j;
// perimeter cells are not considered . So 2<i<rows
// similarly 2<j<cols
for(i=2; i<rows; i++)
{
for(j=2; j<cols; j++)
{
// Peaks
if((elevMap[i][j]<elevMap[i+1][j-1]) && (elevMap[i][j]<elevMap[i+1][j]) && (elevMap[i][j]<elevMap[i+1][j+1]) && (elevMap[i][j]<elevMap[i][j-1]) && (elevMap[i][j]<elevMap[i][j+1]) && (elevMap[i][j]<elevMap[i-1][j-1]) && (elevMap[i][j]<elevMap[i-1][j]) && (elevMap[i][j]<elevMap[i-1][j+1]))
{
pvMap[i][j] = -1;
peaks++;
}
// Valleys
else if((elevMap[i][j]>elevMap[i+1][j-1]) && (elevMap[i][j]>elevMap[i+1][j]) && (elevMap[i][j]>elevMap[i+1][j+1]) && (elevMap[i][j]>elevMap[i][j-1]) && (elevMap[i][j]>elevMap[i][j+1]) && (elevMap[i][j]>elevMap[i-1][j-1]) && (elevMap[i][j]>elevMap[i-1][j]) && (elevMap[i][j]>elevMap[i-1][j+1]))
{
pvMap[i][j] = 1;;
valleys++;
}
else pvMap[i][j] = 0;
}
}
}
void calAvg_MaxGrad()
{
int i, j;
int sum = 0;
int temp = 0;
int gradR, gradB;
// perimeter cells are not considered . So 2<i<rows
// similarly 2<j<cols
for(i=1; i<=rows; i++)
{
for(j=1; j<=cols; j++)
{
sum = sum + elevMap[i][j];
// Finding gradient of right and bottom cells only
// Escaping right most cells for gardL calculation
if(j<cols)
// Taking the absolute value of the gradient, gradient can't be negative
gradR = abs(elevMap[i][j+1] - elevMap[i][j]);
else gradR = 0;
// Escaping Bottom most cells for gardB calculation
if(i<rows)
gradB = abs(elevMap[i+1][j] - elevMap[i][j]);
else gradB = 0;
// assigning max of gradR and gardB to temp
temp = (gradR>gradB)?gradR:gradB;
// Assigning max of maxGrad and temp to maxGrad
maxGrad = (temp>maxGrad)?temp:maxGrad;
}
}
// cal avgElev by dividing sum of all elevation with total cells
avgElev = ((float) sum)/((float) rows*cols);
}
void createDataFile(string fileName)
{
// FILE STRUCTURE
// First line: total peaks and valleys
// from next line: location of peaks and valleys preceded with
// 1 : for peak and -1 : for valley
// Last line: average Elevation and maximum Gradient
int i, j;
ofstream finalOutput;
finalOutput.open(fileName.c_str());
if(finalOutput.is_open())
{
finalOutput << " " << peaks << " " << valleys << endl;
for(i=1; i<=rows; i++)
{
for(j=1; j<=cols; j++)
{
if(pvMap[i][j]>0)
{
finalOutput << " " << "1" << " " << i << " " << j << endl;
}
else if(pvMap[i][j]<0)
{
finalOutput << " " << "-1" << " " << i << " " << j << endl;
}
}
}
finalOutput << " " << avgElev << " " << maxGrad << endl;
}
finalOutput.close();
}
void printInfo(string fileName)
{
ifstream file;
// type casting string to a file name
file.open(fileName.c_str());
int input, infoLines = 0;
int rowNo = 0, colNo = 1;
// In outer while loop, every line from the input file is fetched
// sequentially into variable "line"
string line;
// Fetching
while(getline(file, line))
{
stringstream lineStream(line);
// Read an integer at a time from the line
while(lineStream >> input)
{
// get rows and columns from line 0
if (rowNo == 0)
{
if(colNo == 1)
{
cout << "Total Peaks: " << input << endl;
infoLines += input;
}
else
{
cout << "Total Valleys: " << input << endl;
cout << "------------------------------------" << endl;
cout << endl;
infoLines += input;
}
}
else
{
if(infoLines > 0)
{
if(colNo == 1)
{
if(input > 0) cout << "Peak at: ";
if(input < 0) cout << "Valley at: ";
}
else if(colNo == 2)
{
cout << " " << input ;
}
else if(colNo == 3)
{
cout << " " << input << endl;
infoLines--;
}
}
else
{
if(colNo == 1)
{
cout << "---------------------------------------------" << endl;
cout << "Average Elevation: " << input << endl;
}
else /*if(colNo == 2)*/
{
cout << "Maximum Gradient: " << input << endl;
}
}
}
colNo++;
}
colNo = 1;
rowNo++;
}
file.close();
}
/////////////////////////////// Run //////////////////////////////
Total Peaks: 66
Total Valleys: 36
------------------------------------
Valley at: 3 6
Peak at: 3 9
Valley at: 3 14
Valley at: 4 4
Valley at: 4 12
Valley at: 5 2
Peak at: 5 4
Valley at: 5 10
Peak at: 6 2
Valley at: 6 8
Valley at: 7 6
Valley at: 8 4
Peak at: 8 13
Valley at: 9 2
Valley at: 9 14
Peak at: 10 10
Peak at: 10 16
Peak at: 11 7
Peak at: 11 14
Peak at: 12 5
Valley at: 12 8
Peak at: 13 3
Peak at: 13 10
Valley at: 14 4
Peak at: 14 8
Valley at: 15 2
Peak at: 16 4
Peak at: 17 2
Valley at: 17 15
Valley at: 18 13
Peak at: 18 16
Valley at: 19 11
Peak at: 19 14
Valley at: 20 9
Valley at: 20 15
Valley at: 21 6
Valley at: 22 3
Peak at: 22 6
Valley at: 22 9
Peak at: 22 14
Valley at: 23 7
Peak at: 23 10
Peak at: 24 7
Peak at: 25 4
Valley at: 26 16
Valley at: 27 14
Valley at: 28 5
Valley at: 28 8
Valley at: 28 12
Peak at: 28 15
Valley at: 32 14
Valley at: 33 5
Valley at: 33 8
Valley at: 33 12
Peak at: 33 15
Valley at: 36 11
Valley at: 37 8
Valley at: 37 14
Valley at: 38 5
Valley at: 38 11
Valley at: 39 2
Valley at: 39 8
Peak at: 39 16
Valley at: 40 5
Valley at: 40 16
Valley at: 42 8
Valley at: 43 6
Peak at: 43 9
Valley at: 43 14
Valley at: 44 4
Peak at: 44 6
Valley at: 44 12
Valley at: 45 2
Peak at: 45 4
Valley at: 45 10
Valley at: 46 8
Valley at: 47 6
Peak at: 47 15
Valley at: 48 4
Peak at: 48 13
Valley at: 48 16
Valley at: 49 2
Peak at: 49 11
Valley at: 50 14
Valley at: 51 5
Valley at: 51 11
Valley at: 52 2
Valley at: 52 8
Peak at: 52 16
Valley at: 53 5
Valley at: 53 16
Valley at: 55 8
Valley at: 56 6
Peak at: 56 9
Valley at: 56 14
Valley at: 57 4
Valley at: 57 12
Peak at: 58 7
Peak at: 58 14
Peak at: 59 5
Valley at: 59 8
Peak at: 59 12
---------------------------------------------
Average Elevation: 30
Maximum Gradient: 26
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.