Programming Fundamentals CSC - 112
Programming Fundamentals CSC - 112
CSC - 112
Lecture 2
Input and Output Streams
• A data
stream is a sequence of data
- Typically in the form of characters or numbers
#include <iostream>
Example
int ftemp;
cin>>ftemp;
• Example:
cin >> v1 >> v2 >> v3;
Location 0
Location 1
Location 2 Each location is
Location 3 1 byte of memory
CPU Location 4
1 byte = 8 bits
Location 5
a value.
• This space is reserved until the variable is
required.
Session 2 7
What Makes a Variable
• Variable has three important characteristics:
- Type
• How much memory do a variable need.
- This information is determined by a type.
- Name
• How to differentiate a variable with another variable of the
same type.
- Name refers to the memory location assigned to this variable.
- Value
• What is the value?
- The actual value contained by a variable.
Session 2 8
An Example of a Variable
int temperature = 35
Session 2 9
Example of a Variable
(Memory View)
int temperature = 35
00000000 Location 0
Locations 0 - 3 are collectively 00000000 Location 1
called as ‘temperature’ 00000000 Location 2
00100011 Location 3
Location 4
100011 is the binary equivalent of 35 Location 5
Session 2 10
Changing the Value of Variable
• Lets change the value of ‘temperature’.
temperature = 45902
00000000 Location 0
Locations 0 - 3 are collectively 00000000 Location 1
called as ‘temperature’ 10110011 Location 2
01001110 Location 3
Location 4
Location 5
1011001101001110 is the binary equivalent of 45902
Session 2 11
Variable Capacity
• Among other advantages a ‘type’ determines the
maximum space that can be used by a variable
• The type int is of 4 bytes in C++.
2,147,483,647 value.
• It can also hold values in negative down to
-2,147,483,648.
Session 2 12
Initializing Variables
• Declaring a variable does not give it a value
- Giving a variable its first value is initializing the variable
• Variables are initialized in assignment statements
Session 2 14
Relative Comparison of int and double
int numPeople = 2;
Session 2 15
Session 2 16
Assignment in C++
• Assignment Statement
- In Mathematics the value x = x + 1 is not
possible why?
- In C++ x = x +1 is possible because “=” is an
assignment operator and not an equality
operator.
- Assignment operator means that the contents of
the right hand side is transferred to the memory
location of the left hand side.
Sessio 2 17
Assignment Statement
x = 5671
Session 2 18
Constants
• Constants are values which cannot be
modified e.g. the value of Pi
• To declare a constant in C++, we write a
Session 2 19
Reserved Words
• Some names cannot be declared as variable names
because they are reserved words in C++
20
Variables
• You can store your program (algorithm) data into variables (also
called memory locations). In “C++” language there are various
types of variable available.
Variable types
Variable type Keyword used in Size in bits Range
declaration
integer int 32 bits -2147483648 to
2147483647
Short integer short int 16 bits -32768 to 32767
27
Type char
• Computers process character data too
• char
bool old_enough;
Manipulator
• Manipulators are instructions to the output
stream that modify the output in various ways
-endl is a manipulator that causes a linefeed to
be inserted into the stream
-cout<<“Sample text”<<endl;
Casting
•Converting a value of one type into another
type
•Manual Casting
• static_cast<double> (intVar)
• e.g int n = static_cast<int>(3.14);
•Automatic Casting
• Automatic conversion of lower order type to higher
order type when two operands of different types are
encountered in the same expression
Practice Question
Calculate a weighted average sum of two numbers x1 and x2 such that x1 has
70% weight.