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.
Write a program that
uses a structure called point
to model a point. 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
might look like this:
Enter coordinates for
p1: 3 4
Enter coordinates for
p2: 5 7
Coordinates of p1+p2
are: 8, 11
SOLUTION:
#include<iostream.h>
#include<conio.h>
//////////////////////////////////////////////////////////////
Define Structure
/////////////////////////////////////////////////////////////
struct point
{
int x_coordinate,y_coordinate;
};
///////////////////////////////////////////////////// Define Structure Variables
/////////////////////////////////////////////////////
point p1,p2,p3;
void main()
{
clrscr();
/////////////////////////////////////////////////////////////////
Take Input
////////////////////////////////////////////////////////////////////
cout<<"Enter coordinates of
first point ";
cin>>p1.x_coordinate>>p1.y_coordinate;
cout<<"Enter coordinates of
second point ";
cin>>p2.x_coordinate>>p2.y_coordinate;
//////////////////////////////////////////////////////////////
Calculate Point 3
////////////////////////////////////////////////////////////
p3.x_coordinate=p1.x_coordinate+p2.x_coordinate;
p3.y_coordinate=p1.y_coordinate+p2.y_coordinate;
//////////////////////////////////////////////////////////////////////
Output
/////////////////////////////////////////////////////////////////////
cout<<"\nCoordinates of P1+P2
are: "<<p3.x_coordinate<<" "<<p3.y_coordinate;
getch();
}
OUTPUT:
0 comments: