You are on page 1of 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 2
√(x – x ) +( y – y ) . But the formula is broken down into few steps to make it easier to use in
2 1 2 1

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 x 1 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