3-Lecture-FoP-number Systems and Variables
3-Lecture-FoP-number Systems and Variables
Dr Ayesha Zeb
Email: [email protected]
Lecture Contents
• Number systems
• Variables and memory
• Types of variables
Review – A Basic Program
//EC 100 Algorithms and Computing
//A first program
//Hello World – displays “Hello
World!”
//on the screen
#include <iostream>
int main()
{
cout << "Hello, World!";
return 0;
}
Review – A Basic Program with Input/Output
int main()
{
How are things stored in a computer?
0 0 0 0 0 0 1 0
8 bits = 1 byte
Representing information in computers
string name;
cout << "Please enter your name" <<
endl;
cin >> name;
cout << "\n Hello "<<name<<endl;
Primary
Memory
Loader
Loader puts program
in memory.
Disk ..
[name here]
..
..
Primary
Memory CPU takes each
CPU instruction and
executes it, possibly
storing new data
..
john ..
..
values as the program
executes.
Representing Text
• Information – Data
• Two ways to represent numerical data
– signed (default)
– unsigned
• Signed data will have its sign (positive or negative) for its
MSB (most significant bit)
– Will this effect its range?
Representing Different Types of Information
• Information – Data
• Two ways to represent numerical data
– signed (default)
– unsigned
• Signed data will have its sign (positive or negative) for its
MSB (most significant bit)
– Its range will be reduced by half!
Representing Different Types of Information
• Information – Data
• Two ways to represent numerical data
– signed (default)
– unsigned
• Signed data will have its sign (positive or negative) for its
MSB (most significant bit)
– Its range will be reduced by half!
• Similarly unsigned data will not have a sign (it will always
be positive)
– Its range will be twice as much of signed!
int main()
Variables {
int x = 1564983;
char c;
string name;
• Programs operate on cout << "Please enter
your name" << endl;
and produce data values ………
• Data values are stored }
in memory/RAM char c;
0 11010010
• ‘Variables’ are memory A single byte
variable 1 01001011
locations reserved to
2 10010000
store a piece of data of
3 11101010
specific size and type
int x = 4 01101000
1564983; 5 11010001
A four-byte 6 01101000
variable 7 11010001
8 00001110
… …..
1024 10101010
Variables
• Variables have:
– Location – address in memory where they are located/stored
– Type
– Name/identifier – what the programmer uses to access that
memory location
• C++ variables are ‘typed’
– You must declare (tell the compiler that the variable exists)
before you use it
– Indicated what kind of information you will be storing
• int x = 5;
• float y = 0.5;
• int z = 2.3;
Variable Declaration
• A variable declaration
– Defines the name of the variable
– Defines the type of the variable (int, float, etc.)
– Gives the programmer a description of the variable
• Declaration format:
– type name; //comment
– Examples:
– int sum; //stores ‘sum’ in int type variable
– float answer; //stores ‘answer’ in float type variable
– char something;
– string name
Naming Variables
int b = 9;
a: 7
char c = 'a';
b: 9
char a;
c: 'a'
double x = 1.2;
a: ### garbage
string s1 = "Hello, world";
x: 1.2
string s2 = "1.2";
s1 12 | "Hello, world"
:
s2: 3 | "1.2"
A Simple Program Using Variables
1 // Fig. 1.6: fig01_06.cpp Enter first integer
2 // Addition program. 45
3 #include <iostream> Enter second
4 integer
5 // function main begins program execution 72
6 int main() Sum is 117
7 {
8 int integer1; // first number to be input by user
9 int integer2; // second number to be input by user
10 int sum; // variable in which sum will be stored
11
12 std::cout << "Enter first integer\n"; // prompt
13 std::cin >> integer1; // read an integer
14
15 std::cout << "Enter second integer\n"; // prompt
16 std::cin >> integer2; // read an integer
17
18 sum = integer1 + integer2; // assign result to sum
19
20 std::cout << "Sum is " << sum << std::endl; // print sum
21
22 return 0; // indicate that program ended successfully
23
24 } // end function main © 2003 Prentice Hall, Inc.
All rights reserved.
Variables and Memory
integer1 45
sum = integer1 + integer2; integer2 72
sum 117
2/10/2024
Variables and Memory
• Destructive Process
– Whenever a value is placed in a memory location it overwrites
the previous value in that location placing a new value into that
memory location.
• Nondestructive Process
– Whenever a value is read from a memory location, the value of
that variable in that memory location remains unchanged.
Memory
• For example
sum = num1 + num2;
// add the nums and store result in sum
– The values of ‘num1’ and ‘num2’ will be read out and used in the
calculation of sum. So it will be a nondestructive process.
– However, the result of the addition process will be stored in the
variable ‘sum’. So it will be a destructive process.
• Thus, it is always best to declare a variable as null or
zero.
When to use variables
• Saving values
• Reusing values
• Getting values from the user/at runtime
• Will the values change ?
• Making code more readable
Which Type?