Slide Set - 10
Slide Set - 10
CP & CPSA
17ME & 17PG
Contents
• Data types in C++ (LL 04)
• Variables in C++ (LL 04)
• Variable declaration in C++ (LL 04)
• Variable definition in C++ (LL 04)
• Constants in C++ (LL 04)
• Variable as memory locations (LL 04)
• Inputting with cin statement (LL 02)
• Problem Examples (LL 04)
LL 02 = Learning Level 02 – Comprehension, LL 04 = Learning Level 04 – Analysis
Data Types in C++
Data Type Memory Range Start Range End
char 1 Byte -128 127
unsigned char 1 Byte 0 255
short 2 Bytes -32,768 32,767
unsigned short 2 Bytes 0 65,535
int 4 Bytes -2,147,483,648 2,147,483,647
unsigned int 4 Bytes 0 4,294,967,295
long 8 Bytes -9,223,372,036,854,775,807 9,223,372,036,854,775,807
unsigned long 8 Bytes 0 18,446,744,073,709,551,615
float 4 Bytes 3.4 × 10−38 3.4 × 1038
double 8 Bytes 1.7 × 10−308 1.7 × 10308
bool 1 Byte true and false
Variables in C++
• Variables are used to stored the data temporarily.
• A variable is a named piece of memory location.
• A single variable can store single value at a time.
• The value of the variable is changeable.
• The data type of the variable specifies the type of the value which will be
stored in it.
• The value is the actual content which will be stored in it. It is optional
because some time we know the exact value which will be stored, sometimes
we do not know.
Creating Variables in C++
• There are two ways to create a variable:
Variable Declaration in C++
data_type variable_name;
Variable Declaration in C++
float radius;
radius
Variable Declaration in C++
char v1 ; long v7 ;
unsigned char v2 ; unsigned long v8 ;
short v3 ; float v9 ;
unsigned short v4 ; double v10 ;
int v5 ; bool v11 ;
unsigned int v6 ;
Variable Definition in C++
radius
56.214
Variable Definition in C++
short V1 = 69 ; 2007H
2008H
2009H
200AH
200BH
200CH
200DH
200EH
200FH
2010H
2011H
Variables as Memory Locations
MEMORY
V1 2000H
69 2001H
V2 w 2002H
2003H
2004H
2005H
2006H
char V2 = ‘w’ ; 2007H
2008H
2009H
200AH
200BH
200CH
200DH
200EH
200FH
2010H
2011H
Variables as Memory Locations
MEMORY
V1 2000H
69 2001H
V2 w 2002H
V3 2003H
2004H
5478 2005H
2006H
int V3 = 5478 ; 2007H
2008H
2009H
200AH
200BH
200CH
200DH
200EH
200FH
2010H
2011H
Variables as Memory Locations
MEMORY
V1 2000H
69 2001H
V2 w 2002H
V3 2003H
2004H
5478 2005H
2006H
float V4 = 87.245F ; V4 2007H
2008H
87.245 2009H
200AH
200BH
200CH
200DH
200EH
200FH
2010H
2011H
Variables as Memory Locations
• Consider following memory map: MEMORY
2000H
V4 b 200FH
2010H
2011H
Variables as Memory Locations
• Consider following memory map: MEMORY
2000H
V4 b 200FH
2010H
2011H
Variables as Memory Locations
MEMORY
• Execute the following variables V1
2000H
2001H
definition statements in the same 2002H
memory map: 69.258 2003H
2004H
2005H
2006H
short V5 = 985 ; V2
658
2007H
2008H
V4 b 200FH
2010H
2011H
Variables as Memory Locations
MEMORY
2000H
V1 2001H
2002H
69.258 2003H
2004H
V5 2005H
985 2006H
V2 2007H
short V5 = 985 ; 658 2008H
2009H
200AH
V3 200BH
200CH
1453 200DH
200EH
V4 b 200FH
2010H
2011H
Variables as Memory Locations
MEMORY
V6 r 2000H
V1 2001H
2002H
69.258 2003H
2004H
V5 2005H
985 2006H
V2 2007H
char V6 = ‘r’ ; 658 2008H
2009H
200AH
V3 200BH
200CH
1453 200DH
200EH
V4 b 200FH
2010H
2011H
Variables as Memory Locations
MEMORY
V6 r 2000H
V1 2001H
2002H
69.258 2003H
2004H
V5 2005H
985 2006H
V2 2007H
short V7 = 74 ; 658 2008H
V7 2009H
74 200AH
V3 200BH
200CH
1453 200DH
200EH
V4 b 200FH
2010H
2011H
Inputting with cin
• Anything inputted from keyboard will go to the variable to the right side of
>> extraction operator.
Program Examples
Variables and constants in C++
Program Example 01
Problem Statement:
Write a computer program in C++ that accepts the base and height of a right
angle triangle from the user and displays the area of the triangle.
Program Example 01
Program Example 01
Program Example 02
Problem Statement:
A person is running in a circular ground. Write a program in C++ that asks the
user to input the radius of the ground in meters and the number of rounds the
person completes. The program should display the amount of distance
travelled by the person in meters.
Program Example 02
Program Example 02
Program Example 02
Program Example 03
Problem Statement:
Write a program in C++ that asks the user to enter two integer numbers, stores them
in variables num1 and num2 respectively. The program swaps the values of two
variables with each other without using a third variable and displays the values of
both the variables after swapping.
Sample output:
Input: Output:
num1 = 45 num1 = 94
num2 = 94 num2 = 45
Program Example 03
Program Example 03
Program Example 03