Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab Report 03

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Mechanical Engineering

Lab Report 01
Title:
Program Structure in C++ and different
Data types
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)
Objectives:
1. In this lab, we shall learn about program structure of C++ and its parts.
2. Learn about different type of variable data types and how to assign them.
3. Learn about arithmetic operations in C++.
4. Cout and cin objects.

Theory:
Basic C++ Program structure:
• Pre processor directives:
They are the set of instruction given to the compiler
before program initiation. They are also called compiler directives. They add up special
instructions in a C++ program at time of compilation.
That include #include and define.

• Header files:
Header files are C++ source files that define library objects/functions.
They are usually declared after the preprocessor directives. They are included in the
program if the function used in the program is associated with a library.
There are different type of header files in C++ each having specific
library. They are declared by entering header file name in angular brackets < > after the
preprocessor directives. Such as
#include<iostream> has included input/ output stream header
file.

• Main ( ) function:
Main function is used to indicate start of a C++ program. The
statements that are to be included in the program are written in curly brackets { } after the
main function statement.
The control moves towards the main function first at the
execution of the program.

• C++ Statements:
C++ statements are included in the curly brackets of main
function. They end up with a semi colon ; and they are the body of the program.

• Keywords:
Keywords are the system reserved words in the C++ program. They are
assigned special functions and they can’t be declared as variable names.
For example int, float, amin are system reserved variables.

