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

Lab Report 2

This document contains a lab report for a mechanical engineering class. It includes objectives, theory, lab work, homework assignments, and practice problems related to concepts like data types, classes, units conversion, and calculating distance, tension, and pressure using formulas.

Uploaded by

Hamza Sohail
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lab Report 2

This document contains a lab report for a mechanical engineering class. It includes objectives, theory, lab work, homework assignments, and practice problems related to concepts like data types, classes, units conversion, and calculating distance, tension, and pressure using formulas.

Uploaded by

Hamza Sohail
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Mechanical Engineering

Lab Report 02

1ST SEMESTER

Submitted To: Engr. Ali Hassan


Class: ME-13 B Group: N/A
Submitted By
Sr.No Name (Roll.no) Data Viva Total
Presentation
1 Muhammad
Usama Tariq
(366451)

School Of Mechanical and Manufacturing Engineering


Objectives :
1. Introduction to different data types.
2. Introduction to Classes.
3. Difference b\w “Char” and “string”.
4. Usage of a Different Preprocessing derective

Theory :
1. Structure of C++ Program
2. Introduction to classes in C++
3. Difference b/w “char” and “string”

1. Structure of C++ Program :

• Preprocessing Directives :
They give instructions before the
program is compiled.
It begins with #include and then file name
inside < > signs such as
#include<file name>

Example :
#include<iostream> is a preprocessing directive
to give additional statements regarding input/output.

<cmath> is used to give statements regarding


maths.
They are of 2 types :

#include“filename”
#include<filename>

• Directives :
Tells compiler to use library names declared in
namespace .
Example:
std (standard namespace) has C++ defined components.

• Main Function :
Every C++ Program contains set of instructions
which constitute the main program called the main function.

Example :
int main shows that function will return integer value to
the system after execution.
The main function is always enclosed inside braces { } called code
blocks.

• Declarations :
Before storing data inside a variable, we have to
first allocate it a memory location that will be used by it to store input
user data, this task is performed by writing statement called
Declaration.
Example :
int a ;
float b ;
Here “int a” means that variable a can only store integer values, and
“float b” means b can store values that are in decimal points.

• Assignment Statements :
Such statements that are written to
assign values to variables are called assignment statements.
These values can be a character, an integer or result of a program.
Example :
a=b
t=0
x = y = z = ans
• Arithmatic Operators :
They are the mathematical characters
used for different math functions i.e + - / * etc .
• Data Types :
They are the strings which tell us which type of data
will be stored in the variable.
The common C++ data types are
int : stores only integer values, takes up the size of 4 bytes.
float : stores data in decimal values, takes up the size of 4 bytes.
char : stores data in form of single character(letter or number);
takes up the size of 1 byte.
bool : stores data in form of Boolean variables (True/False)
double : similar data type as float with higher storage capacity
i.e 8 bytes.

2. Introduction to Classes :

• Definition :
Classes are mainly used to define user defined data
types. Classes make the new data easily accessible and can be
implemented to other functions without much effort,
• Declaration :

1. Class definition should be written in code blocks.


2. Members may include variables and functions.
3. Commands like public, private and protected tell about the data
access.
4. A semi colon must follow closing bracket i.e };

• Implementation :
The constructor file must include block of
every method declared in class. They have the same name as that
of the class.
In this way, a single class can give rise to multiple
objects (programs), as different programs may use the data of the
class according to their needs.
3. Difference b/w string and char :
A char is just a letter, an alphabet that may also be a number.
While on the other hand, a string is an array of chars i.e it can be
a full name unlike char.
1. In char, you can only take one character at a time while in string
you can store a complete word in the defined variable.

Lab Work (10) :


1. Code to calculate distance b/w two points.
2. Code to enter values in an algebraic formula.

1. Code to calculate distance :

#include<iostream>
#include<cmath>

using namespace std;


int main()
{
float px,py,qx,qy,ans;//declaring all variables

float dist;
cout<<"Please put in Px :";//input and printing of variables
cin>>px;
cout<<"Please put in Py :";
cin>>py;

cout<<"Please put in Qx :";


cin>>qx;

cout<<"Please put in Qy :";


cin>>qy;

dist=(px-qx)*(px-qx)+(py-qy)*(py-qy);//formula to calculate distance

ans=sqrt(dist);

cout<<"Your answer is ="<<ans;//print answer


cout<<"\n Mission Passed Respect+";
}
2. Code for algebraic formula :
3. #include<iostream>
4. #include<cmath>
5.
6. using namespace std;
7. int main(){
8.
9. float x,ans;
10. cout<<"Please enter value of x ";
11. cin>>x;
12.
13. ans=(x*x+x)/3;
14. cout<<"Your answer is "<<ans;
15.
16. }

