You are on page 1of 9

National University of Sciences and Technology (NUST)

SMME

LAB REPORT
LAB REPORT # 3

COURSE : Fundamentals of Programming

SUBMITTED BY : Altaf Ahmad (250282)

SUBMITTED TO: Ahmad Subhani

SECTION : ME-10 A

DATE : October 11, 2018


Program#1: Declare and define a class named Point having two Float x and y
coordinates, two constructors Point::Point() and a Parametrized constructor
Point::Point(float,float). Create two objects, change and display changed values of x and
y coordinates.
Code:
File: Point.h

File: Point.cpp

File: Main.cpp
Description:
The program pasted above basically defines a class named Point and defines two
Constructors in it. The default constructor is Point::Point(), and the other is
Point::Point(float,float). As a point is defined by its x and y coordinates in a plane, therefore
the program also consists of a point defined by its x and y coordinates. Finally, the values of
x and y are changed in the main function and displayed on the screen.
Explanation:
In the Point.h file, a class named Point is defined, the access to which is made public.
Two constructors are defined, one is the default constructor, Point::Point () and the other is
a parametrized constructor which takes two float parameters.
In the point.cpp file, at first the point.h file is loaded. And then the standard
input/output libraries are loaded. Then a default point at (0,0) is drawn in the default
constructor of class Point. And the parametrized constructor takes variables a and b as the
values of x and y coordinates respectively.
Finally, in the main.cpp file pch.h and the standard libraries are loaded. And then
two objects of class Point are defined which are named as object1 and object2. Both object1
and object2 take x and y coordinates. object1 has the coordinates (2,3) and object2 has
coordinates (4,5). Then the datafields of object1 and object2 are modified. And finally the
changed values of object1 and object2 are shown on the screen using the cout statements.
Output:

Explanation:
In the ouput, at first the original x and y coordinates of object1 and object2 are
shown which were defined as (2,3) and (4,5) respectively. And then the modified values of
object1 and object2 are shown which change to (3,2) and (5,4).
Flowchart:
Program#2: Create a program to calculate:
1:Distance Traveled = x0 + v0t + at2
𝟐𝟐𝒎𝒎𝟏𝟏 𝒎𝒎𝟐𝟐
2:Tension in a cord =
𝒎𝒎𝟏𝟏 +𝒎𝒎𝟐𝟐
*g
𝝆𝝆𝝆𝝆(𝑨𝑨𝟐𝟐𝟐𝟐 − 𝑨𝑨𝟏𝟏𝟐𝟐 )
3: Fluid pressure at the end of a pipe = 𝑷𝑷𝟐𝟐 = 𝑷𝑷𝟏𝟏 +
𝟐𝟐𝟐𝟐𝟏𝟏𝟐𝟐

Code:

Description:
The program written above basically consists of three parts. All of the sections are
separated by dashed comments. The first part calculates the distance traveled and shows it
on screen. The second part calculates the tension in a cord and outputs it on screen. And
the third and final part calculates the fluid pressure at the end of a pipe and shows its
ouput. All of these parts consist of complex formulas and are broken down into parts to
successfully code it.
Explanation:
After loading the standard libraries, a constant of double datatype named g is
defined whose value is declared as 9.80665. In the main function, the first part of the
program is evaluated by first declaring its variables and assigning them values. All the
variables are defined as double. Xo is assigned a value of 23.6, Vo (5), t(10), a(4.23). And a
variable named distance stores the final value of distance calculated by the respective
formula. The calculated distance is shown on screen using cout statement. And then a blank
line is printed on screen just to make the program neat.
In part 2, at first 2 variables named m1 and m2 are declared as doubles and are given
values 20 and 30 respectively. Another variable of datatype double is declared named
tension which calculates the tension in the string by its formula and shows the calculated
tension on the screen. And another blank line is printed.
In part 3, variables of double datatype are declared at first. P1 is given a value of 200,
p(1000), v(20), A1(20), A2(30). Then 2 other double variables are defined name as
numerator and denominator. Numerator calculates the values given in the numerator of the
𝝆𝝆𝝆𝝆(𝑨𝑨𝟐𝟐𝟐𝟐 − 𝑨𝑨𝟏𝟏𝟐𝟐 )
formula: . While denominator calculates the values in its denominator.
𝟐𝟐𝟐𝟐𝟏𝟏𝟐𝟐
Finally the variable P2 is defined as double which calculates the whole calculations of the
final formula by adding P1 to quotient of numerator and denominator. And then output is
shown on screen using cout statement.
Output:

Explanation:
Using the given values, the output first shows the total distance traveled by the first
part of the program which comes out to be 496.6. Then, the tension in the cord is shown
which is calculated by 2nd part of program which comes out to be 235.36. Finally the fluid
pressure at end of pipe is shown which is calculated by third part of program which comes
out to be 12700.
Flowchart:

Program#3
Code:
Description:
The program pasted above basically just shows the sizes of some of the datatypes
used in C++. It also shows the precision and exponential capacity of some of the datatypes.
Explanation:
After loading the standard libraries and preprocessor directives, some of the other
preprocessor directives like climits and cfloat are loaded into the program. In the main
function body of the program, at first the size of short datatype is shown on the screen by
using sizeof(short) command. Similarly, by using sizeof() command and cout the sizes of int
and long datatype are shown on the screen.
After this the number of decimal digits held by float datatype which can be rounded
into a floating type without change in number of decimal digits are shown on the screen by
using FLT_DIG command and cout. Similarly, maximum integer value for the exponent of a
base-10 expression that will create a floating point number is shown on the screen by using
FLT_MAX_10_EXP and cout command. The FLT_DIG and FLT_MAX_10_EXP are enabled by
using the preprocessor directive “cfloat”. And then, the size of float is shown on screen by
using sizeof(float) and cout.
Then similarly the same codes are used for displaying the precision, exponents and
actual size of the double and long double datatypes.
Output:

Explanation:
In the output, at first the sizes of short, int and long datatypes are shown on the
screen which are 2,4 and 4 respectively. The sizes are actually displayed as the number of
bytes taken by each datatype.
Then the precision digits of float, its maximum exponent and the maximum size of
float are shown which are 6,38 and 4 respectively. The precision digits are number of
decimal digits which can be rounded into a floating type without change in number of
decimal digits. The maximum exponent is maximum integer value for the exponent of a
base-10 expression.
Similarly, then the precision digits, maximum exponent and the size of double datatype are
shown which are 15,308 and 8 respectively. And finally, the precision digits, maximum
exponent and size of the long double datatype are shown which are 15,308 and 8
respectively. The main idea of the program is to show the user how different datatypes hold
values and work.
Flowchart:

You might also like