-2

I am teaching myself c++ and while on the topic of pointers, I came across an exercise which wants me to define a function length() that receives the coordinates of a point P passed as a pointer, and computes the distance from the origin to the point P:

double length(Coord3D *p);

int main() 
{
    Coord3D pointP = {10, 20, 30};
    cout << length(&pointP) << endl; // would print 37.4166
}

   class Coord3D // Given this class which had variables x,y,z.
   {

     public:
       double x;
       double y;
       double z;
   };

I am confused as to what my next steps are for example I am thinking I should use pointers to create a variable p and set *p to the class. Also I think the function at the very top should contain the formula which would be sqrt(pow(x,2)+pow(y,2)+pow(z,3)). If I am right then how can I implement the pointer properly and if I am wrong what I am I doing wrong and how can I fix it. Please keep in mind this is my first time learning so I am trying my best.

The code that I wrote so far is :

#include<iostream>
#include<string>
#include<cmath>
using namespace std;

// Class


class Coord3D 
{
   public:
     double x;
     double y;
     double z;
};

int p = Coord3D;
p*= Coord3D

// Function

    double length(Coord3D *p)
    {
       double length = sqrt(pow(x,2)+pow(y,2)+pow(z,3));
       cin>>x;
       cin>>y;
       cin>>z;
       cout << length << endl;
    }

// Main

int main() 
{
    Coord3D pointP = {10, 20, 30};
    cout << length(&pointP) << endl; // would print 37.4166
}

Assuming the code works the output should a double that shows the distance from origin to point p.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • 3
    Which C++ textbook are you learning this from? –  Apr 12 '19 at 23:52
  • 1
    Really important note: You cannot perform operations on variables and get meaningful results before getting values in the variables. For example, `double length = sqrt(pow(x,2)+pow(y,2)+pow(z,3));` is utterly worthless if performed before `cin>>x; cin>>y; cin>>z;` – user4581301 Apr 12 '19 at 23:54
  • 1
    When learning C++ an invaluable tool, apart from [a good reference book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) is a debugger. You can step through your code and see exactly how it's behaving which helps build understanding of what certain operations actually do. – tadman Apr 13 '19 at 00:02

2 Answers2

0

Trouble writing a function is often a sign that more time needs to be spent designing the function. Let's start by moving your description of the function into the function's documentation:

// Receives the coordinates of a point P passed as a pointer, and
// computes the distance from the origin to the point P.
double length(Coord3D *p)

It's a start, but it combines three aspects of the function into one statement. Let's try breaking out 1) the function's purpose, 2) the function's input, 3) the function's output.

// Computes the distance from the origin to a point.
// Input: the point P passed as a pointer
// Output: the distance
double length(Coord3D *p)

You've already got an idea of how to accomplish the purpose (the call to the sqrt function), but you're not sure how to get values to that function. (Terminology note: "values", not "variables". The number 10 is a value, but it is not a variable. Variables have names, like "pointP". A single variable may hold one, many, or even no values, depending on its type.) So we should take a closer look at your function's input requirement.

// Input: the point P passed as a pointer

Well, that's not exactly right, is it? The inputs to a function come via parameters, but this function has no parameter named "P" (remember that case matters). In fact, the name "P" seems to refer to the variable in the main function. One important rule for writing good functions: keep it self-contained. In particular, we don't care what the caller calls things. So toss that name. We are only interested in "the point". (Which point? The one mentioned in the statement of the function's purpose. More complex cases would need to be more specific, but we're starting simple. You can work on more complex once you've got the simple down.)

Also, if the input parameter is a pointer, then it cannot be a point. This is where one of your programming skills comes in: translate the natural language description into more precise terms. A parameter cannot be "as a pointer"; either it is a pointer or it is not. Get rid of the wishy-washy and take a stand. We want a pointer!**

// Computes the distance from the origin to a point.
// Input:  a pointer to the point
// Output: the distance (via a return statement)
double length(Coord3D *p)

Now hopefully things are becoming a bit clearer. This function needs to calculate the distance to the thing to which its parameter points (i.e. *p). It's not some "variable x" that you need to square, but the x-coordinate of *p. More precisely, you need to square the x field of the object to which p points, a.k.a. p->x. Similarly for y and z.

[Get rid of the statements between your class definition and your function definition -- they serve no purpose other than generating confusion. Get rid of the streaming operations (>> and <<) in your function -- that is not how this function is supposed to do its input/output. Input is via its parameter, and output is via its (currently missing) return statement.]

**Actually, we probably want a reference instead of a pointer, but that might be a future lesson.

JaMiT
  • 14,422
  • 4
  • 15
  • 31
-1

Fixed version:

#include <iostream>
#include <cmath>

class Coord3D 
{
public:
    double x;
    double y;
    double z;
};

double length(const Coord3D& p)
{
    return sqrt(pow(p.x, 2) + pow(p.y, 2) + pow(p.z, 2));
}

std::istream& operator >> (std::istream& is, Coord3D& p)
{
    return is >> p.x >> p.y >> p.z;
}

int main() 
{
    Coord3D pointP = {10, 20, 30};
    std::cout << length(pointP) << std::endl; // would print 37.4166

    // And now from input:
    std::cin >> pointP;
    std::cout << length(pointP) << std::endl;
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • 1
    Recommend `sqrt (p.x * p.x + p.y * p.y + p.z * p.z)` -- no need for the function call to simply square a number `:)` – David C. Rankin Apr 13 '19 at 00:05
  • @DavidC.Rankin: For learning purpose, using those methods seem fine. We are not looking for performance here. – Jarod42 Apr 13 '19 at 00:08
  • Thank you so much for the feedback and the help but I am confused on one thing? Where exactly does the variable p come from and how does p.variable work? – Intisar Ratul Apr 13 '19 at 00:18
  • @user207421 code is an explanation to anyone who has motivated themselves to learn the basic language. – Richard Hodges Apr 13 '19 at 01:11
  • @Jarod42 - sure, no dings, just helping others that might not see it. You would hope the compiler would automatically optimize on square (or cube, etc..), but I don't think that is a guarantee. – David C. Rankin Apr 13 '19 at 01:42