I am having trouble on figureing out on how to return more than one value for x and Y. I know you can only have one return statement but i need to return a value for x and y. I am a little lost on how to do this.

This is what i have so far.

Point.cpp

``````Point Point::midpoint(Point &midPointX, Point &midPointY)
{

Point middle;

midPointX = ((0 + middle.getX()) / 2);       // i am adding 0 because its supposed to go from point (0,0)

midPointY = ((0 + middle.getY()) / 2);       // i am adding 0 because its supposed to go from point (0,0)

return middle;
}``````

here is a sample output of the program. If more code is needed i to see if i am going wrong somewhere i can post it but when i comment out the midpoint stuff it compiles fine and just gives (0,0) as the midpoint

1st coordinate: 2
2nd coordinate: 2

The coordinates are: (2, 2)

The distance from invoking point (0,0)
to the coordinate you entered (2,2)
is: 2.82843 units

The midpoint between the invoking point (0,0)
and the coordinate you entered (2,2)
is: (0,0)

5
Contributors
9
Replies
10
Views
6 Years
Discussion Span
Last Post by JasonHippy

Pass the midpoints in by reference. Load your values into them. They will be changed when you ret

Pass the midpoints in by reference. Load your values into them. They will be changed when you ret

so something like:

``````double midX, midY

midX = getX();
midY = getY();

// then do something like

midPointX = ((0 + middle.getX()) / 2);

midPointY = ((0 + middle.getY()) / 2);

return middle;``````

No,

Your original code is essentially correct. The problem I see is that the "middle" object is not initialized anywhere, it's a default object. As a result, you are most likely getting garbage values when you call middle.getX() and middle.getY().

What does your "Point" class' default constructor look like? What state is a "Point" in after the default constructor executes?

Edited by Fbody: n/a

Assuming that I properly understand what you're trying to do (I think I do!); Although the maths is basically correct, as this is a member function of your class, you'd probably be better off rethinking your function somewhat. Try to take a slightly more object-oriented approach.

Ignore the origin and the midpointX and midpointY things for now too, they are just background things that you are concentrating too hard on and overcomplicating things for yourself. There is a pretty simple and elegant solution that is staring you right in the face, yet still eludes you.

The bare essentials of what your function should be doing is returning a point which represents the midpoint between two points. The two points being 'this' point (the current instance of the Point class) and an instance of another Point object that is passed into the function.

The point returned by your function will contain midpointX and midpointY values. Both of which can be extracted from the returned Point by using the getX() and getY() accessors you have defined in your Point class

You've already got the function pretty much set up (apart from the parameters to your function, which are inappropriate and unnecessary IMHO, as the returned point will take care of these values!)
Here's a very strong hint:

``````Point Point::midpoint(const Point &refPt)
{
Point middle;
middle.setX( /* I'll leave you to work out */ );
middle.setY( /* what goes in these calls */ );
return middle;
}``````

As you can see, the above code is not much different to what you've already got. But there are a couple of minor differences.

It's also incomplete. There are a couple of things missing! {shock horror!} Namely two very simple expressions to go into the calls to middle.setX() and middle.setY()... I'll leave you to fill in the exact details of those. You already know the maths, so you should be able to work it out!

Just in case, some further hints:
It involves the member variables for the x and y properties of 'this' instance of the Point object (m_Xpos and m_Ypos, or whatever you've called them) and the x and y properties of the passed-in instance. (Your getX() and getY() accessors may help here!)

You'll also need an addition operation and a division by 2 in each expression! I can't put it any simpler than that without writing it for you!
(Although, writing it would probably be quicker than doing all of this! heh heh!)

And the 'origin' remains irrelevant, as do the 'midpointX' and 'midpointY' variables.. You can safely put them out of your mind. You'll see why shortly!

With the function set up as shown; you'll be able to use the function in your main program like this:

``````// IDK how your Point class is initialised
// but this is just an abstract example
// you know how to initialise your class objects
// Either way, set up two points and give 'em some values!:
Point origin(0.0, 0.0);
Point pt1(2.0, 2.0);

// find the midpoint between the points:
Point mid = pt1.midpoint(origin);

// or equally:
Point mid2 = origin.midpoint(pt1);

// both should yield the same result! {1,1}

// now you have the midpoint, you can get the x and y components if you need to
// by using the relevant accessor:
double midpointX = mid.getX();``````

It's a very simple idea, but also very effective. I think you were just trying to overcomplicate things for yourself by overthinking the problem!

As the meerkats of Meerkovo would say
"Seemples! {squeak}" heh heh! XD

Edited by JasonHippy: n/a

ok. I must really be missing something because when i put in a number the program runs but when i put in what is being passed (which is random numbers) it gives

Ex.

``````Point Point::midpoint( const Point &midPoint)
{

Point middle;

middle.setX(/* it only lets me set numbers, like 5, not what is being passed*/);
middle.setY(/*same problem as above*/)

return middle;
}``````

these are the errors it gives

``````Point.cpp:82: error: ‘number1’ was not declared in this scope
Point.cpp:83: error: no matching function for call to ‘Point::setY()’
Point.cpp:53: note: candidates are: void Point::setY(double)``````

this is where i set the random numbers

``````srand(time(0));
number1 = rand() % 1000 ;
number2 = rand() % 1000 ;

pt1.setX(number1);
pt1.setY(number2);
pt1.print();``````

and when i try and set pt1 to the random numbers like you did with pt1(2.0,2.0) I get

``````Point.cpp:82: error: no matching function for call to ‘Point::setX()’
Point.cpp:48: note: candidates are: void Point::setX(double)
Point.cpp:83: error: no matching function for call to ‘Point::setY()’
Point.cpp:53: note: candidates are: void Point::setY(double)
make[2]: Leaving directory `/home/dcornell/2004/hw/HW6'``````

Edited by clickspiker23: n/a

Two ways:
1. using class: declare midPointX and midPointY as public int, then it can be access from other class or main.
2. return x*1000 +y. Then when you getting the value, you have to undo the process to get back two values x and y.

Two ways:
1. using class: declare midPointX and midPointY as public int, then it can be access from other class or main.
2. return x*1000 +y. Then when you getting the value, you have to undo the process to get back two values x and y.

why do you declare it in class and not in main?
I know you said then it can be access from other class or main.

Edited by clickspiker23: n/a

why do you declare it in class and not in main?
I know you said then it can be access from other class or main.

then u can make it only accessible for the class you want by using friend method

Edited by murnesty: n/a

ok. I must really be missing something because when i put in a number the program runs but when i put in what is being passed (which is random numbers) it gives

Ex.

``````Point Point::midpoint( const Point &midPoint)
{

Point middle;

middle.setX(/* it only lets me set numbers, like 5, not what is being passed*/);
middle.setY(/*same problem as above*/)

return middle;
}``````

these are the errors it gives

``````Point.cpp:82: error: ‘number1’ was not declared in this scope
Point.cpp:83: error: no matching function for call to ‘Point::setY()’
Point.cpp:53: note: candidates are: void Point::setY(double)``````

this is where i set the random numbers

``````srand(time(0));
number1 = rand() % 1000 ;
number2 = rand() % 1000 ;

pt1.setX(number1);
pt1.setY(number2);
pt1.print();``````

and when i try and set pt1 to the random numbers like you did with pt1(2.0,2.0) I get

``````Point.cpp:82: error: no matching function for call to ‘Point::setX()’
Point.cpp:48: note: candidates are: void Point::setX(double)
Point.cpp:83: error: no matching function for call to ‘Point::setY()’
Point.cpp:53: note: candidates are: void Point::setY(double)
make[2]: Leaving directory `/home/dcornell/2004/hw/HW6'``````

Sorry, I was running under the assumption that you were using a double to represent your x and y variables in your Point class and therefore assumed that your setX and setY member functions would also take doubles as parameters.

When calling your classes setX and setY functions, you need to make sure you are passing whatever type of values the functions are expecting.

Also what types are 'number1' and 'number2'? From what I can see there you haven't specified their type, which is probably why you're getting this error:
Point.cpp:82: error: ‘number1’ was not declared in this scope

You should also be getting similar errors about 'number2' too!

The thing to bear in mind is that rand() will generate an int value and the modulus operation will also yield an int.

Bearing this in mind; if your setX and setY functions require an int as a parameter, then you simply need to ensure that number1 and number2 are set up as ints:

``````srand(time(0));
int number1 = rand() % 1000;
int number2 = rand() % 1000;

Point pt1;
pt1.setX(number1);
pt1.setY(number2);
pt1.print();``````

Otherwise, if your setX and setY functions require a double as a parameter, then you'll need to declare number1 and number2 to be doubles and cast the int returned by the expression 'rand()%1000' to double like this:

``````srand(time(0));
double number1 = static_cast<double>(rand() % 1000);
double number2 = static_cast<double>(rand() % 1000);

Point pt1;
pt1.setX(number1);
pt1.setY(number2);
pt1.print();``````

BTW: Inside the Point::MidPoint function you should have something like this:

``````Point Point::midpoint( const Point &somePoint)
{

Point middle; // this is the midpoint which we'll return

//calculate and set the x and y of the midpoint
middle.setX((this->getX() + somePoint.getX())/2);
middle.setY((this->getY() + somePoint.getY())/2)

// return the midpoint
return middle;
}``````

The point passed into the function is not the midpoint, it's the point we want to find the midpoint for. i.e. We want to find the midpoint between 'this' Point instance (the point whose midpoint function we're calling) and the passed-in point.
The point returned by the function contains the midpoint.

Is that kinda making sense now??

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.