0% found this document useful (0 votes)
25 views

Lab Report: C: S B: S: D: September 24, 2018

This program calculates the distance between two points in a Cartesian plane. It takes input for the x and y coordinates of the first point (x1, y1) and the second point (x2, y2) from the user. It then calculates the difference between the x and y coordinates and stores them in variables x and y. Finally, it uses the distance formula, √(x2 - x1)2 + (y2 - y1)2, to calculate and output the distance between the two points given. The program is tested by inputting sample values (8,7) and (10,14) for the two points, and it correctly outputs the distance as 7.28011, matching

Uploaded by

Altaf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Lab Report: C: S B: S: D: September 24, 2018

This program calculates the distance between two points in a Cartesian plane. It takes input for the x and y coordinates of the first point (x1, y1) and the second point (x2, y2) from the user. It then calculates the difference between the x and y coordinates and stores them in variables x and y. Finally, it uses the distance formula, √(x2 - x1)2 + (y2 - y1)2, to calculate and output the distance between the two points given. The program is tested by inputting sample values (8,7) and (10,14) for the two points, and it correctly outputs the distance as 7.28011, matching

Uploaded by

Altaf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

National University of Sciences and Technology (NUST)

SMME

LAB REPORT

COURSE : Fundamentals of Programming

SUBMITTED BY : Altaf Ahmad (250282)

SECTION : ME-10 A

DATE : September 24, 2018


Program: Create a program to calculate the distance between two points.
Code:

Description:
This program written in C++ programming language calculates the distance between
two points placed in a cartesian plane. It is created using Microsoft Visual Studio. The formula
used in this program is the actual distance formula used in Mathematics which is:
�(𝑥𝑥2 – 𝑥𝑥1 )2 + (𝑦𝑦2 – 𝑦𝑦1 )2 . But the formula is broken down into few steps to make it easier to
use in the code. Here x2 and x1 are the x coordinates of the two points. While y2 and y1 are the y
coordinates of the two points. This program calculates the distance between the two points
given by the user and shows the output on the screen.
Explanation:
At first the program loads the standard libraries of the programming language. Then, in
the main function of the code, the programs asks for input from the user and stores them in the
variables declared for the x and y coordinates. The x coordinates being x1 and x2 and the y
coordinates are y1 and y2. The variables are declared as doubles to even take the input in
decimals and output the result in decimals in case a decimal value is obtained. The program
asks the user to enter the four values of x1,x2,y1,y2. Then two other variables in double datatype
are declared to calculate the difference between the two x coordinates and y coordinates
respectively. They are named as x and y. The final statement in the code calculates the distance
of the two points and outputs it on the screen.
Output:

Explanation:
The program asks users to enter the values one by one. The output is taken using some
random values to calculate the distance between them. For this particular example the user
entered first x coordinate as 8, first y coordinate as 7, second x coordinate as 10 and second y
coordinate as 14. So, the two points are: (8,7) and (10,14). And the program calculated the
distance between the points which came as 7.28011
Conclusion:
Taking the same example practically and calculating with the same values, the result comes out
to be exactly the same as computed with the help of this code. Thus, the efficiency of this code
is ensured

You might also like