Intro to Programming Week # 2
Variables/Data type Lecture # 4 & 5
By: Saqib Rasheed
Department of Computer Science & Engineering Air University
// First Program
// Author Saqib Rasheed
#include <iostream>
using namespace std;
int main() {
cout << "Hello world!";
return 0;
}
A First Program - Greeting.cpp
Greeting Output
By Saqib Rasheed 3
Whitespace
#include <iostream>
using
namespace std;
int main () { cout
<<
“Hello word!\n”
; return
0;}
• Reference Chapter # 2:
C++ Programming Basics by Robert Lafore
By Saqib Rasheed 5
Escape Sequence
• Escape sequences are used to represent certain special characters within string
literals and character literals.
• \n new line
• \t horizontal tab (8 spaces)
• \’ single quote
• \” double quote
• \? question mark
• \v vertical tab
• \a audible bell
Example # 2
#include <iostream>
using namespace std;
int main ( ) {
cout << “\t Welcome to Air University \n“;
cout<<“ \n\t School of Engineering”;
cout<<“it can be\n used \t between\n the string”;
return 0;
}
New Line Tab
By Saqib Rasheed 7
Variables
• A storage box, its type defines the kind of stuff you store in it
– E.g. shoe box used for shoes
– Jewelry box used for jewelry
– CD kit used for CDs
Variable
• Variable is a box
• In computer, a storage box is a memory location on RAM
By Saqib Rasheed 9
Memory in Computer
• First you ask computer to give you a box and you also have to specify what type of box you want
Occupied Memory Spaces
Free Memory Spaces
Each little box is a memory
location
Asking for Memory
Type of Box box name;
Type is a bit
messy, it can’t be shoe box or
jewelry
Could be anything, alpha, bravo, charlie
integer character double float
bool (etc)
Some rules apply
By Saqib Rasheed 11
Rules for Variable name
Rules to Write Variable.
1. First Letter is always Character
2. Underscore _ (Not Recommended) 3. Can’t use real numbers (Syntax Errorr) int num1;
Rules for Variable name
In a program a variable has:
1. Name 2. Type 3. Size 4. Value
int num = 10;
Assignment Operator
By Saqib Rasheed 13
Rules of variables
• int n1;
• int n1,n2,n3;
• int n1; int n2; int n3;
• float a1; int b1; double c1;
Type Size (bytes) Range-Minimum Range- Maximum
Character 1 -128 127
Unsigned Char 1 0 255 Signed Char 1 -128 127
Integer 4 -2,147,483,648 2,147,483,647 Signed Int 4 0 ,4294,967,295
Unsigned Int 4 -2,147,483,648 2,147,483,647 Short Int 2 -32,768 32,767
Signed short Int 2 0 65,535
Unsigned Short Int 2 -32,768 32,767
Long Int 4 -2,147,483,648 2,147,483,647 Signed Long Int 8 0 4,294,967,295
Unsigned Long Int 8 -2,147,483,648 2,147,483,647 Float 4 1.8E-38 3.4E+38
Double 8 2.2E-308 1.8E+308 Long Double 8 2.2E-308 1.8E+308
Bool 1 True/False
By Saqib Rasheed 15
How To get Value from User
How to get value from user we use
cin>>x;
Also defined in <iostream> library file
16
Displaying age
#include<iostream>
using namespace std;
int main (){
float a1,a2;
cout<<“Enter age for student 1=”;
cin>>a1;
cout<<“Enter age for student 2=”;
cin>>a2;
cout<<a1;
cout<<a2;
return 0;
} By Saqib Rasheed 17
Displaying age
#include<iostream>
using namespace std;
int main (){
float a1,a2;
cout<<“Enter age for student 1=”;
cin>>a1;
cout<<“Enter age for student 2=”;
cin>>a2;
cout<<“\nAge of Student 1 = ”<<a1;
cout<<“\nAge of Student 2 = ”<<a2;
return 0;
Displaying age
#include<iostream>
using namespace std;
int main (){
float a1,a2;
cout<<“Enter age for student 1=”;
cin>>a1;
cout<<“Enter age for student 2=”;
cin>>a2;
cout<<“\nAge of Student 1 = ”<<a1;
cout<<“\nAge of Student 2 = “ <<a2;
return 0;
} By Saqib Rasheed 19
Displaying age
#include<iostream>
using namespace std;
int main (){
float a1,a2;
cout<<“Enter age for student 1=”;
cin>>a1;
cout<<“Enter age for student 2=”;
cin>>a2;
cout<<“\nAge of Student 1 = ”<<a1;
cout<<“\nAge of Student 2 = “;
cout <<a2;
return 0;
Average age Example
#include<iostream>
using namespace std;
int main (){
float a1,a2,a3,a4,a5;
cout<<“Enter age for student 1=”;
cin>>a1;
cout<<“Enter age for student 2=”;
cin>>a2;
: :
float sum, avg;
sum = a1+a2+a3+a4+a5;
Avg = sum / 5;
cout<<“Average age = ”<<avg;
return 0;
}
By Saqib Rasheed 21
Arithmetic Operators
• Common
– Addition + – Subtraction - – Multiplication * – Division /
– Mod %
• Note
– No exponentiation operator – Single division operator
– Operators are overloaded to work with more than one type of object
Write m*x + b not mx + b
Integer Division
• Integer division produces an integer result
– Truncates the result
• Examples
– 3 / 2 evaluates to 1 – 4 / 6 evaluates to 0 – 10 / 3 evaluates to 3
By Saqib Rasheed 23
Mod
• Produces the remainder of the division
• Examples
– 5 % 2 evaluates to 1 – 12 % 4 evaluates to 0 – 4 % 5 evaluates to 4
Operators and Precedence
• Consider mx + b
Consider m*x + b which of the following is it equivalent to
(m * x) + b
m * (x + b)
Operator precedence tells how to evaluate expressions
Standard precedence order
() Evaluate first, if nested innermost done first
* / % Evaluate second. If there are several, then evaluate from left-to-right
+ - Evaluate third. If there are several, then evaluate from left-to-right
By Saqib Rasheed 25
Operator Precedence
• Examples
20 - 4 / 5 * 2 + 3 * 5 % 4
(4 / 5)
((4 / 5) * 2)
((4 / 5) * 2) (3 * 5)
((4 / 5) * 2) ((3 * 5) % 4) (20 -((4 / 5) * 2)) ((3 * 5) % 4) (20 -((4 / 5) * 2)) + ((3 * 5) % 4)
27 By Saqib Rasheed
Precedence
21.2 192 19.2
24 8
X = 2 + 4 * 6 ( 10 – 2 ) / 10 Y = 10 / 5 * 2 ( 8 - 2 ) + 6
Write Code for this equation
12 3
5
Quadratic Equation
• In algebra
y = ax2 + bx + c
• In C
y = a*x*x + b*x + c
29 By Saqib Rasheed
#include <iostream>
using namespace std int main ( ) {
int x ; int y ; int z ; x = 10 ; y = 20 ; z = x + y ;
cout << " x = " ; cout << x ;
cout<< " Y = " <<y;
cout << " z =x + y " << x+y ; }
• int x, y, z ;
• int x; int y; int z ;
• char n1, n2 = ‘a’;
• float num1, num2 = 30.8, n3;
• double n3;
Declaration of Variables
Calculator Code
#include <iostream>
using namespace std int main ( ) {
int num1, num2, res;
cout<<“Enter Number 1 =”;
cin>>num1;
cout<<“Enter Number 2 =”;
cin>>num2;
res = num1 + num2;
cout<<“Addition Result = ”<<res;
res = num1 - num2;
cout<<“Subtraction Result = ”<<res;
res = num1 * num2;
cout<<“Multiplication Result = ”<<res;
res = num1 / num2;
cout<<“Division Result = ”<<res;
res = num1 % num2;
cout<<“Modulation Result = ”<<res;
} 31
Assignment # 1
• Get five equations and write code for each equation
– The depth of equation should be at least 6 numbers – Get these numbers from user (Key board)
– Display the equation properly (Before ) – Display the Result
• Instructions
– Individual assignment – Due date is next class
– Copied assignments will be marked zero – Late assignments not accepted
– Submit hard copies along with out put (Screen Shots)
• Reference Chapter # 2:
C++ Programming Basics by Robert Lafore
• https://sites.google.com/site/saqibrashied/cplusplus
By Saqib Rasheed 33