Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Class:

BearNSWE

Method:

totalDistance

Parameters:

int[], string

Returns:

double

Method signature:

double totalDistance(int a[], string dir)

(be sure your method is public)

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)

0) {1,3,3}

"NES"

Returns: 10.60555127546399

In total, Limak's walk consists of four parts:

He goes 1 meter North.

3 meters East.

3 meters South.

He goes straight to his home. The distance is sqrt(2*2+3*3) = sqrt(13) = 3.6055512755.

The total distance is 1 + 3 + 3 + 3.6055512755 = 10.6055512755.

1)

1) {10,10,10,10}

"NWSE"

Returns: 40.0

In this test case the store is located at the same place as Limak's home. Thus, the length of Limak's walk from the store back home is 0 meters.

2)

2) {10,10,10,15,8,20,5}

"NEWEWWE"

Returns: 90.8062484748657

It's possible that Limak visits some places more than once.

3)

3) {42}

"E"

Returns: 84.0

4)

4) {10,40,40}

"NSE"

Returns: 140.0

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke, David J. Auer

7th edition

133544621, 133544626, 0-13-354462-1, 978-0133544626

More Books

Students also viewed these Databases questions

Question

What is a residual plot?

Answered: 1 week ago