Home Task :
1. Program to convert Miles into kilometers:
2. //**************************Convert Miles into
Kilometres****************************//
3. #include<iostream>
4. using namespace std;
5.
6. int main(){
7. float mil,km;
8. cout<<"Enter the value in Miles :";
9. cin>>mil;
10. cout<<"The value in Miles is "<<mil<<" mil.";
11. km=0.62137*mil;
12. cout<<"\nConverting into Kilometres........";
13. cout<<"\nThe value in Kilometres is "<<km<<" km.";
14.
2. Program to convert Meters into Miles:
3. //**************************Convert Metres into
Miles****************************//
4. #include<iostream>
5. using namespace std;
6.
7. int main(){
8. float mil,m;
9. cout<<"Enter the value in Metres :";
10. cin>>m;
11. cout<<"The value in Metres is "<<m<<" m.";
12. mil=0.00062137*m;
13. cout<<"\nConverting into Miles........";
14. cout<<"\nThe value in Miles is "<<mil<<" mil.";
15. }

3. Program to convert Pounds to kilograms:


4. //**************************Convert Pounds into
Kilograms****************************//
5. #include<iostream>
6. using namespace std;
7.
8. int main(){
9. float kg,lb;
10. cout<<"Enter the value in Pounds :";
11. cin>>lb;
12. cout<<"The value in Pounds is "<<lb<<" lb.";
13. kg=0.45359*lb;
14. cout<<"\nConverting into Kilograms........";
15. cout<<"\nThe value in Kilograms is "<<kg<<" kg.";
16. }
4. Program to convert newtons to pounds:
5. //**************************Convert newtons into
Pounds****************************//
6. #include<iostream>
7. using namespace std;
8.
9. int main(){
10. float nt,lb;
11. cout<<"Enter the value in Newtons :";
12. cin>>nt;
13. cout<<"The value in Newtons is "<<nt<<" N.";
14. lb=0.2248*nt;
15. cout<<"\nConverting into Pounds........";
16. cout<<"\nThe value in Pounds is "<<lb<<" lb.";
17. }

5. Program to convert Fahrenheit to Rankines;


6. //**************************Convert Fahrenheit into
Rankines****************************//
7. #include<iostream>
8. using namespace std;
9.
10. int main(){
11. float fah,rk;
12. cout<<"Enter the value in Fahrenheit :";
13. cin>>fah;
14. cout<<"The value in Fahrenheit is "<<fah<<" *F.";
15. rk=(fah+459.67);
16. cout<<"\nConverting into Rankines........";
17. cout<<"\nThe value in Rankines is "<<rk<<" degrees rankine.";
18. }

6. Program to convert Celcius into Rankines:


//**************************Convert Celcius into
Rankines****************************//
#include<iostream>
using namespace std;

int main(){
float cel,rk;
cout<<"Enter the value in Celcius :";
cin>>cel;
cout<<"The value in Celcius is "<<cel<<" *C.";
rk=1.8*(cel)+491.67;
cout<<"\nConverting into Rankines........";
cout<<"\nThe value in Rankines is "<<rk<<" degrees rankine.";
}

7. Program to convert kelvin into Fahrenheit:


8. //**************************Convert Kelvin into
Fahrenheit****************************//
9. #include<iostream>
10. using namespace std;
11.
12. int main(){
13. float fah,kel;
14. cout<<"Enter the value in Degree Kelvin :";
15. cin>>kel;
16. cout<<"The value in degree Kelvin is "<<kel<<" K.";
17. fah=((kel-273.15)*1.8+32);
18. cout<<"\nConverting into Fahrenheit........";
19. cout<<"\nThe value in Fahrenheit is "<<fah<<" *F.";
20. }
Practice Problems :
1. Program to calculate Distance travelled:
2. //**************************Calculate Distance
Travelled****************************//
3. #include<iostream>
4. using namespace std;
5.
6. int main(){
7.
8. float dis,v,a,t;
9. cout<<"Enter the value of Initial Velocity :";
10. cin>>v;
11.
12. cout<<"\nEnter the value of Acceleration :";
13. cin>>a;
14.
15. cout<<"\nEnter the value of Time :";
16. cin>>t;
17.
18. dis=(v*t+(0.5)*(a*t*t));
19. cout<<"\nUsing formula to calculate Distance";
20. cout<<"\nThe value of Distance is = "<<dis;
21. }

2. Program to calculate Tension in a cord:


3. //**************************Calculate Tension in a
cord****************************//
4. #include<iostream>
5. using namespace std;
6.
7. int main(){
8.
9. float n,m,t,g;
10. cout<<"Enter the value of Mass 1 :";
11. cin>>m;
12.
13. cout<<"Enter the value of Mass 2 :";
14. cin>>n;
15. g=9.80665;
16. cout<<"The value of g is "<<g;
17.
18. t=((2*m*n)/(m+n))*g;
19.
20. cout<<"\nUsing formula to calculate Tension";
21. cout<<"\nThe value of Tension in the cord is = "<<t;
22. }

3. Program to calculate Pressure at end of pipe:


4. //**************************Calculate Fluid Pressure at end of the
pipe****************************//
5. #include<iostream>
6. using namespace std;
7.
8. int main(){
9.
10. float p,q,rho,a,b,v,ans,an;
11. cout<<"\nEnter the value of Pressure at start of pipe(Initial Prssure)
:";
12. cin>>p;
13. cout<<"\nEnter the value of Velocity :";
14. cin>>v;
15. cout<<"\nEnter the value of Coefficient of viscousity :";
16. cin>>rho;
17. cout<<"\nEnter the value of area at start of pipe :";
18. cin>>a;
19.
20. cout<<"\nEnter the value of area at end of the pipe :";
21. cin>>b;
22.
23. ans=(rho*v*(b*b-a*a));
24. an=p+ans;
25. q=an/(2*a*a);
26. cout<<"\nUsing formula to calculate Pressure at end of pipe(Final
Pressure) ";
27. cout<<"\nThe value of Final Pressure is = "<<q;
28. }
4. Program to calculate Gravitational Centripetal Acceleration:
5. //**************************Calculate Centripetal
Acceleration****************************//
6. #include<iostream>
7. using namespace std;
8.
9. int main(){
10.
11. float ans,ac,pi,t,rad;
12. cout<<"Enter the value of Radius :";
13. cin>>rad;
14. pi=3.142857;
15.
16. cout<<"\nEnter the value of Time Period :";
17. cin>>t;
18.
19. ans=(4*pi*pi*rad);
20. ac=ans/(t*t);
21. cout<<"\nThe value of Centripetal Acceleration is :"<<ac;
22. }

