A point on the two-dimensional plane can be represented by two numbers: an x- co
ID: 3917240 • Letter: A
Question
A point on the two-dimensional plane can be represented by two numbers: an x- coordinate and a y- coordinate. For example, (4,5) represents a point 4 units to the right of the vertical axis, and 5 units up from the horizontal axis. The sum of two points can be defined as a new point whose x coordinate is the sum of the x coordinates of the two points, and whose y coordinate is the sum of the y coordinates.
Complete the program below that uses a structure called point to model a point. Comments in the code tells you what you need to do. The program should define three points, and have the user input values to two of them. Then set the third point equal to the sum of the other two, and display the value of the new point.
Interaction with the program should look like this:
Enter coordinates for p1 (use input format: x, y): 3, 4
Enter coordinates for p2 (use input format: x, y): 5, 7
Coordinates of p1 + p2 are: 8, 11
Explanation / Answer
Following is the answer:
#include <iostream>
using namespace std;
struct point
{
int xCo; //X coordinate
int yCo; //Y coordinate
};
//Sum function Prototype
point sum(const point &a1, const point &a2);
//Write the prototype for the showPoint function here
void showPoint(const point &p);
int main()
{
point p1, p2, p3; //define 3 points
char dummy[100];
cout << " Enter coordinates for p1 (use input format: x, y): "; //get 2 points
cin >> p1.xCo >> dummy >> p1.yCo;
cout << "Enter coordinates for p2 (use input format: x, y): ";
cin >> p2.xCo >> dummy >> p2.yCo;
p3 = sum(p1, p2);
showPoint(p3);
cout << " Coordinates of p1+p2 are: "<<p3.xCo << "," << p3.yCo; //display the sum
//Call the showPoint function here
cout << endl;
return 0;
}
void showPoint(const point &p){
cout << p.xCo << ", " << p.yCo;
}
//define the sum function here
point sum(const point &a1, const point &a2){
point p3;
p3.xCo=a1.xCo+a2.xCo;
p3.yCo=a1.yCo+a2.yCo;
return p3;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.