need help! Input terrain data using 2d array and finding its peak. The condition
ID: 3573530 • Letter: N
Question
need help! Input terrain data using 2d array and finding its peak. The conditions are below. Thanks alot, urgent pls.
Problem Solving Applied: Terrain Navigation Terrain navigation is a key component in the design of robotic spacecraft. Robotic spacecraft refers to spacecraft with no humans on board. These spacecraft can travel on land, such as the Mars Exploration Rovers, or above land as in the Mars Reconnaissance Orbiter. A robotic spacecraft contains numerous onboard computers that store terrain information for the area in which it is to be operated. By knowing at any time where it is (perhaps with the aid of a global positioning system [GPS] receiver, the vehicle can then select the best path to get to a designated spot. If the destination changes, the vehicle can refer to its internal maps to recompute the new path. The computer software that guides these vehicles must be tested over a variety of land formations and topologies. Elevation information for large grids of land is available in computer databases. One way of measuring the "difficulty" of a land grid with respect toExplanation / Answer
//Tested on linux,Ubuntu
/******************PeakElement.cpp**********************/
#include <iostream>
using namespace std;
int main() {
//variable declaration
int rows,columns;
int left,right,top,down,data;
//prompt for rows
std::cout<<"Please Enter number of rows: ";
std::cin>>rows;
//prompt for columns
std::cout<<"Please Enter number of columns:";
std::cin>>columns;
//creation of 2-d array dynamically
int** peak=new int*[columns];
for(int i=0;i<columns;i++) {
peak[i]=new int[rows];
}
//Prompt for user input
for(int i=0;i<rows;i++) {
for(int j=0;j<columns;j++) {
std::cout<<"(row,col):("<<i<<","<<j<<"):";
std::cin>>peak[i][j];
}
std::cout<<std::endl;
}
//logic written here
std::cout<<"Top Left point defined as row 0,clumn 0"<<std::endl;
for(int i=1;i<rows-1;i++) {
for(int j=1;j<columns-1;j++) {
data=peak[i][j];//main value
left=peak[i][j-1];//left value to main value
right=peak[i][j+1];//right value to main value
top=peak[i-1][j];//top value to main value
down=peak[i+1][j];//down value to main value
if((data>left)&&(data>right)&&(data>top)&&(data>down)){
std::cout<<"Peak at row: "<<i<<" column: "<<j<<std::endl;
}
}
}
//code for Releasing dynamically allocated memory
/*for(int i = 0; i < columns; i++) {
delete [] peak[i];
}
delete [] peak;*/
return 0;
}
/*********************output********************/
lalchand@lalchand:~/Desktop/chegg$ g++ PeakElement.cpp
lalchand@lalchand:~/Desktop/chegg$ ./a.out
Please Enter number of rows: 6
Please Enter number of columns:7
(row,col):(0,0):5039
(row,col):(0,1):5127
(row,col):(0,2):5238
(row,col):(0,3):5259
(row,col):(0,4):5248
(row,col):(0,5):5310
(row,col):(0,6):5299
(row,col):(1,0):5150
(row,col):(1,1):5392
(row,col):(1,2):5410
(row,col):(1,3):5401
(row,col):(1,4):5320
(row,col):(1,5):5820
(row,col):(1,6):5321
(row,col):(2,0):5290
(row,col):(2,1):5560
(row,col):(2,2):5490
(row,col):(2,3):5421
(row,col):(2,4):5530
(row,col):(2,5):5831
(row,col):(2,6):5210
(row,col):(3,0):5110
(row,col):(3,1):5429
(row,col):(3,2):5430
(row,col):(3,3):5411
(row,col):(3,4):5459
(row,col):(3,5):5630
(row,col):(3,6):5319
(row,col):(4,0):4920
(row,col):(4,1):5129
(row,col):(4,2):4921
(row,col):(4,3):5821
(row,col):(4,4):4722
(row,col):(4,5):4921
(row,col):(4,6):5129
(row,col):(5,0):5023
(row,col):(5,1):5129
(row,col):(5,2):4822
(row,col):(5,3):4872
(row,col):(5,4):4794
(row,col):(5,5):4862
(row,col):(5,6):4245
Top Left point defined as row 0,clumn 0
Peak at row: 2 column: 1
Peak at row: 2 column: 5
Peak at row: 4 column: 3
Thanks a lot.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.