Question
C++ Bear Limak is going to walk to the store. On his way to the store he likes to visit his friends and stop at
|
C++ Bear Limak is going to walk to the store. On his way to the store he likes to visit his friends and stop at various other interesting places. On his way back he is carrying heavy bags and therefore he walks straight home.
Limak's walk from his home to the store consists of M parts. In the i-th part (0-based index) Limak will walk a[i] meters in the direction indicated by the character dir[i]. That character is one of 'N', 'S', 'W', 'E', denoting North, South, West, and East, respectively.
On his way back home, Limak follows a straight line from the store to his home. You are given the description of Limak's way to the store: the int[] a with M integers and the String dir with M characters. Compute and return the total distance (in meters) Limak will walk on his way to the store and back. Note that the correct answer may be non-integer (see the first example below). | ||||||||||||
| |||||||||||||
Definition | |||||||||||||
|
| ||||||||||||
| |||||||||||||
| |||||||||||||
Notes | |||||||||||||
- | Your return value must have relative error smaller than 1e-6. In other words, your returned value x will be accepted only if abs(x - ans) ans * 10-6, where ans denotes the exact correct answer. | ||||||||||||
| |||||||||||||
Constraints | |||||||||||||
- | M will be between 1 and 50, inclusive. | ||||||||||||
- | a will have exactly M elements. | ||||||||||||
- | dir will have exactly M characters. | ||||||||||||
- | Each element in a will be between 1 and 50, inclusive. | ||||||||||||
- | Each character in dir will be one of 'N', 'S', 'W', 'E'. | ||||||||||||
| |||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
|
| ||||||||||||
1) | |||||||||||||
|
| ||||||||||||
2) | |||||||||||||
|
| ||||||||||||
3) | |||||||||||||
|
| ||||||||||||
4) | |||||||||||||
|
|
The class design is given as the following:
#ifndef BEAR_H
#define BEAR_H
#include
using namespace std;
class Bear
{
public:
double totalDistance(int a[], string dir);
private:
double x;
double y;
};
#endif
Here is what i got so far,
#include "Bear.h"
#include
#include
/* North: decrement y coordinate
South: increment y coordinate
East: increment x coordinate
West: decrement x coordinate
*/
double Bear::totalDistance(int a[], string dir)
{
/*
set both data members (x and y) to zeroes
declare a double variable total and initialize it to 0.0
for( i = 0 to dir.length() - 1)
{
if( the ith direction is north)
increment total by a[i]
decrement y by a[i]
else if(the ith direction is south)
increment total by a[i]
increment y by a[i]
else if(the ith direction is east)
increment total by a[i]
increment x by a[i]
else
increment total by a[i]
decrement x by a[i]
}
update total to (total + sqrt(x*x + y*y))
return total*/
}
void verifyResult(double result, double answer, int caseNum);
int main()
{
Bear teddy;
//Testing case 1
int test1[] = {1,3,3};
string dir1 = "NES";
double result1 = teddy.totalDistance(test1, dir1);
verifyResult(result1, 10.605551275, 1);
//Testing case 2
int test2[] = {10,10,10,10};
string dir2 = "NWSE";
double result2 = teddy.totalDistance(test2, dir2);
verifyResult(result2, 40.0, 2);
//Testing case 3
int test3[] = {10,10,10,15,8,20,5};
string dir3 = "NEWEWWE";
double result3 = teddy.totalDistance(test3, dir3);
verifyResult(result3, 90.8062484748657, 3);
//Testing case 4
int test4[] = {42};
string dir4 = "E";
double result4 = teddy.totalDistance(test4, dir4);
verifyResult(result4, 84.0, 4);
//Testing case 5
int test5[] = {10,40,40};
string dir3 = "NSE";
double result5 = teddy.totalDistance(test5, dir5);
verifyResult(result5, 140.0 , 5);
system("pause");
return 0;
}
void verifyResult(double result, double answer, int caseNum)
{
cout << " ------------------------------- ";
cout << "Testing case " << caseNum << "...... ";
cout << "Computed result = " << result << endl;
cout << "Expected result = " << answer << endl;
double diff = fabs(result - answer);
if(diff <= 0.001)
cout << "Passed. ";
else
cout << "Failed. ";
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started