Question

North West Down Suppose two divers start at the surface and establish the following coordinate system: x is to the West, y is

I need the algorithm and the c++ program

0 0
Add a comment Improve this question Transcribed image text
Answer #1

DETAILED PROBLEM ANALYSIS

The problem is a simple 3D vector problem and can be used by making use of vector arithmetics

Let the vector D1 = (x1, y1, z1) relative to 0

and vector D2 = (x2, y2, z2) relative to 0

where x is the distance travelled east, y is the distance travelled north and z is the distance travelled down for both divers D1 and D2

These data can be stored as integers x,y,z in the form of a structure vector

Let O = (0,0,0) be the origin.

1. The distance between 2 points is given as sqrt((x2-x1)2 + (y2-y1)2 + (z2-z1)2)

where the points are (x1,y1,z1) and (x2,y2,z2)

if one the points is initial point, the length of the vector is obtained.

2. Vector V1 - Vector V2 represents another vector from point D2 to point D1.

This creates a relative location between 2 points. if D2 is origin, no change is observed.

If V2 is the diver D1 and V1 the diver D2 we get (D2 - D1) a vector from diver D1 to diver D2. If D1 Traversing according to this new vector, D1 will eventually reach D2.

Note that Negative values only imply opposite direction which needs changes like East --> West, North --> South and Down --> Up

ALGORITHMS

// Global Variables and their Initial values //

vector O, D1, D2, I1, I2

X(O) <- Y(O) <- Z(O) <- 0

X(D1) <- Y(D1) <- Z(D1) <- 0

X(D2) <- Y(D2) <- Z(D2) <- 0

X(I2) <- Y(I2) <- Z(I2) <- 0

X(I2) <- Y(I2) <- Z(I2) <- 0

procedure MOVE_V(V, x, y, z)
    X(V) <- X(V) + x
    Y(V) <- Y(V) + y
    Y(V) <- Y(V) + z
end MOVE_V

procedure DISTANCE(V1, V2)
    x <- X(V2)-X(V1)
    y <- Y(V2)-Y(V1)
    z <- Z(V2)-Z(V1)
    return sqrt(x*x + y*y + z*z)
end DISTANCE

procedure MINUS(V1, V2)
    V <- NEWVECTOR()
    X(V) <- X(V1) - X(V2)
    Y(V) <- Y(V1) - Y(V2)
    Z(V) <- Z(V1) - Z(V2)
    return V
end MINUS

C++ PROGRAM

/* CODE BEGINS HERE */

#include <iostream>
#include <math.h>

struct vector
{
   int X;
   int Y;
   int Z;
};

vector O = { 0,0,0 };

// Current Position of Divers
vector D1 = { 0,0,0 };
vector D2 = { 0,0,0 };

// Start Points of Divers
vector I1 = { 0, 0, 0 };
vector I2 = { 0, 0, 0 };

vector MOVE(vector &V, int x, int y, int z)
{
   V.X += x;
   V.Y += y;
   V.Z += z;

   return V;
}

vector MINUS(vector V1, vector V2)
{
   vector V = {V1.X - V2.X, V1.Y - V2.Y, V1.Z - V2.Z};
   return V;
}

double DISTANCE(vector V1, vector V2)
{
   int x = V1.X - V2.X;
   int y = V1.Y - V2.Y;
   int z = V1.Z - V2.Z;

   return sqrt(x*x + y*y + z*z);
}


int main()
{
   int x, y, z;

   std::cout << "Enter initial x coordinate of Diver 1(-ve if East): ";
   std::cin >> x;
   std::cout << "Enter initial y coordinate of Diver 1(-ve if South): ";
   std::cin >> y;
   MOVE(D1, x, y, 0); // Both Starts From Surface
   MOVE(I1, x, y, 0);

   std::cout << "Enter initial x coordinate of Diver 2(-ve if East): ";
   std::cin >> x;
   std::cout << "Enter initial y coordinate of Diver 2(-ve if South): ";
   std::cin >> y;
   MOVE(D2, x, y, 0);
   MOVE(I2, x, y, 0);


   std::cout << "\nEnter Movement of Diver 1 in West Direction (-ve if East): ";
   std::cin >> x;
   std::cout << "Enter Movement of Diver 1 in North Direction (-ve if South): ";
   std::cin >> y;
   std::cout << "Enter Movement of Diver 1 in Down Direction (-ve if Up): ";
   std::cin >> z;
   MOVE(D1, x, y, z);

   std::cout << "\nEnter Movement of Diver 2 in West Direction (-ve if East): ";
   std::cin >> x;
   std::cout << "Enter Movement of Diver 2 in North Direction (-ve if South): ";
   std::cin >> y;
   std::cout << "Enter Movement of Diver 2 in Down Direction (-ve if Up): ";
   std::cin >> z;
   MOVE(D2, x, y, z);


   std::cout << "\n\nDistance Of Diver 1 From Starting Point = " << DISTANCE(D1, I1);


   vector path = MINUS(D2, D1);
   std::cout << "\n\nPath for Diver 1 to Reach D2 : \n";
   // If zero then no movement is required
   if (path.X > 0)
       std::cout << path.X << " towards West\n";
   else if (path.X > 0)
       std::cout << path.X*-1 << " towards East\n";
   if (path.Y > 0)
       std::cout << path.Y << " towards North\n";
   else if (path.Y > 0)
       std::cout << path.Y * -1 << " towards South\n";
   if (path.Z > 0)
       std::cout << path.Z << " towards Down\n";
   else if (path.Z > 0)
       std::cout << path.Z * -1 << " towards Up\n";

   std::cout << "\nDistance Diver 1 must travel to reach D2 = " << DISTANCE(path, O) << std::endl;

// Or DISTANCE(D1,D2)//

   return 0;
}