5. Program to calculate Gravitational Potential Energy:


6. //**************************Calculate Change in Potential
Energy****************************//
7. #include<iostream>
8. #include<cmath>
9. using namespace std;
10.
11. int main(){
12.
13. float pe,G,m,M,rad,k;
14. cout<<"Enter the value of Mass of Object :";
15. cin>>m;
16. M=6*pow(10,24);
17. cout<<"\nEnter the value of Radius :";
18. cin>>rad;
19. G=6.673*pow(10,-11);
20. k=-G*m*M;
21. pe=k/(rad);
22. cout<<"\nThe value of Gravitational Potential Energy is :"<<pe;
23. }

Basic class structure:


class P
{
//Type declaration statements
//Data members
private:
double xCoord, yCoord, ;//Class attributes
public:

//Declaration of statements for class methods


//constructors for point class

P(); //Default constructor


P(double x, double y);//parametrized constructor
};

Class Implementation:
/*------------------------------------------------------*/
/* Class implementation for Point */
/* filename: Point.cpp */
#include "Point.h" //Required for Point
#include <iostream> //Required for cout
using namespace std;
//Parameterized constructor
P:: P(double x, double y)
{

//input parameters x,y


cout << " Constructing Point object, parameterized: \n" ;
cout << " input parameters: " << x << " ," << y << endl;
xCoord = x;
yCoord = y;
}
//Default constructor
P::P()
{
cout << " Constructing Point object, default: \n" ;
cout << " initializing to zero" << endl;
xCoord = 0.0;
yCoord = 0.0;
}
int main()
{
//Declare and initialize objects.
cout << " In main, declare p1..." << endl;
P p1;
cout << " \nIn main, declare p2..." << endl;
P p2(1.5, -4.7);
cout << " \nIn main, declare ORIGIN..." << endl;
const P ORIGIN(0.0, 0.0);
return 0;
}

You might also like