• Tokens:
Tokens are the characters, preprocessor directives, variables and operators
etc used in a C++ statement
In statement #include<iostream>
int main( ) { There is include directive, iostream header and main
function with {<>()} punctuation marks.

• Variables :
Variables are the quantities whose values can be changed during program
execution.
They refer to memory storage or location in system memory. They have a
fixed name but there value can be changed during program.

• Data Types:
There are 5 main data types in C++
1. Int
2. Float
3. char
4. bool
5. double

int data type:


It is used to represent integer data type. It declares integer type variables,
having only whole numbers without fraction or decimal point.
It is of 2 bytes and store value upto -32768 and 32767
It can also be further specified as
• Short int: 2 bytes and -32468 and 32467 Range.
• Long int : 4 bytes and -2147483648 and 2147483647 Range.
• Unsigned int: 2 bytes (only positive numbers) and 0-65535 Range.

Float data type:


It is used to represent real and floating data. It gives floating value to
variable. Float has exponential notation and decimal point both in the number.
Its range is of 4 bytes with 3.14x10-38 to 3.14x10+38
• Long float: 8 bytes with 1.7x10-308 to 1.7x10+308
• Double: 8 bytes with 1.7x10-308 to 1.7x10+308
• Long double: 10 bytes with 1.7x10-4932 to 1.7x10+4932

Char data type:


It is used to declare characters. These characters can include alphabets,
numeric operators and numbers. It has the value of 1 byte per character and can store upto
65535 bytes.

Bool data type:


It is used to declare logical variables. True/ false called the Boolean
variables. True has the value of 1 and false has 0.

• Declaration of variable:
Assigning a name and value to a variable is called
declaration of the variable. For this purpose, a statement a declaring statement is written
for the variable to get its value assigned.
For example int a ; shows a variable has an integer data type

• Initialization:
When the variable is declared, a specific memory location is assigned
to it and it gets access to its value. This pres assigned value if not used by mistake can
cause problems in the program. For this purpose, sometimes a value is assigned to
variable when it is declared. It is call initialization.
For example float b=100, shows b float type is given value of 100.

• Constants:
They are the quantities whose values are fixed and they don’t change
during the program execution.

• Const qualifier:
The data item followed by the const term has a specific value
assigned to it which cant be changed during program execution.
• Define directive:
It is a preprocessor directive. It is used to define a constant
quantity.
For example #define identifier constant shows that the character has been assigned a
constant value and constant indicates the value which is assigned.

• Arithmatic Operators:
They are the symbols to perform mathematical operations.
They are used to relate two quantities and operation is applied between them. The
whole statement is called arithmetic expression.
For example ans = a*b;

Questions
1. What are preprocessor directives. Explain with examples.
Ans : The preprocessor directives are the set of instruction given to the compiler
before the compilation of the program. These instructions add up useful codes during
program execution. They are usually written at start of the program (before main( )).
They are written as #include or #define and then a header file
is added afterwards.
2. What is a variable? Write rules to write the variable
Ans : Variable :
A variable is a quantity whose value can be changed during the
program execution.
It is a memory location or storage in system memory. The variable
are given a specific data type and a name by a declaration statement. The name of the
variable is fixed but not its value.
Rules to write its name:
• Its first letter should be an alphabet.
• Underscore can also be used as the first letter of the variable.
• Its cant start with a number.
• No special character can be included in the name of the alphabet such as #
• Variable cant be assigned a name similar to a C++ keyword or reserved words.
• Blank spaces cant be included in the name of the variable.
• A variable used to declare one data type cant be used to declare another.

3. Difference between cout and cin objects .


Ans : cout Objects:
Cout command also read as See Out is a statement to take value/
instruction from the computer and print it on the screen. The c stands for console
meaning the computer screen. It is the part of the iostream headerfile.

For example #include<iostream>


using namespace std;
Int main(){
Cout<<”Hello world”;
Return 0;
}
This small program is used to print Hello World on the screen using a cout command.
Cin objects:
It is also a C++ defined object included in the iostream headerfile. Cin
stands for See In and it’s the command used for taking an input from the user and
assign it to variables. It is basically an input stream taking values from the user .

For example #include<iostream>


using namespace std;
int main(){
int age;
cout<<”Enter your age “;
cin>>age;
return 0;
}

Lab Task:
1. Program to calculate radius of the circle
2. //To Calculate radius of circle//
3.
4. #include<iostream>
5. using namespace std;
6.
7. int main(){
8.
9. float rad,pi,ans;//declaration
10. cout<<"Enter the value of Radius of circle :";
11. cin>>rad;//input radius
12. cout<<"Applying Formula";
13.
14. pi=3.142857;
15. ans=pi*rad*rad;
16. cout<<"\nThe radius of circle is "<<ans;
17.
18. }

2. Program to convert rupees to dollars


//Convert Rupees into Dollars//

#include<iostream>
using namespace std;
int main(){
float dol,rup;
cout<<"Enter the amount in Rupees :";
cin>>rup;
dol=0.01*rup;
cout<<"The Amount in Dollars(USD) is " <<dol<<" USD.";

3. Program to put your name, address and age and print it on the screen.
4. //Program to print name,age and address//
5. #include<iostream>
6. #include<string>
7. using namespace std;
8.
9. int main(){
10. string nam,addr;
11. int age;
12. cout<<"Please enter your Name :";
13. getline(cin,nam);
14. cout<<"\nEnter your Address :";
15. getline(cin,addr);
16. cout<<"\nPlease enter your Age :";
17. cin>>age;
18. cout<<"\n*************MY INTRODUCTION**************";
19. cout<<"\nMy name is "<<nam<<" ."<<"\nMy Address is "<<addr<<"
."<<"\nMy Age is "<<age<<" .";
20. }
21.

4. Program to convert years age into months, days, hours, minutes and seconds
5. //Program to convert age into months,days,hours and seconds
6. #include<iostream>
7. #include<cmath>
8. using namespace std;
9.
10. int main(){
11.
12. float yr,day,mon,hr;
13. float min,sec;//Declaration
14. cout<<"Please Enter your Age in Years :";
15. cin>>yr;
16. mon=12*yr;
17. cout<<"Your Age in Months is "<<mon<<" months.";
18. day=365*yr;
19. cout<<"\nYour Age in Days is "<<day<<" days.";
20. hr=8760*yr;
21. cout<<"\nYour Age in Hours is "<<hr<<" hrs.";
22. min=525600*yr;
23. cout<<"\nYour Age in Minutes is "<<min<<" min.";
24. sec=3.1536*pow(10,7)*yr;
25. cout<<"\nYour Age in Seconds is "<<sec<<" sec.";
26.
27. }

You might also like