The Point stuct contains two doubles, x and y. Write a function Point midPoint(c
ID: 3569020 • Letter: T
Question
The Point stuct contains two doubles, x and y. Write a function Point midPoint(const Point& a, const Point& b) that takes two Points , calculates the midpoint between them and returns a Point with those coordinates.
Hint: you should declare and return a Point type variable in the function.
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
struct Point {
double x;
double y;
};
//Do not modify anything on or above the line below this
//START_PROVIDED
//YOUR FUNCTION HERE
//END_PROVIDED
//Do not modify anything on or below the line above this
int main()
{
Point p1;
cin >> p1.x;
cin >> p1.y;
Point p2;
cin >> p2.x;
cin >> p2.y;
Point p3 = midPoint(p1, p2);
cout << "(" << p3.x << ", " << p3.y << ")" << endl;
return 0;
}
I need to create a midpoint function
Explanation / Answer
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
struct Point {
double x;
double y;
};
Point midPoint(const Point& a, const Point& b)
{
Point midPoint;
midPoint.x= (a.x+b.x)/2;
midPoint.y= (a.y+b.y)/2;
return midPoint;
}
int main()
{
Point p1;
cin >> p1.x;
cin >> p1.y;
Point p2;
cin >> p2.x;
cin >> p2.y;
Point p3 = midPoint(p1, p2);
cout << "(" << p3.x << ", " << p3.y << ")" << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.