/* CODE ENDS HERE */

Microsoft Visual Studio Debug Console Enter initial x coordinate of Diver 1(-ve if East): 1 Enter initial y coordinate of Div


answered by: ANURANJAN SARSAM
Add a comment
Answer #2

DETAILED PROBLEM ANALYSIS

The problem is a simple 3D vector problem and can be used by making use of vector arithmetics

Let the vector D1 = (x1, y1, z1) relative to 0

and vector D2 = (x2, y2, z2) relative to 0

where x is the distance travelled east, y is the distance travelled north and z is the distance travelled down for both divers D1 and D2

These data can be stored as integers x,y,z in the form of a structure vector

Let O = (0,0,0) be the origin.

1. The distance between 2 points is given as sqrt((x2-x1)2 + (y2-y1)2 + (z2-z1)2)

where the points are (x1,y1,z1) and (x2,y2,z2)

if one the points is initial point, the length of the vector is obtained.

2. Vector V1 - Vector V2 represents another vector from point D2 to point D1.

This creates a relative location between 2 points. if D2 is origin, no change is observed.

If V2 is the diver D1 and V1 the diver D2 we get (D2 - D1) a vector from diver D1 to diver D2. If D1 Traversing according to this new vector, D1 will eventually reach D2.

Note that Negative values only imply opposite direction which needs changes like East --> West, North --> South and Down --> Up

ALGORITHMS

// Global Variables and their Initial values //

vector O, D1, D2, I1, I2

X(O) <- Y(O) <- Z(O) <- 0

X(D1) <- Y(D1) <- Z(D1) <- 0

X(D2) <- Y(D2) <- Z(D2) <- 0

X(I2) <- Y(I2) <- Z(I2) <- 0

X(I2) <- Y(I2) <- Z(I2) <- 0

procedure MOVE_V(V, x, y, z)
    X(V) <- X(V) + x
    Y(V) <- Y(V) + y
    Y(V) <- Y(V) + z
end MOVE_V

procedure DISTANCE(V1, V2)
    x <- X(V2)-X(V1)
    y <- Y(V2)-Y(V1)
    z <- Z(V2)-Z(V1)
    return sqrt(x*x + y*y + z*z)
end DISTANCE

procedure MINUS(V1, V2)
    V <- NEWVECTOR()
    X(V) <- X(V1) - X(V2)
    Y(V) <- Y(V1) - Y(V2)
    Z(V) <- Z(V1) - Z(V2)
    return V
end MINUS

C++ PROGRAM

/* CODE BEGINS HERE */

#include <iostream>
#include <math.h>

struct vector
{
   int X;
   int Y;
   int Z;
};

vector O = { 0,0,0 };

// Current Position of Divers
vector D1 = { 0,0,0 };
vector D2 = { 0,0,0 };

// Start Points of Divers
vector I1 = { 0, 0, 0 };
vector I2 = { 0, 0, 0 };

vector MOVE(vector &V, int x, int y, int z)
{
   V.X += x;
   V.Y += y;
   V.Z += z;

   return V;
}

vector MINUS(vector V1, vector V2)
{
   vector V = {V1.X - V2.X, V1.Y - V2.Y, V1.Z - V2.Z};
   return V;
}

double DISTANCE(vector V1, vector V2)
{
   int x = V1.X - V2.X;
   int y = V1.Y - V2.Y;
   int z = V1.Z - V2.Z;

   return sqrt(x*x + y*y + z*z);
}


