SlideShare a Scribd company logo
#include<iostream.h>
using namespace std;
int main ( )
{
int r;
float p, peri;
r = 2;
p = 3.14;
peri = 2*p*r;
cout<<―Result is = ―<<peri<<endl;
return 0;
}
3/26/2015 1
RESULT
3/26/2015 2
Coding
3/26/2015 3
Coding
3/26/2015 4
CS-211 PROGRAMMING
FUNDAMENTALS
Introduction
3/26/2015 5
 Pakistan Air force Schools
 F.Sc. PAF Inter college Lahore
 B.E. Mechanical Engineering (CEME NUST)
 Pak Elektron Limtd. Lahore
 Millat Tractors Sheikhupura Road
 Descon Lahore
 Olayan Descon Saudia
 Descon Dubai
3/26/2015 6
 M.Sc. Sustainable Technology, Sweden
 Heritage-University of South Asia
3/26/2015 7
 C++ (See plus pluss)
 To follow syntax and rules of Turbo C++ and Borland
C+
 Text editors e.g. WordPad and note pad
 These are used to write the code
 Compilers are used to run the code
 Source code written and stored in first.cpp
 Compiled code stored/saved as first.obj
 Executed code stored as first.exe
3/26/2015 8
 Contains following three parts
◦ Preprocessor directives
◦ The main( ) Function
◦ C++ statement
3/26/2015 9
Preprocessor Directives
 Instructions given before beginning of actual program
 Also called Compiler Directives
 To define certain actions or special instructions (include
arithmetic equation)
 Normally begin with number sign (#)
 May include keywords like ―include‖ or ―define‖
3/26/2015 10
 Example 1-01
#include<iostream>
int main( )
{
cout <<―this is my first program‖;
return 0;
}
3/26/2015 11
 Example 1-01 (output)
3/26/2015 12
Preprocessor Directives
Header file
 C++ source file
 Contains definitions of function/objects
 ―include‖ is used to add ―iostream‖ into the program
 Has Large number of header files in which library
functions are defined
3/26/2015 13
 Example 1-01
#include <iostream>
using namespace std;
int main ( )
{
cout <<―this is my first program‖;
return 0;
}
3/26/2015 14
 Example 1-01 (output)
3/26/2015 15
 Example 1-01
#include<iostream.h>
#include<conio.h>
void main( )
{
cout <<―this is my first program‖;
getch( );
}
3/26/2015 16
Header file
 ―iostream‖ is short of ―input output stream‖
 Has definitions of several input and output objects or
functions
 Which was it in the last example?
 ―cout‖ (see out)
 Syntax
◦ #include <name of the header file>
3/26/2015 17
 Example 1-01
#include<iostream>
int main( )
{
cout <<―this is my first program‖;
return 0;
}
3/26/2015 18
The main ( ) Function
 Indicates the beginning of a program
 After the preprocessor directives
 Must be included in every program
 Syntax
◦ main ( )
{
program statements…
}
3/26/2015 19
C++ Statements
 Syntax
◦ main ( )
{
program statements…
}
 The statements are written under main ( )
 Between { }
 Are the body of the program
 Each statement ends with ‗;‘
3/26/2015 20
C++ Statements
 Case sensitivity (must remember)
3/26/2015 21
C++ Statements
Key words
 Words used by languages for
 Special purposes
 Also called ‗reserved words‘
 Cannot be used as variable names
3/26/2015 22
 Example 1-02
#include<iostream.h>
int main( )
{
int a, b;
}
3/26/2015 23
C++ Statements
Tokens
 Certain elements of a program
 Every thing other than a statement
3/26/2015 24
 Example 1-02
main ( )
{
int a, b;
}
3/26/2015 25
Example 1-03
#include<iostream.h>
using namespace std;
int main ( )
{
int abc = 4, b = 1997;
float xy = 3.4;
char name[15] = ―Marriam Ahmed‖;
cout <<name<<endl;
cout<<abc<<endl;
cout<<b<<endl;
cout<<xy<<endl;
return 0;
}
3/26/2015 26
Example 1-03 (output)
3/26/2015 27
C++ Statements
Variables
 Not a constant
 Which may change during execution of a program
 Represented by a symbol or a name
 Nadeem, fox pro, x, y etc.
 It represents a storage location on a computer
 Data stored may change, the variable does not
 Also known as a object
 In C++ variable names consists of alphabets and digits
3/26/2015 28
C++ Statements
Rules for writing a variable name
 First character must be alphabetic, exception ‗_‘
 No blank spaces
 Arithematic characters not allowed #, ^, etc
 Reserved words are not allowed
 Maximum length depends up on compiler
 Must be declared
 Again, case sensitive language
3/26/2015 29
Variables Valid/Invalid Remarks
Nadeem valid
perform valid
double invalid C++ reserved word
foxpro valid
switch invalid C++ reserved word
Marriam valid
int invalid C++ reserved word
3taq invalid Starts with numeral
unsigned invalid C++ reserved word
x-y invalid
Special character is not
allowed
Taq Ahd invalid Space is not allowed
3/26/2015 30
 Each variable (we define) is specified by the data types of
the data stored in it
 Each variable is Declared by its type
 C++ has 5 basic data types
◦ int Integer 25, 100, 5000
◦ float Floating Point 3.4×105
◦ double double precision 3.4×105
◦ char characters almost all
◦ bool Boolean logic type variables
3/26/2015 31
The ‗int‘ Data Type
 Represents the integer type data
 Integer is a whole number!
 i.e. without fraction or decimal
◦ 601, 250, -6, 501
 The range depends the computer system being used
◦ In MS-DOS range is -32768 to 32767
 Range can be changed by using following qualifiers
◦ short int
◦ long int
◦ unsigned int
3/26/2015 32
The ‗float‘ Data Type
 Represents real or floating type data
 Real, decimal or exponential notation
 Float, signed or unsigned
3/26/2015 33
The ‗double‘ Data Type
 Represents real or floating type data
 Twice the storage capacity than ‗float data type‘
 Long double Data type
3/26/2015 34
The ‗char‘ Data Type
 char stands for character
 Used to declare character type variables
 Variables, alphabetic characters, numeric digits and
special characters can be stored
 Twice the storage capacity than ‗float data type‘
 Long double Data type
3/26/2015 35
Declaration of Variables
 Assigning the name a variable can hold
◦ Zohaib, sum, addition, x, xyz etc
 Assigning the data type a variable can hold
◦ int, float etc
 Example
◦ int a;
◦ int abc, xyz, d, s;
 Can be more than one variables but separated by a
comma (if of same data type)
3/26/2015 36
Declaration of Variables
 If not, written in each individual line where each
statement is ended with a ‗;‘
 Syntax
◦ type list of variables
 Example
int a, xy;
Float b;
Char nm [15]
double sum;
3/26/2015 37
Initialization of Variables
 Before we declared a variable
 Declared variable gets a memory location assigned to it
that specifies it has a place in memory on the computer
 Now a value to the variable must also be assigned or
defined
 A known value is assigned to it
◦ int a = 110
 The statement can be written as
◦ int a = 110, b = 60, c;
3/26/2015 38
Example 1-03
#include<iostream.h>
using namespace std;
int main ( )
{
int abc = 4, b = 1997;
float xy = 3.4;
char name[15] = ―Marriam Ahmed‖;
cout <<name<<endl;
cout<<abc<<endl;
cout<<b<<endl;
cout<<xy<<endl;
return 0;
}
3/26/2015 39
Example 1-03 (output)
3/26/2015 40
Constants
 A value that cannot be changed during execution of a
program
◦ int a = 44
 Four types of constants in C++
◦ Integer constants
◦ Floating point constants
◦ Character constants
◦ String constants
3/26/2015 41
Constants
Integer constants
 Numerical value without a decimal part
 + can also be used with it
 Integer constants are used in expressions for calculations
◦ int a = 44, b = 55;
sum = a+b;
cout<<sum<<endl;
3/26/2015 42
Constants
Floating-point contant
 Numeric values having both integer and decimal part
 Can also be written in exponential notation
 In exponential notation, can be written as
◦ 123.5E2
◦ Where E represents the exponent
 both the constant and the E notation can be +
3/26/2015 43
Example 1-04
#include<iostream>
int main ( )
{
int r;
const float p = 3.14;
float peri;
r = 2;
peri = 2*p*r;
cout<<―Result is = ―<<peri;
return 0;
}
3/26/2015 44
Example 1-04 (output)
3/26/2015 45
The ―define‖ Directive
 It is a preprocessor directive, used at the beginning of
the program
 Used to define a constant quantity
 Syntax
◦ #define identifier constant
◦ #define p 3.14
 Value defined is ‗p‘, where 3.14 is the constant assigned
to ‗p‘
 3.14 remains same throughout the program
 ‗p‘ can not be used again in the program to define other
3/26/2015 46
The ―define‖ Directive
 Identifier does not have any data type but any data type
can be assigned to it
3/26/2015 47
Example 1-05
#include <iostream>
#define p 3.14
int main ( )
{
int r;
float peri;
r = 2;
peri = 2*p*r;
cout<<―Result is = ―<<peri;
return 0;
}
3/26/2015 48
Example 1-05 (output)
3/26/2015 49
Arithmetic Operators
 Symbols that represent arithmetic operations
 Used in arithmetic expressions
 Following are the arithmetic operators in C++
Operators Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% For remainder
3/26/2015 50
Example 1-06
#include <iostream.h> redundant lines will be excluded
using namespace std;
int main ( )
{
int d, p, s, m, r;
p = 5+2;
s = 5-2;
m = 5*2;
d = 5/2;
r = 5%2;
cout<<―Addition of 5 & 2 is =―<<p<<endl;
cout<<―Subtraction of 5 & 2 is =―<<s<<endl;
cout<<―multiplication of 5 & 2 is =―<<m<<endl;
cout<<―division of 5&2 is =―<<d<endl;
cout<<―remainder of 5/2 is =―<<r<<endl; }
3/26/2015 51
Example 1-06 (output)
3/26/2015 52
Arithmetic Expression
 It is a combination of variables, constants and arithmetic
operators
◦ p = m*x+100 where m=10 and x=5
 Used to calculate value of an arithmetic formula
 Returns a single value after evaluation
 Here ‗=‗ is called the assignment operator
 After evaluation the resultant variable is called the
―receiving variable‖
◦ res = m*x+100
3/26/2015 53
Order of Precedence of Operation
 It is the order in which the expression is evaluated
 C++ only performs one operation at a time
 Following is the order of precedence
◦ All multiplications and divisions are performed first from left to
right
◦ All additions and subtractions are then performed left to right
◦ If parenthesis are used in an expression, the expressions within
parentheses are first computed from left to right
◦ When parentheses are used within parentheses, the expression
within innermost parentheses is evaluated first
3/26/2015 54
Order of Precedence of Operation
 Example (4-(3*5))+2 is evaluated as follows
 First?
 Solution
◦ (3*5) is computed and returns value of 15.
◦ 4-15 is computed and returns value of -11
◦ -11+2 is computed and returns the value of -9
3/26/2015 55
 Statements used to get some data and assign to variables
are input statements
◦ Int a=10, b=510
 Statements used to receive data from computer memory
and send to output devices (monitor) are output
statements
◦ cout<<―your total GPA is = ―<<m<<endl;
3/26/2015 56
The ‗cout‘ object –Output Stream
 The flow of data from-and-to a device is called a stream
 ‗cout‘ (see-out) stands for console out
 Here the console out is the display screen (monitor)
 ‗cout‘ is used as an output statement and is part of
iostream header file
 Example
◦ cout<<―One kilobyte = ―<<1024<<―bytes‖;
 There are two string constants, one numeric constants
and three put to operators
3/26/2015 57
Example 1-07
#include <iostream.h>
using namespace std;
int main ( )
{
cout<<―one kilobyte = ―<<1024<<―bytes‖;
return 0;
}
3/26/2015 58
Example 1-07 (output)
3/26/2015 59
Example 1-08
#include <iostream>
#include <conio> used for functions such as
clearing the screen
int main ( )
{
clrscr ( )
cout<<―C++is a powerful programming language‖;
cout<<―UNIX operating system is written in C++‖;
cout<<―it is an easy to learn language‖;
return 0;
}
3/26/2015 60
Example 1-08 (output)
3/26/2015 61
Example 1-09
#include <iostream>
int main ( )
{
cout<<―I ‖<<―LOVE ‖<<―PAKISTAN‖ ;
}
3/26/2015 62
Example 1-09 (output)
3/26/2015 63
The Escape Sequence
 ‗endl‘ is similar to an escape sequence
 Do you remember what it did?
 These are special non-printing characters
 Used to control printing on the output device
 Combination of ‗‘ and a code character
 For example, n is an escape sequence which is used to
transfer the printing control to a new line
 Used inside a string constant or independently
 In single or double quotes
3/26/2015 64
The Escape Sequence
 Can be used anywhere in the output stream
 for example
◦ cout<<―I Love Pakistann‖;
◦ cout<<―I n Love n Pakistan‖;
3/26/2015 65
The Escape Sequence
Escape Sequence Explaination
a sounds an alert or alarm
b
backspace, print sequnce moves
a space back
cout<<"Pakistanb";
cout<<"Punjab";
the ouptput will be
PakistaPunjab
3/26/2015 66
The ―endl‖ Manipulator
 Is an important and most common used manipulator
 These are the operators used with put to (<<) operators
 Stands for end of line
 Has the same effect as ―n‖
 For example
◦ cout<<―C++ n‖<<―programming n‖<<―language‖;
 Is equivalent to
◦ Cout<<―C++ ―<<endl<<―programming ―<<endl<<―language‖;
3/26/2015 67
Example 1-10
#include <iostream>
int main ( )
{
cout<<―I am a ‖<<endl<<―Pakistani‖ ;
return 0;
}
3/26/2015 68
Example 1-10
3/26/2015 69
The ―setw‖ Manipulator
 Stands for set width
 Used to set width of the output on the output on the
screen
 The output is left-justified
 Syntax
◦ setw(n) where n is the specified width and is an integer
 For example
◦ cout<<setw(10)<<―Pakistan‖<<setw(15)<<―Islamabad‖;
 Is a part of ―iomanip.h‖ header file
3/26/2015 70
Result
3/26/2015 71
Example 1-11
#include <iostream>
#include<iomanip>
int main ( )
{
cout<<setw(5)<<62<<setw(5)<<8<<endl;
cout<<setw(5)<<100<<setw(5)<<77<<endl;
cout<<setw(5)<<5<<setw(5)<<162<<endl;
return 0;
}
3/26/2015 72
Example 1-11
3/26/2015 73
Example 1-12 (Assignment Statement)
#include <iostream.h>
int main ( )
{
int a, b, c;
a=200;
b=100;
c=a
a=b
b=c
cout<<―value of a = ―<<a<<endl;
cout<<―value of b = ―<<b<<endl;
}
3/26/2015 74
Example 1-12
3/26/2015 75
Example 1-13
#include <iostream>
int main ( )
{
int year, month;
year = 20;
month = year*12;
cout<<―years = ―<<year<<endl;
cout<<―months = ―<<month<<endl;
return 0;
}
3/26/2015 76
Example 1-13
3/26/2015 77
The ―cin‖ Object— Input Stream
 ‗cin‘ (see-in) stands for console input
 This is an input stream
 It is a part of iostream header file
 It requires you to input from your keyboard during the execution
of a program
 Value is input and press Enter to input the value
 Syntax
 cin>>var1[>>var2….];  >> is an extraction operator or get from
operator
 Usually a separate statement is used for each variable
 For example
◦ cin>>a>>b>>c; and press Enter to for typing each data
3/26/2015 78
Example 1-14
#include <iostream.h>
int main ( )
{
int n1, n2, s, p;
cout<<―Enter the first number ? ―;
cin>>n1;
cout<<―Enter the second number ? ―;
cin>>n2;
s=n1+n2;
p=n1*n2
cout<<―Sum of numbers = ―<<s<<endl;
cout<<―Product of numbers = ―<<p<<endl;
}
3/26/2015 79
Example 1-14
3/26/2015 80
Example 1-15
#include <iostream>
{
int age;
long int age_mon;
char name[50];
cout<<―Enter the name of the person ―;
cin>>name;
cout<<―Enter the age of person in years ―;
cin>>age;
age_mon = age*12;
cout<<―Name of the person = ―<<name<<endl;
cout<<―Age in months = ―<<age_mon<<endl;
}
3/26/2015 81
Example 1-15
3/26/2015 82
Example 1-16
float avg;
char name[20];
int total, cpp, os, edp;
cout<<―Enter the name of the student ―;
cin>>name;
cout<<―Enter the marks obtained in C++―;
cin>>cpp;
cout<<―Enter the marks obtained in Operating Systems―;
cin>>os;
cout<<―Enter the marks obtained in EDP―;
cin>>edp;
total = cpp+os+edp;
avg = total/3.0;
cout<<―Name of the student = ―<<name<<endl;
cout<<―Total Marks = ―<<total<<endl;
cout<<―Average Marks = ―<<avg<<endl;
3/26/2015 83
Example 1-16
3/26/2015 84
Example 1-17
{
float c, f;
cout << "Enter the temperature in Fahrenheit? ";
cin>>f;
c=(f-32)*5.0/9.0;
cout<<"The temperature in Celsius is = "<<c<<endl;
return 0;
}
3/26/2015 85
Example 1-17
3/26/2015 86
Example 1-18
{
float r,h,v;
cout << "Enter the radius of the cylinder = ";
cin>>r;
cout << "Enter the height of the cylinder = ";
cin>>h;
v=3.14*r*r*h;
cout<<"The volume of the cylinder is = "<<v;
return 0;
}
3/26/2015 87
Example 1-18
3/26/2015 88
Compound Assignment Statement
 We have previously used simple statements, to assign
values to a variable
 Like
◦ m=x*100+50
◦ v=p*r*r*h
 The same assignment statements can be used to assign
one value to more than one variable.
 E.g.
◦ x = y = 16;
3/26/2015 89
Example 1-19
{
int x, y, a, b, c, s;
x = y = a = b = c = 515;
s = x+y+a+b+c;
cout << "the sum is = " <<s<< endl;
return 0;
}
3/26/2015 90
Example 1-19
3/26/2015 91
Compound Assignment Expression
 Is used to add, subtract, multiply etc a value to or from a
variable
◦ Without writing on either side of op ‗=‗
 The arithmetic operator is combined with the
assignment operator (=)
 Syntax
◦ var op = expression
 xy = xy + 10;
◦ Here 10 is added to the variable xy
◦ Where xy already has some value
3/26/2015 92
Compound Assignment Expression
 Which can also be written as
 xy + = 10;
 Similarly x += 9; is the same as?
◦ x = x + 9;
 x -=9; is the same as?
◦ x = x – 9;
3/26/2015 93
Example 1-20
{
int a,b,c;
a=3;
b=6;
c=9;
c*=(a+b); // c = c * (a+b)
cout << "Result is = " <<c<< endl;
return 0;
}
3/26/2015 94
Example 1-20 (cout)
3/26/2015 95
The Comment Statement
 Used to show comment on the coding statements in a
program
 Used to explain the logic of the program
 It is a non-executable statement
◦ // This is a my first program in C++
◦ // Java language is similar to C++
 Can be given in any line of the program
 e.g.
◦ sum=a+b //used to take sum of two variables
3/26/2015 96
Example 1-21
{
// declare the variables
int a,c; //a&c are variable names
a=10; //value 10 is assigned to a
c=a*a*a; //assignment statement to calculate cube
/* next statement is the output statement to
print the cube of 10 */
cout << "cube of "<<a <<" = "<<c<< endl;
// this is the end of the program
return 0;
}
3/26/2015 97
Example 1-21 (cout)
3/26/2015 98

More Related Content

PPTX
Three address code In Compiler Design
PPTX
Intermediate code- generation
PPT
Code generator
PPTX
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
PDF
C Programming
PDF
Compiler unit 4
PPTX
Lecture 12 intermediate code generation
PPTX
Code Generation
Three address code In Compiler Design
Intermediate code- generation
Code generator
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
C Programming
Compiler unit 4
Lecture 12 intermediate code generation
Code Generation

What's hot (20)

PDF
Compiler unit 2&3
PPT
Interm codegen
PPT
Intermediate code generation
PPTX
COMPILER DESIGN AND CONSTRUCTION
PPT
Chapter Eight(3)
PDF
Compiler unit 5
PPT
Intermediate code generation (Compiler Design)
PPT
Chapter 6 intermediate code generation
PPTX
C++ AND CATEGORIES OF SOFTWARE
PPTX
Intermediate code
PPT
C++ for beginners
PPT
Introduction to Basic C programming 01
PPTX
Intermediate code generation1
PPT
Unit 2 c programming_basics
PPT
Unit 4 functions and pointers
PPTX
C Language (All Concept)
PPTX
Back patching
PDF
Intermediate code generation in Compiler Design
Compiler unit 2&3
Interm codegen
Intermediate code generation
COMPILER DESIGN AND CONSTRUCTION
Chapter Eight(3)
Compiler unit 5
Intermediate code generation (Compiler Design)
Chapter 6 intermediate code generation
C++ AND CATEGORIES OF SOFTWARE
Intermediate code
C++ for beginners
Introduction to Basic C programming 01
Intermediate code generation1
Unit 2 c programming_basics
Unit 4 functions and pointers
C Language (All Concept)
Back patching
Intermediate code generation in Compiler Design
Ad

Similar to Cs211 module 1_2015 (20)

PPTX
Programming Fundamentals
PPT
Chapter02-S11.ppt
PPT
Basics of c++ Programming Language
PPT
Chapter 2 Introduction to C++
PPT
2.overview of c++ ________lecture2
PDF
BASIC C++ PROGRAMMING
PPT
Pengaturcaraan asas
PPT
programming week 2.ppt
PDF
Introduction to Programming Fundamentals 3.pdf
PDF
Basic Elements of C++
PPT
Chapter2
PPTX
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
PDF
Week 02_Development Environment of C++.pdf
PPT
02a fundamental c++ types, arithmetic
PPTX
Programming Fundamentals
PPT
Savitch ch 02
PPT
Elementary_Of_C++_Programming_Language.ppt
PPTX
Chap_________________1_Introduction.pptx
PPTX
Introduction to C++ lecture ************
Programming Fundamentals
Chapter02-S11.ppt
Basics of c++ Programming Language
Chapter 2 Introduction to C++
2.overview of c++ ________lecture2
BASIC C++ PROGRAMMING
Pengaturcaraan asas
programming week 2.ppt
Introduction to Programming Fundamentals 3.pdf
Basic Elements of C++
Chapter2
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
Week 02_Development Environment of C++.pdf
02a fundamental c++ types, arithmetic
Programming Fundamentals
Savitch ch 02
Elementary_Of_C++_Programming_Language.ppt
Chap_________________1_Introduction.pptx
Introduction to C++ lecture ************
Ad

Recently uploaded (20)

PPTX
Artificial Intelligence
PPT
Project quality management in manufacturing
PDF
Well-logging-methods_new................
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Geodesy 1.pptx...............................................
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
composite construction of structures.pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Current and future trends in Computer Vision.pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
web development for engineering and engineering
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
additive manufacturing of ss316l using mig welding
PPT
introduction to datamining and warehousing
Artificial Intelligence
Project quality management in manufacturing
Well-logging-methods_new................
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Foundation to blockchain - A guide to Blockchain Tech
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Geodesy 1.pptx...............................................
Internet of Things (IOT) - A guide to understanding
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
UNIT 4 Total Quality Management .pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
composite construction of structures.pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Current and future trends in Computer Vision.pptx
bas. eng. economics group 4 presentation 1.pptx
web development for engineering and engineering
CH1 Production IntroductoryConcepts.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
additive manufacturing of ss316l using mig welding
introduction to datamining and warehousing

Cs211 module 1_2015

  • 1. #include<iostream.h> using namespace std; int main ( ) { int r; float p, peri; r = 2; p = 3.14; peri = 2*p*r; cout<<―Result is = ―<<peri<<endl; return 0; } 3/26/2015 1
  • 6.  Pakistan Air force Schools  F.Sc. PAF Inter college Lahore  B.E. Mechanical Engineering (CEME NUST)  Pak Elektron Limtd. Lahore  Millat Tractors Sheikhupura Road  Descon Lahore  Olayan Descon Saudia  Descon Dubai 3/26/2015 6
  • 7.  M.Sc. Sustainable Technology, Sweden  Heritage-University of South Asia 3/26/2015 7
  • 8.  C++ (See plus pluss)  To follow syntax and rules of Turbo C++ and Borland C+  Text editors e.g. WordPad and note pad  These are used to write the code  Compilers are used to run the code  Source code written and stored in first.cpp  Compiled code stored/saved as first.obj  Executed code stored as first.exe 3/26/2015 8
  • 9.  Contains following three parts ◦ Preprocessor directives ◦ The main( ) Function ◦ C++ statement 3/26/2015 9
  • 10. Preprocessor Directives  Instructions given before beginning of actual program  Also called Compiler Directives  To define certain actions or special instructions (include arithmetic equation)  Normally begin with number sign (#)  May include keywords like ―include‖ or ―define‖ 3/26/2015 10
  • 11.  Example 1-01 #include<iostream> int main( ) { cout <<―this is my first program‖; return 0; } 3/26/2015 11
  • 12.  Example 1-01 (output) 3/26/2015 12
  • 13. Preprocessor Directives Header file  C++ source file  Contains definitions of function/objects  ―include‖ is used to add ―iostream‖ into the program  Has Large number of header files in which library functions are defined 3/26/2015 13
  • 14.  Example 1-01 #include <iostream> using namespace std; int main ( ) { cout <<―this is my first program‖; return 0; } 3/26/2015 14
  • 15.  Example 1-01 (output) 3/26/2015 15
  • 16.  Example 1-01 #include<iostream.h> #include<conio.h> void main( ) { cout <<―this is my first program‖; getch( ); } 3/26/2015 16
  • 17. Header file  ―iostream‖ is short of ―input output stream‖  Has definitions of several input and output objects or functions  Which was it in the last example?  ―cout‖ (see out)  Syntax ◦ #include <name of the header file> 3/26/2015 17
  • 18.  Example 1-01 #include<iostream> int main( ) { cout <<―this is my first program‖; return 0; } 3/26/2015 18
  • 19. The main ( ) Function  Indicates the beginning of a program  After the preprocessor directives  Must be included in every program  Syntax ◦ main ( ) { program statements… } 3/26/2015 19
  • 20. C++ Statements  Syntax ◦ main ( ) { program statements… }  The statements are written under main ( )  Between { }  Are the body of the program  Each statement ends with ‗;‘ 3/26/2015 20
  • 21. C++ Statements  Case sensitivity (must remember) 3/26/2015 21
  • 22. C++ Statements Key words  Words used by languages for  Special purposes  Also called ‗reserved words‘  Cannot be used as variable names 3/26/2015 22
  • 23.  Example 1-02 #include<iostream.h> int main( ) { int a, b; } 3/26/2015 23
  • 24. C++ Statements Tokens  Certain elements of a program  Every thing other than a statement 3/26/2015 24
  • 25.  Example 1-02 main ( ) { int a, b; } 3/26/2015 25
  • 26. Example 1-03 #include<iostream.h> using namespace std; int main ( ) { int abc = 4, b = 1997; float xy = 3.4; char name[15] = ―Marriam Ahmed‖; cout <<name<<endl; cout<<abc<<endl; cout<<b<<endl; cout<<xy<<endl; return 0; } 3/26/2015 26
  • 28. C++ Statements Variables  Not a constant  Which may change during execution of a program  Represented by a symbol or a name  Nadeem, fox pro, x, y etc.  It represents a storage location on a computer  Data stored may change, the variable does not  Also known as a object  In C++ variable names consists of alphabets and digits 3/26/2015 28
  • 29. C++ Statements Rules for writing a variable name  First character must be alphabetic, exception ‗_‘  No blank spaces  Arithematic characters not allowed #, ^, etc  Reserved words are not allowed  Maximum length depends up on compiler  Must be declared  Again, case sensitive language 3/26/2015 29
  • 30. Variables Valid/Invalid Remarks Nadeem valid perform valid double invalid C++ reserved word foxpro valid switch invalid C++ reserved word Marriam valid int invalid C++ reserved word 3taq invalid Starts with numeral unsigned invalid C++ reserved word x-y invalid Special character is not allowed Taq Ahd invalid Space is not allowed 3/26/2015 30
  • 31.  Each variable (we define) is specified by the data types of the data stored in it  Each variable is Declared by its type  C++ has 5 basic data types ◦ int Integer 25, 100, 5000 ◦ float Floating Point 3.4×105 ◦ double double precision 3.4×105 ◦ char characters almost all ◦ bool Boolean logic type variables 3/26/2015 31
  • 32. The ‗int‘ Data Type  Represents the integer type data  Integer is a whole number!  i.e. without fraction or decimal ◦ 601, 250, -6, 501  The range depends the computer system being used ◦ In MS-DOS range is -32768 to 32767  Range can be changed by using following qualifiers ◦ short int ◦ long int ◦ unsigned int 3/26/2015 32
  • 33. The ‗float‘ Data Type  Represents real or floating type data  Real, decimal or exponential notation  Float, signed or unsigned 3/26/2015 33
  • 34. The ‗double‘ Data Type  Represents real or floating type data  Twice the storage capacity than ‗float data type‘  Long double Data type 3/26/2015 34
  • 35. The ‗char‘ Data Type  char stands for character  Used to declare character type variables  Variables, alphabetic characters, numeric digits and special characters can be stored  Twice the storage capacity than ‗float data type‘  Long double Data type 3/26/2015 35
  • 36. Declaration of Variables  Assigning the name a variable can hold ◦ Zohaib, sum, addition, x, xyz etc  Assigning the data type a variable can hold ◦ int, float etc  Example ◦ int a; ◦ int abc, xyz, d, s;  Can be more than one variables but separated by a comma (if of same data type) 3/26/2015 36
  • 37. Declaration of Variables  If not, written in each individual line where each statement is ended with a ‗;‘  Syntax ◦ type list of variables  Example int a, xy; Float b; Char nm [15] double sum; 3/26/2015 37
  • 38. Initialization of Variables  Before we declared a variable  Declared variable gets a memory location assigned to it that specifies it has a place in memory on the computer  Now a value to the variable must also be assigned or defined  A known value is assigned to it ◦ int a = 110  The statement can be written as ◦ int a = 110, b = 60, c; 3/26/2015 38
  • 39. Example 1-03 #include<iostream.h> using namespace std; int main ( ) { int abc = 4, b = 1997; float xy = 3.4; char name[15] = ―Marriam Ahmed‖; cout <<name<<endl; cout<<abc<<endl; cout<<b<<endl; cout<<xy<<endl; return 0; } 3/26/2015 39
  • 41. Constants  A value that cannot be changed during execution of a program ◦ int a = 44  Four types of constants in C++ ◦ Integer constants ◦ Floating point constants ◦ Character constants ◦ String constants 3/26/2015 41
  • 42. Constants Integer constants  Numerical value without a decimal part  + can also be used with it  Integer constants are used in expressions for calculations ◦ int a = 44, b = 55; sum = a+b; cout<<sum<<endl; 3/26/2015 42
  • 43. Constants Floating-point contant  Numeric values having both integer and decimal part  Can also be written in exponential notation  In exponential notation, can be written as ◦ 123.5E2 ◦ Where E represents the exponent  both the constant and the E notation can be + 3/26/2015 43
  • 44. Example 1-04 #include<iostream> int main ( ) { int r; const float p = 3.14; float peri; r = 2; peri = 2*p*r; cout<<―Result is = ―<<peri; return 0; } 3/26/2015 44
  • 46. The ―define‖ Directive  It is a preprocessor directive, used at the beginning of the program  Used to define a constant quantity  Syntax ◦ #define identifier constant ◦ #define p 3.14  Value defined is ‗p‘, where 3.14 is the constant assigned to ‗p‘  3.14 remains same throughout the program  ‗p‘ can not be used again in the program to define other 3/26/2015 46
  • 47. The ―define‖ Directive  Identifier does not have any data type but any data type can be assigned to it 3/26/2015 47
  • 48. Example 1-05 #include <iostream> #define p 3.14 int main ( ) { int r; float peri; r = 2; peri = 2*p*r; cout<<―Result is = ―<<peri; return 0; } 3/26/2015 48
  • 50. Arithmetic Operators  Symbols that represent arithmetic operations  Used in arithmetic expressions  Following are the arithmetic operators in C++ Operators Meaning + Addition - Subtraction * Multiplication / Division % For remainder 3/26/2015 50
  • 51. Example 1-06 #include <iostream.h> redundant lines will be excluded using namespace std; int main ( ) { int d, p, s, m, r; p = 5+2; s = 5-2; m = 5*2; d = 5/2; r = 5%2; cout<<―Addition of 5 & 2 is =―<<p<<endl; cout<<―Subtraction of 5 & 2 is =―<<s<<endl; cout<<―multiplication of 5 & 2 is =―<<m<<endl; cout<<―division of 5&2 is =―<<d<endl; cout<<―remainder of 5/2 is =―<<r<<endl; } 3/26/2015 51
  • 53. Arithmetic Expression  It is a combination of variables, constants and arithmetic operators ◦ p = m*x+100 where m=10 and x=5  Used to calculate value of an arithmetic formula  Returns a single value after evaluation  Here ‗=‗ is called the assignment operator  After evaluation the resultant variable is called the ―receiving variable‖ ◦ res = m*x+100 3/26/2015 53
  • 54. Order of Precedence of Operation  It is the order in which the expression is evaluated  C++ only performs one operation at a time  Following is the order of precedence ◦ All multiplications and divisions are performed first from left to right ◦ All additions and subtractions are then performed left to right ◦ If parenthesis are used in an expression, the expressions within parentheses are first computed from left to right ◦ When parentheses are used within parentheses, the expression within innermost parentheses is evaluated first 3/26/2015 54
  • 55. Order of Precedence of Operation  Example (4-(3*5))+2 is evaluated as follows  First?  Solution ◦ (3*5) is computed and returns value of 15. ◦ 4-15 is computed and returns value of -11 ◦ -11+2 is computed and returns the value of -9 3/26/2015 55
  • 56.  Statements used to get some data and assign to variables are input statements ◦ Int a=10, b=510  Statements used to receive data from computer memory and send to output devices (monitor) are output statements ◦ cout<<―your total GPA is = ―<<m<<endl; 3/26/2015 56
  • 57. The ‗cout‘ object –Output Stream  The flow of data from-and-to a device is called a stream  ‗cout‘ (see-out) stands for console out  Here the console out is the display screen (monitor)  ‗cout‘ is used as an output statement and is part of iostream header file  Example ◦ cout<<―One kilobyte = ―<<1024<<―bytes‖;  There are two string constants, one numeric constants and three put to operators 3/26/2015 57
  • 58. Example 1-07 #include <iostream.h> using namespace std; int main ( ) { cout<<―one kilobyte = ―<<1024<<―bytes‖; return 0; } 3/26/2015 58
  • 60. Example 1-08 #include <iostream> #include <conio> used for functions such as clearing the screen int main ( ) { clrscr ( ) cout<<―C++is a powerful programming language‖; cout<<―UNIX operating system is written in C++‖; cout<<―it is an easy to learn language‖; return 0; } 3/26/2015 60
  • 62. Example 1-09 #include <iostream> int main ( ) { cout<<―I ‖<<―LOVE ‖<<―PAKISTAN‖ ; } 3/26/2015 62
  • 64. The Escape Sequence  ‗endl‘ is similar to an escape sequence  Do you remember what it did?  These are special non-printing characters  Used to control printing on the output device  Combination of ‗‘ and a code character  For example, n is an escape sequence which is used to transfer the printing control to a new line  Used inside a string constant or independently  In single or double quotes 3/26/2015 64
  • 65. The Escape Sequence  Can be used anywhere in the output stream  for example ◦ cout<<―I Love Pakistann‖; ◦ cout<<―I n Love n Pakistan‖; 3/26/2015 65
  • 66. The Escape Sequence Escape Sequence Explaination a sounds an alert or alarm b backspace, print sequnce moves a space back cout<<"Pakistanb"; cout<<"Punjab"; the ouptput will be PakistaPunjab 3/26/2015 66
  • 67. The ―endl‖ Manipulator  Is an important and most common used manipulator  These are the operators used with put to (<<) operators  Stands for end of line  Has the same effect as ―n‖  For example ◦ cout<<―C++ n‖<<―programming n‖<<―language‖;  Is equivalent to ◦ Cout<<―C++ ―<<endl<<―programming ―<<endl<<―language‖; 3/26/2015 67
  • 68. Example 1-10 #include <iostream> int main ( ) { cout<<―I am a ‖<<endl<<―Pakistani‖ ; return 0; } 3/26/2015 68
  • 70. The ―setw‖ Manipulator  Stands for set width  Used to set width of the output on the output on the screen  The output is left-justified  Syntax ◦ setw(n) where n is the specified width and is an integer  For example ◦ cout<<setw(10)<<―Pakistan‖<<setw(15)<<―Islamabad‖;  Is a part of ―iomanip.h‖ header file 3/26/2015 70
  • 72. Example 1-11 #include <iostream> #include<iomanip> int main ( ) { cout<<setw(5)<<62<<setw(5)<<8<<endl; cout<<setw(5)<<100<<setw(5)<<77<<endl; cout<<setw(5)<<5<<setw(5)<<162<<endl; return 0; } 3/26/2015 72
  • 74. Example 1-12 (Assignment Statement) #include <iostream.h> int main ( ) { int a, b, c; a=200; b=100; c=a a=b b=c cout<<―value of a = ―<<a<<endl; cout<<―value of b = ―<<b<<endl; } 3/26/2015 74
  • 76. Example 1-13 #include <iostream> int main ( ) { int year, month; year = 20; month = year*12; cout<<―years = ―<<year<<endl; cout<<―months = ―<<month<<endl; return 0; } 3/26/2015 76
  • 78. The ―cin‖ Object— Input Stream  ‗cin‘ (see-in) stands for console input  This is an input stream  It is a part of iostream header file  It requires you to input from your keyboard during the execution of a program  Value is input and press Enter to input the value  Syntax  cin>>var1[>>var2….]; >> is an extraction operator or get from operator  Usually a separate statement is used for each variable  For example ◦ cin>>a>>b>>c; and press Enter to for typing each data 3/26/2015 78
  • 79. Example 1-14 #include <iostream.h> int main ( ) { int n1, n2, s, p; cout<<―Enter the first number ? ―; cin>>n1; cout<<―Enter the second number ? ―; cin>>n2; s=n1+n2; p=n1*n2 cout<<―Sum of numbers = ―<<s<<endl; cout<<―Product of numbers = ―<<p<<endl; } 3/26/2015 79
  • 81. Example 1-15 #include <iostream> { int age; long int age_mon; char name[50]; cout<<―Enter the name of the person ―; cin>>name; cout<<―Enter the age of person in years ―; cin>>age; age_mon = age*12; cout<<―Name of the person = ―<<name<<endl; cout<<―Age in months = ―<<age_mon<<endl; } 3/26/2015 81
  • 83. Example 1-16 float avg; char name[20]; int total, cpp, os, edp; cout<<―Enter the name of the student ―; cin>>name; cout<<―Enter the marks obtained in C++―; cin>>cpp; cout<<―Enter the marks obtained in Operating Systems―; cin>>os; cout<<―Enter the marks obtained in EDP―; cin>>edp; total = cpp+os+edp; avg = total/3.0; cout<<―Name of the student = ―<<name<<endl; cout<<―Total Marks = ―<<total<<endl; cout<<―Average Marks = ―<<avg<<endl; 3/26/2015 83
  • 85. Example 1-17 { float c, f; cout << "Enter the temperature in Fahrenheit? "; cin>>f; c=(f-32)*5.0/9.0; cout<<"The temperature in Celsius is = "<<c<<endl; return 0; } 3/26/2015 85
  • 87. Example 1-18 { float r,h,v; cout << "Enter the radius of the cylinder = "; cin>>r; cout << "Enter the height of the cylinder = "; cin>>h; v=3.14*r*r*h; cout<<"The volume of the cylinder is = "<<v; return 0; } 3/26/2015 87
  • 89. Compound Assignment Statement  We have previously used simple statements, to assign values to a variable  Like ◦ m=x*100+50 ◦ v=p*r*r*h  The same assignment statements can be used to assign one value to more than one variable.  E.g. ◦ x = y = 16; 3/26/2015 89
  • 90. Example 1-19 { int x, y, a, b, c, s; x = y = a = b = c = 515; s = x+y+a+b+c; cout << "the sum is = " <<s<< endl; return 0; } 3/26/2015 90
  • 92. Compound Assignment Expression  Is used to add, subtract, multiply etc a value to or from a variable ◦ Without writing on either side of op ‗=‗  The arithmetic operator is combined with the assignment operator (=)  Syntax ◦ var op = expression  xy = xy + 10; ◦ Here 10 is added to the variable xy ◦ Where xy already has some value 3/26/2015 92
  • 93. Compound Assignment Expression  Which can also be written as  xy + = 10;  Similarly x += 9; is the same as? ◦ x = x + 9;  x -=9; is the same as? ◦ x = x – 9; 3/26/2015 93
  • 94. Example 1-20 { int a,b,c; a=3; b=6; c=9; c*=(a+b); // c = c * (a+b) cout << "Result is = " <<c<< endl; return 0; } 3/26/2015 94
  • 96. The Comment Statement  Used to show comment on the coding statements in a program  Used to explain the logic of the program  It is a non-executable statement ◦ // This is a my first program in C++ ◦ // Java language is similar to C++  Can be given in any line of the program  e.g. ◦ sum=a+b //used to take sum of two variables 3/26/2015 96
  • 97. Example 1-21 { // declare the variables int a,c; //a&c are variable names a=10; //value 10 is assigned to a c=a*a*a; //assignment statement to calculate cube /* next statement is the output statement to print the cube of 10 */ cout << "cube of "<<a <<" = "<<c<< endl; // this is the end of the program return 0; } 3/26/2015 97