CS 101:
Computer Programming and
Utilization
Puru
with
CS101 TAs and Staff
Course webpage: https://www.cse.iitb.ac.in/~cs101/
Lecture 3: Variables, Expressions and Numbers
Autumn 2019 CS101@CSE IIT Bombay
this week
• Lab2
• variables, expressions
• number representation
• input/output programs and more loops based
programs
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 2
about these slides
• Based on Chapter 2 and 3 of the book
An Introduction to Programming Through C++
by Abhiram Ranade (Tata McGraw Hill, 2014)
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 3
some questions
• How to store data in the memory of a computer?
• How to store numbers in memory?
• How to perform arithmetic?
• How to read numbers into the memory from the keyboard?
• How to print numbers on the screen?
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 4
variable declaration
a general statement of the form
data_type_name variable_name;
creates and declares variables (named space in memory to store data)
earlier example …
int sides;
int : name of the data type. Short form for integer. Says reserve space
for storing integer values
sides : name given to the reserved space, or the variable created
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 5
variable declaration
0
1
2
3
4
5 .......
6
7
8
9
32 bits
int sides;
Results in a memory location being reserved for this variable. The
program will refer to it by the name sides
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 6
variable names: identifiers
• sequence of letters, digits & the underscore “_” character
• case matters. ABC, Abc and abc are distinct identifiers
• should not begin with a digit
• some words such as int cannot be used as variable names. reserved
by C++ for its own use
• Which of these are valid variable names:
sides, #sides, 3rd_cousin, third cousin, tele_number, x, third_cousin,
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 7
naming practices
• use meaningful names, describing the purpose for which
the variable will be used
exterior_angle = sum_exterior/num_sides;
• never use single letter variable name unless appropriate in
the context.
Example:
u*t+1/2*a*t*t
e = s/n
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 8
the assignment operator
• assignment operator =
exterior_angle = sum_exterior/num_sides;
used to store results of computation into a variable
Form: variable_name = expression;
• expression
a constant (a number) or
a variable name or
a formula involving constants or variables or
a function call
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 9
variable initialization examples
initialization - an INITIAL value is assigned
to the variable
the value stored in the variable at the time of its int i=0;
creation
double vx=1.0;
double vy=2.0e5;
variables i, vx, vy are declared and are
initialized
char value = ‘f’;
2.0e5 is how we write 2.0*105
‘f’ is a character constant
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 10
more examples
int x=2, y=3, p=4, q=5, r, s, t;
x = rt;
x = r*s;
r = x*y + p*q;
s = x*(y+p)*q;
t = x – y + p – q;
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 11
more examples
int x=2, y=3, p=4, q=5, r, s, t;
x = rt; // syntax error!
x = r*s; // disaster! r, s undefined
r = x*y + p*q; // r becomes 2*3 + 4*5 = 26
s = x*(y+p)*q; // s becomes 2*(3+4)*5 = 70
t = x – y + p – q; // equal precedence,
// so evaluated left to right,
// t becomes (((2-3)+4)-5 = -2
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 12
reading values into variables
• can read value of a variable from the terminal
• can read into several variables one after another
• int, char etc. can be read cin >> num_sides;
cin >> vx >> vy;
• user expected to type in values consistent
char command;
with the type of the variable into which it is
cin >> command;
to be read
• whitespaces (i.e. space characters, tabs, newlines)
typed by the user are ignored
• newline/enter key must be pressed after values are typed
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 13
printing variables on the screen
• general form: cout << variable; cout << x;
• many values can be printed one cout << x << y;
after another cout << x << “, “ <<
• need to put “ “ if you want to have gap y;
between successive values
cout <<“Position: " <<
• to print newline, use endl x << “, “ << y <<
endl;
• text can be printed by enclosing
it in quotes
char var = ‘G’;
cout << var;
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 14
arithmetic between different types allowed
int x=2, y=3, z, w;
double q=3.1, r, s;
r = x; // representation changed
// 2 stored as a double in r "2.0"
z = q; // store with truncation
// z takes integer value 3
s = x*q; // convert to same type,
// then multiply
// Which type?
cout << (360/240) << “ ” << (360.0/240) << “ ” << (360/240.0);
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 15
evaluating varA op varB
• e.g. x*q
if varA, varB have the same data type: the result will have same
data type
• if varA, varB have different data types: the result will have more
expressive data type
• int/short/unsigned int are less expressive than float/double
• shorter types are less expressive than longer types
• we will only use int and double in this course
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 16
integer division, evaluation order
int x=2, y=4, p=3;
double u;
u = x/p + y/p;
cout << u << (x+y)/p;
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 17
integer division, evaluation order
int x=2, y=4, p=3;
double u;
u = x/p + y/p;
cout << u << (x+y)/p;
• x/p : both are int. So truncation. Hence 0
• y/p : 4/3 after truncation will be 1
• So the output is “1 2”
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 18
more examples (homework)
int num_sides=100;
int i_angle1, i_angle2;
double d_angle1, d_angle2;
d_angle1 = 360/num_sides + 0.45; // 3.45
i_angle1 = 360/num_sides + 0.45; // 3
d_angle2 = 360.0/num_sides + 0.45 // 4.05
i_angle2 = 360.0/num_sides + 0.45; // 4
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 19
a sample program
main_program{
double centigrade, fahrenheit;
cout << “Give temperature in Centigrade: ”;
cin >> centigrade;
fahrenheit = centigrade * 9.0 / 5.0 + 32;
cout << “In Fahrenheit: ” << fahrenheit
<< endl; // newline
}
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 20
re-assignment
• Same variable can be assigned a value again
• When a variable appears in a statement, its value at the
time of the execution of the statement gets used
int p=3, q=4, r;
r = p + q; // 7 stored into r
cout << r << endl; // 7 printed
r = p * q; // 12 stored into r
cout << r << endl; // 12 printed
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 21
in C++ `=‘ is assignment, not equal’
int p=12;
p = p+1;
See it as: p p+1; // Let p become p+1
rule for evaluation:
• FIRST evaluate the RHS and THEN store the result into the
LHS variable
• 1 is added to 12 (the value of p)
• the result, 13, is then stored in p
• thus, p finally becomes 13
p = p + 1 is false in mathematics
“=” in C++ is different from “=” in mathematics
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 22
repeat and re-assignment
main_program{
int i=0;
repeat(10){
i = i + 1;
cout << i << endl;
}
}
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 23
add 10 numbers based on user input
• using repeat and re-assignment
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 24
another re-assignment example
main_program{
int term, s = 0;
repeat(10){
cin >> term;
s = s + term;
}
cout << s << endl;
}
• values read are summed into s
• we could have used other binary operators too, say
multiplication, or gcd or max or min
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 25
fundamental idiom
main_program{
int i=0;
repeat(10){
i = i + 1;
cout << i << endl;
}
}
sequence generation
• can you make i take values 1, 3, 5, 7, …?
• can you make i take values 1, 2, 4, 8, 16, …?
• both can be done by making slight modifications to
above program
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 26
composing two idioms
Write a program to calculate n!
5! = 1 x 2 x 3 x 4 x 5
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 27
composing two idioms
Write a program to calculate n! given n.
main_program{
int n, nfac=1, i=1;
cin >> n;
repeat(n){ Accummulation idiom
nfac = nfac * i;
i = i + 1;
}
cout << nfac << endl;
Sequence idiom
}
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 28
finding remainder
• x % y computes the remainder of dividing x by y
• Both x and y must be integer expressions
• Example
int n=12345678, d0, d1;
•
d0 = n % 10; // 8
d1 = (n / 10) % 10; // 7
•
• d0 will equal 8 (the least significant digit of n)
• d1 will equal 7 (the second least significant digit of n)
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 29
summary
• variables are regions of memory which can store values & have a type, as
decided at the time of creation
• choose names to fit the purpose for which the variable is defined
• name of the variable may refer to the region of memory (if the name appears on
the left hand side of an assignment), or
its value (if the name appears on the right hand side of an assignment)
• operators have an ordering (* / before + -)
• if variables have different data types: result will have more expressive data type
• int/short/unsigned int are less expressive than float/double
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 30
more practice
• What is the result of evaluating the expression (3+2)/4?
• What is printed by this code snippet …
float f=6.022E23; float r=f+2-f; cout<<r;
• What is printed by this code snippet
int t=10; repeat(2){t=t-1.2;} cout<<t;
• What is printed by this code:
int i=2, j=3, k=4; i=j; j=k; k=i; cout << (i*j*k);
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 31
Numbers, number
representation and variables
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 32
why numbers?
• problem solving using computers is about
problem solving on numbers
• examples
– salaries, temperature, length, distance, voltage …
– picture, language, characters, …
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 33
data types of C++
• char : used for storing characters or small integers
− 1 byte will be allocated
− ASCII code of characters is stored
• float : used for storing real numbers
− 1 word will be allocated
− IEEE FP representation, 8 bits exponent, 24 bits significand
• double : used for storing real numbers
− 2 words will be allocated
− IEEE FP representation, 11 bits exponent, 53 bits significand
• unsigned int : used for storing integers which will always be
positive
− 1 word (32 bits) will be allocated
− ordinary binary representation will be used
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 34
How are all these types represented
in memory/storage as bits?
How are operations on all these types
performed in terms of bits?
Autumn 2019 CS101@CSE IIT Bombay
radix-based number systems
• Number systems with radix r has r symbols (including 0)
• (x)r x is a string of symbols, r is the radix/base
• (100)10, (100) 2, (100) 8, (500) 8 , (100) 16, (ab)16
• key idea: position of a symbol determines it's value! PLACE
VALUE
– Multiply from right to left by: r0, r1, r2, r3, ... and then add
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 36
decimal number system
• RADIX is 10
SYMBOLS: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
• Place-Values: 1, 10,100,1000...
• In the decimal system: 346
− Value of "6" = 6
− Value of "4" = 4 x 10
− Value of "3" = 3 x 10 x 10
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 37
hexadecimal number system
• RADIX is 16. Place Value: 1, 16, 256, 4096,....
• 16 digits needed : 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f
• 23 in hexadecimal
– Value of 3 = 3
– Value of 2 = 2 x 16
– Value of 23 in hexadecimal = 35 in decimal
• 45171 in hexadecimal =
– 1+16*7+16*16*1+16*16*16*5+16*16*16*16*4
= 282993 in decimal
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 38
binary system
circuits have wires that carry current, and are at a certain electric potential
digital circuits interpret electrical potential/voltage as numbers
simple convention
voltage above 5 volt = number 1
voltage between 0 and 0.2 volt = number 0
circuits designed so that voltage will never be between 0.2 and 5 volt, hence no
ambiguity
an inverter circuit
Copyright © 1999-2000 Michael Stutz stutz@dsl.org
capacitors (like batteries) can store electrical
charges
Autumn 2019 CS101@CSE IIT Bombay
the binary system
• Radix= 2 (two bits)
• Needs ONLY ` digits : 0 and 1
• Place-value: powers of two
128 64 32 16 8 4 2 1
•
11 in binary:
– Value of rightmost 1 = 1
– Value of next 1 = 1 x2
– 11 in binary = 3 in decimal
• 1100101
=
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 40
decimal to binary conversion
128 64 32 16 8 4 2 1
1 0 0 1 1 0 1 0
• Decimal to binary conversion
– Express it as a sum of powers of two
• Example: the number 154 in binary:
– 154 = 128 + 16 + 8 + 2
– 154 = 1 x 27 + 0 x 26 + 0 x 25 + 1 x 24 + 1 x 23 +0 x 22 + 1 x 21 +
0 x 20
– Thus, 154 in binary is 10011010
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 41
fractions in binary
• powers on the right side of the point are negative:
8 4 2 1 1/2 1/4 1/8 1/16
• binary 0.1 = 0 + 1 x 2-1 = 0.5 in decimal
• in binary 0.11 = 0x 1 + 1 x 2-1 + 1 x 2-2
= 0.5 + 0.25 = 0.75 in decimal
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 42
fractions and precision
• represent decimal number 1.45 using 3 bits and
4 bits
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 43
fractions and precision
• represent decimal number 1.45 using 3 bits and
4 bits
• with 3 bits: 1.11 => 1.375
• with 4 bits: 1.111 => 1.4375
• for actual storage as numbers all factions
converted to mantissa + exponent form
• length of mantissa determines precission!
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 44
representing non-negative numbers
• the number of bits (capacitors/wires) used cannot be chosen
arbitrarily
• choices allowed: 8, 16, 32, 64 (width in bits)
• example: To store 9 using 8 bits:
− 9 decimal = 0000 1001 binary
− so store the following charge pattern (H=High, L=Low)
− LLLLHLLH
• range of numbers that can be stored: 0 to 28 – 1
If your numbers are likely to be larger, then use 16/32/64 bits
• choose the number of bits depending upon how large you expect
the number to be.
Autumn 2019 CS101@CSE IIT Bombay
representing positive and negative integers
• one of the bits is used to indicate sign
• sign bit = 0 means positive, = 1 means negative number
• to store -9 :
− 1000 1001, Leftmost bit = sign bit
• range stored: -(28 – 1) to 28 – 1
Autumn 2019 CS101@CSE IIT Bombay
representing positive and negative integers
• One of the bits is used to indicate sign
• Sign bit = 0 means positive, = 1 means negative number
• To store -9 use
− 10001001, Leftmost bit = sign bit
• Range stored: -(28 – 1) to 28 – 1
• Actual representation: Two’s complement
– If x is positive: (0 <= x <= 2n-1 – 1)
• Binary form of x
– If x is negative ( -2n-1 <= x < 0)
• Binary form of 2n + x
• E.g. -9 in 2's complement: (100000000 - 000001001) = 11110111 = 247 decimal
Autumn 2019 CS101@CSE IIT Bombay
representing non-negative numbers
• The number of bits (capacitors/wires) used cannot be chosen
arbitrarily
• choices allowed: 8, 16, 32, 64
• example: To store 25 using 32 bits:
− 25 Decimal = 00000000000000000000000000011001
− So store the following charge pattern (H=High, L=Low)
− LLLLLLLLLLLLLLLLLLLLLLLLLLLHHLLH
• range stored: 0 to 232 – 1. If your numbers are likely to be
larger, then use 64 bits
• choose the number of bits depending upon how large you
expect the number to be.
Autumn 2019 CS101@CSE IIT Bombay
representing positive and negative integers
• One of the bits is used to indicate sign
• Sign bit = 0 means positive, = 1 means negative number
• To store -25 use
− 10000000000000000000000000011001, Leftmost bit = sign bit
• Range stored: -(231 – 1) to 231 – 1
Autumn 2019 CS101@CSE IIT Bombay
representing positive and negative integers
• One of the bits is used to indicate sign
• Sign bit = 0 means positive, = 1 means negative number
• To store -25 use
− 10000000000000000000000000011001, Leftmost bit = sign bit
• Range stored: -(231 – 1) to 231 – 1
• Actual representation: Two’s complement
– If x is positive: (0 <= x <= 2n-1 – 1)
• Binary form of x
– If x is negative ( -2n-1 <= x < 0)
• Binary form of 2n + x
• E.g. -25 in 2's complement: 11111111111111111111111111111100111
= (100000000000000000000000000000000 -
00000000000000000000000000011001)
Autumn 2019 CS101@CSE IIT Bombay
the two’s complement wrap-around
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 51
the two’s complement wrap-around
Min=10…0 -1=1…1 0=0…0 Max=01…1
Zero is one position
to the right of center
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 52
Representing floating point numbers
• Use an analogue of scientific notation:
• significand * 10exponent, e.g. 6.022 * 1022
• Significand (with a bit for sign) and exponent are in binary
• significand * 2exponent
Costs how No of bits for No of bits for
many bits to mantissa exponent
store
float 32 24 8
double 64 52 12
• Actual representation: more complex. “IEEE Floating Point
Standard”
Autumn 2019 CS101@CSE IIT Bombay
Example
• Let us represent the number 3450 = 3.45 x 103
• First: Convert to binary:
• 3450 = 211+ 210+ 28 + 26+ 25+24 +23 + 21
11 10 9 8 7 6 5 4 3 2 1 0
1 1 0 1 0 1 1 1 1 0 1 0
• Thus 3450 in binary = 110101111010
• 3450 in significand-exponent notation: how?
• 1.10101111010 x 21011
− 10 in binary is 2 in decimal
− 1011 in binary is 11 in decimal, we have to move the
"binary point" 11 places to the right
Autumn 2019 CS101@CSE IIT Bombay
Example Continued
For computer representation:
• Use 23 bits for magnitude of significand, 1 bit for sign
• Use 7 bits for magnitude of exponent, 1 bit for sign
01101011110100000000000000001011
• Decimal point is assumed after 2nd bit.
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 56
Different Needs, Different Variable Types
• The keyword long : says, I unsigned int
telephone_number;
need to store bigger or
more precise numbers, so float velocity;
give me more than usual
float mass, acceleration;
space.
• long unsigned int: Likely 64 long unsigned int
crypto_password;
bits will be allocated
• long double: likely 96 bits long double
more_precise_vaule;
will be allocated
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 57