int main()
{
   int x, y, z;

   std::cout << "Enter initial x coordinate of Diver 1(-ve if East): ";
   std::cin >> x;
   std::cout << "Enter initial y coordinate of Diver 1(-ve if South): ";
   std::cin >> y;
   MOVE(D1, x, y, 0); // Both Starts From Surface
   MOVE(I1, x, y, 0);

   std::cout << "Enter initial x coordinate of Diver 2(-ve if East): ";
   std::cin >> x;
   std::cout << "Enter initial y coordinate of Diver 2(-ve if South): ";
   std::cin >> y;
   MOVE(D2, x, y, 0);
   MOVE(I2, x, y, 0);


   std::cout << "\nEnter Movement of Diver 1 in West Direction (-ve if East): ";
   std::cin >> x;
   std::cout << "Enter Movement of Diver 1 in North Direction (-ve if South): ";
   std::cin >> y;
   std::cout << "Enter Movement of Diver 1 in Down Direction (-ve if Up): ";
   std::cin >> z;
   MOVE(D1, x, y, z);

   std::cout << "\nEnter Movement of Diver 2 in West Direction (-ve if East): ";
   std::cin >> x;
   std::cout << "Enter Movement of Diver 2 in North Direction (-ve if South): ";
   std::cin >> y;
   std::cout << "Enter Movement of Diver 2 in Down Direction (-ve if Up): ";
   std::cin >> z;
   MOVE(D2, x, y, z);


   std::cout << "\n\nDistance Of Diver 1 From Starting Point = " << DISTANCE(D1, I1);


   vector path = MINUS(D2, D1);
   std::cout << "\n\nPath for Diver 1 to Reach D2 : \n";
   // If zero then no movement is required
   if (path.X > 0)
       std::cout << path.X << " towards West\n";
   else if (path.X > 0)
       std::cout << path.X*-1 << " towards East\n";
   if (path.Y > 0)
       std::cout << path.Y << " towards North\n";
   else if (path.Y > 0)
       std::cout << path.Y * -1 << " towards South\n";
   if (path.Z > 0)
       std::cout << path.Z << " towards Down\n";
   else if (path.Z > 0)
       std::cout << path.Z * -1 << " towards Up\n";

   std::cout << "\nDistance Diver 1 must travel to reach D2 = " << DISTANCE(path, O) << std::endl;

// Or DISTANCE(D1,D2)//

   return 0;
}

/* CODE ENDS HERE */

Microsoft Visual Studio Debug Console Enter initial x coordinate of Diver 1(-ve if East): 1 Enter initial y coordinate of Div

Add a comment
Know the answer?
Add Answer to:
I need the algorithm and the c++ program North West Down Suppose two divers start at...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • URGENT!!!!! I need to develop a C++ program that uses a recursive function to solve this...

    URGENT!!!!! I need to develop a C++ program that uses a recursive function to solve this maze problem: (No classes please!!!!) A robot is asked to navigate a maze. It is placed at a certain position (the starting position) in the maze and is asked to try to reach another position (the goal position). Positions in the maze will either be open or blocked with an obstacle. Positions are identified by (x,y) coordinates. At any given moment, the robot can...

  • I need help with my very last assignment of this term PLEASE!!, and here are the instructions: After reading Chapter T...

    I need help with my very last assignment of this term PLEASE!!, and here are the instructions: After reading Chapter Two, “Keys to Successful IT Governance,” from Roger Kroft and Guy Scalzi’s book entitled, IT Governance in Hospitals and Health Systems, please refer to the following assignment instructions below. This chapter consists of interviews with executives identifying mistakes that are made when governing healthcare information technology (IT). The chapter is broken down into subheadings listing areas of importance to understand...

  • Hi there! I need to compare two essay into 1 essay, and make it interesting and...

    Hi there! I need to compare two essay into 1 essay, and make it interesting and choose couple topics which im going to talk about in my essay FIRST ESSAY “Teaching New Worlds/New Words” bell hooks Like desire, language disrupts, refuses to be contained within boundaries. It speaks itself against our will, in words and thoughts that intrude, even violate the most private spaces of mind and body. It was in my first year of college that I read Adrienne...

  • Risk management in Information Security today Everyday information security professionals are bombarded with marketing messages around...

    Risk management in Information Security today Everyday information security professionals are bombarded with marketing messages around risk and threat management, fostering an environment in which objectives seem clear: manage risk, manage threat, stop attacks, identify attackers. These objectives aren't wrong, but they are fundamentally misleading.In this session we'll examine the state of the information security industry in order to understand how the current climate fails to address the true needs of the business. We'll use those lessons as a foundation...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT