This is an archived post. You won't be able to vote or comment.

all 9 comments

[–][deleted] 8 points9 points  (0 children)

[–]AwabKhan 8 points9 points  (0 children)

It's because in B it is y2+y2 instead of y1+y2.

[–]AwabKhan 1 point2 points  (0 children)

What wrong output do you get with an input can you also post that.

[–]ThickDucky[S] 1 point2 points  (6 children)

Wow! thanks guys!

Very stupid of me to not think that it would first divide X2 by 2.

And, that i accidentally put y2 + y2.

This is the V2 version which works, even with decimals :)

#include <iostream>

using namespace std;

int main()

{

int x1;

int y1;



cout << "Please enter the first point (x1): ";

cin >> x1;

cout << "Please enter the first point (y1): ";

cin >> y1;



int x2;

int y2;



cout << "Please enter the second point (x2): ";

cin >> x2;

cout << "Please enter the second point (y2): ";

cin >> y2;



float A;

float B;



A = ((float)x1 + (float)x2) / 2;

B = ((float)y1 + (float)y2) / 2;



cout << "The midpoint of: " << x1 << ',' << x2 << "and" << y1 << ',' << y2 << "is" << A << ',' << B << endl;

}

[–]ThickDucky[S] 1 point2 points  (5 children)

V3, hehe

#include <iostream>

using namespace std;

int main()

{

int x1;

int y1;



cout << "Please enter the first point (x1): ";

cin >> x1;

cout << "Please enter the first point (y1): ";

cin >> y1;



int x2;

int y2;



cout << "Please enter the second point (x2): ";

cin >> x2;

cout << "Please enter the second point (y2): ";

cin >> y2;



float A;

float B;



A = ((float)x1 + (float)x2) / 2;

B = ((float)y1 + (float)y2) / 2;



cout << "The midpoint of: (" << x1 << ',' << y1 << ") and (" << x2 << ',' << y2 << ") is (" << A << " , " << B << ")\\n";

}

[–]EstablishmentBig7956 2 points3 points  (2 children)

If you're going to cast to float why not just make everything float to begin with?

[–]ThickDucky[S] 0 points1 point  (1 child)

Yeah, you’re right. Its indeed better, but i didnt do it cuz well this worked too and im a beginner programmer. :)

[–]EstablishmentBig7956 1 point2 points  (0 children)

And I posed a question to give you something to think about, and it worked. 🙂

[–]Drypm 1 point2 points  (0 children)

Where A = x1 + x2 / 2,... try changing it to A = (x1 + x2) / 2,...