Saturday, 28 January 2017

Object-Oriented Programming in C++ Fourth Edition By Robert Lafore Chapter-4 C++ Structures -- Questions 2

Leave a Comment

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. 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

Explanation:

We strongly recommend you to minimize your browser and try this yourself first.

Below mention code is compiled in Visual Studio 2015 and Code Blocks 13.12,output snap is attached.. If any problem you feel and you want some explanation feel free to contact us.



Code:

/**************************************************|
/*************C++ Programs And Projects************|
***************************************************/
// structure models point on the plane
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
struct point
{
       int xCo;     //X coordinate
       int yCo;     //Y coordinate
};
////////////////////////////////////////////////////////////////
int main()
{
       point p1, p2, p3;                        //define 3 points
       cout << "\nEnter coordinates for p1: "//get 2 points
       cin >> p1.xCo >> p1.yCo;                 //from user
       cout << "Enter coordinates for p2: ";
       cin >> p2.xCo >> p2.yCo;
       p3.xCo = p1.xCo + p2.xCo;                //find sum of
       p3.yCo = p1.yCo + p2.yCo;                //p1 and p2
       cout << "Coordinates of p1 + p2 are : "     //display the sum
              << p3.xCo << ", " << p3.yCo << endl;
       return 0;

}

Output:
Object-Oriented Programming in C++ Fourth Edition By Robert Lafore Chapter-4 C++ Structures -- Questions 2



Related Articles:



If You Enjoyed This, Take 5 Seconds To Share It