Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

3d Distance array

Posted on 2004-04-05
1
Medium Priority
?
298 Views
Last Modified: 2010-04-01
can any one provide a simplistic code for a 3d array?  This is for homework.  This what I have so far.

void input_fun(Point &, &);
double calculate_fun(Point, Point);
void print_fun(double distance);

struct Point {

      double x;
      double y;
      double z;

};

      Point p1, p2;      

      double distance;

      input_fun(p1, p2);
      distance = calculate_fun(p_array);
      print_fun(distance);

      return 0;

}

void input_fun(Point &p1, &p2) {

      cin >> p1.x >> p1.y >> p1.z;
      cin >> p2.x >> p2.y >> p2C.z;
}

double calculate_fun(Point p1, Point p2) {

      double distance;
      
      distance = sqrt(p1.x -p2.x....

      return distance;

}

void print_fun(double distance) {

      cout << "distance = " << distance << endl;

}

I am looking for the modified working code.  This is for a homework assignment.  CMPS 222
0
Comment
Question by:edelossantos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 4

Accepted Solution

by:
PerryDK earned 2000 total points
ID: 10763005
Looks like your having trouble with the distance formula for two 3d points.

Distance formula for 2d points is sqrt( (x1 - x2)^2 + (y1 - y2)^2 )

You can extend this for 3d points simply by sqrt( (x1 - x2)^2 + (y1 - y2)^2  + (z1 - z2)^2 )

Your other problem was with arrays.  if you know the size of your array you can use
Point myArray[10];

In the case of the below program the size of the array is not known until runtime which means we need to dynamically allocate our array such as this.

int numPoints;
cin >> numPoints;

Point* ptrArray = new Point[numPoints];

When doing this you must be sure to delete any memory that you allocate at run time with the usuage of the delete operator. like
delete[] ptrArray;

Below I have provided a sample output and program that produces the output.  This probably isn't exactly what your teacher wants so I wouldn't just hand it in.  Read carefully what your instructor is asking for and provide exactly that.

******BEGIN OUTPUT*******
How many points do you want to enter?  3

Enter the values for point 1.  0 0 0

Enter the values for point 2.  1 1 1

Enter the values for point 3.  5 0 0


The points you have entered are
Point 1 = (0, 0, 0)
Point 2 = (1, 1, 1)
Point 3 = (5, 0, 0)

Enter the numbers of the points you want to calculate the distance of.
(Note: enter 0 if you want to quit)
1 2

The distance between the point (0, 0, 0) and the point (1, 1, 1) is 1.73205

Enter the numbers of the points you want to calculate the distance of.
(Note: enter 0 if you want to quit)
1 3

The distance between the point (0, 0, 0) and the point (5, 0, 0) is 5

Enter the numbers of the points you want to calculate the distance of.
(Note: enter 0 if you want to quit)
0

******END OUTPUT*******


The actual program

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

struct Point {

     double x;
     double y;
     double z;

};


void inputPoint(Point &p)
{
  cin >> p.x >> p.y >> p.z;
}

void outputPoint(const Point& p)
{
  cout << "(" << p.x << ", " << p.y << ", " << p.z << ")";
}

double getDistance(const Point& p1, const Point& p2)
{
  double distance;

  distance =
      sqrt(   (p1.x - p2.x) * (p1.x - p2.x)
            + (p1.y - p2.y) * (p1.y - p2.y)
            + (p1.z - p2.z) * (p1.z - p2.z)
           );

  return distance;
}

void outputDistanceBetween(const Point& p1, const Point& p2)
{
  cout << "The distance between the point ";
  outputPoint(p1);
  cout << " and the point ";
  outputPoint(p2);
  cout << " is " << getDistance(p1, p2) << endl;
}

void inputPointsForDistanceComparison(int& point1Index, int& point2Index)
{
  cout << endl;
  cout << "Enter the numbers of the points you want to calculate the distance of." << endl
       << "(Note: enter 0 if you want to quit)" << endl;
  cin >> point1Index;
  cin >> point2Index;
}

int main(int argc, char* argv[])
{
  cout << "How many points do you want to enter?  ";
  int numPoints;
  cin >> numPoints;

  Point* ptrPoints = new Point[numPoints];
  for(int i = 0; i < numPoints; i++)
  {
    cout << endl << "Enter the values for point " << i + 1 << ".  ";
    inputPoint(ptrPoints[i]);
  }

  cout << endl << endl << "The points you have entered are " << endl;
  for(int i = 0; i < numPoints; i++)
  {
    cout << "Point " << i + 1 << " = ";
    outputPoint(ptrPoints[i]);
    cout << endl;
  }

  int point1Index, point2Index;
  inputPointsForDistanceComparison(point1Index, point2Index);

  while(point1Index != 0)
  {
    cout << endl;

    //validate the indexes;
    if(point2Index <= 0 || point2Index > numPoints)
    {
      cout << "Point " << point2Index << " is not a valid point " << endl;
      inputPointsForDistanceComparison(point1Index, point2Index);
      continue;
    }

    //set the proper indexes
    point1Index--;
    point2Index--;
    //this was done because we listed the points 1, 2, 3, ... num points

    outputDistanceBetween(ptrPoints[point1Index], ptrPoints[point2Index]);

    inputPointsForDistanceComparison(point1Index, point2Index);
  }

  //finally remebmer to delete the memory we allocated
  delete[] ptrPoints;

  return 0;

}
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question